Source code for cgl.ui.tools.assetizer.maya
try:
from maya import cmds
except ImportError:
pass
import cgl.plugins.smart_task as smart_task
from cgl.core.path.object import PathObject
from importlib import reload
reload(smart_task)
[docs]
def tag_selected_asset(selection=None, filepath=""):
"""
This function tags the current selection with the asset we've exported it to
this will allow us to track the asset in the future
Returns:
"""
# create an extra attribute on the selected object called "asset_path"
# this will allow us to track the asset in the future
if not selection:
selection = cmds.ls(selection=True)[0]
# add attrbiute to the selection
try:
cmds.addAttr(selection, longName="asset_path", dataType="string")
except TypeError:
print("Attribute already exists")
cmds.setAttr("{}.asset_path".format(selection), filepath, type="string")
[docs]
def recenter_selected(asset_name, filename="", task="mdl"):
"""
This function re-centers the selected object in the scene. It's used when a mesh it not at the origin.
We take its current position, store that in memory
then move it to the origin,
then create a locator
then move the locator to the stored position
then parent the mesh to the locator
Returns:
"""
selection = cmds.ls(selection=True)[0]
pos = cmds.xform(selection, query=True, translation=True, worldSpace=True)
cmds.xform(selection, translation=(0, 0, 0))
# export the selected model while it's at origin
if filename:
smart_task = load_smart_task(task=task)
smart_task().export_selected(filename)
locator = cmds.spaceLocator()
locator = cmds.rename(locator, "{}_loc".format(asset_name))
cmds.parent(selection, locator)
cmds.xform(locator, translation=pos)
if filename:
cmds.select(locator)
tag_selected_asset(selection=None, filepath=filename)
cmds.rename(selection, f"{selection}_old_geo")
return locator
[docs]
def load_smart_task(task, dcc="maya"):
"""
This function loads the smart task for the current selection.
Returns:
"""
task_class = smart_task.get_task_class(dcc, task)
return task_class
[docs]
def replace_with_published_reference(delete=False):
"""
This function replaces the selected object with the published reference.
Returns:
"""
# see if the object has the "asset_path" attribute
selection = cmds.ls(selection=True)[0]
pos = cmds.xform(selection, query=True, translation=True, worldSpace=True)
if cmds.attributeQuery("asset_path", node=selection, exists=True):
# get the asset path
asset_path = cmds.getAttr("{}.asset_path".format(selection))
asset_obj = PathObject().from_path_string(asset_path)
namespace = asset_obj.shot
cmds.file(asset_path, reference=True, namespace=namespace)
ref = select_objects_in_namespace(namespace)
if delete:
cmds.delete(selection)
cmds.xform(ref, translation=pos)
organize_ref(ref)
else:
print("No asset path found for selection")
[docs]
def select_objects_in_namespace(namespace):
"""
This function selects all the objects in the given namespace
Returns:
"""
cmds.select(clear=True)
objects_in_namespace = cmds.ls(namespace + ":mdl", long=True)
if objects_in_namespace:
cmds.select(objects_in_namespace)
return cmds.ls(selection=True)[0]
else:
print(f"No objects found in namespace '{namespace}'")
return None
[docs]
def organize_ref(ref=None):
"""
Args:
ref:
Returns:
"""
# get the file path of the reference
if not ref:
ref = cmds.ls(selection=True)[0]
ref_path = cmds.referenceQuery(ref, filename=True)
po = PathObject().from_path_string(ref_path)
# get the asset category
category = po.sequence.upper()
# parent the reference to WORLD|category
print(ref, category)
cmds.parent(ref, category.upper())
[docs]
def replace_locators(delete=True):
"""
go through all the items named "*_loc" and replace them with the published reference
Returns:
"""
cmds.select(clear=True)
locators = cmds.ls("*_loc")
for loc in locators:
print(loc)
cmds.select(loc)
replace_with_published_reference(delete=delete)
# cmds.delete(loc)
cmds.select(clear=True)
if __name__ == "__main__":
print(load_smart_task("mdl", "maya"))