"""Utility methods for shapes.
:created: 05/03/2017
:author: Benoit Gielly <benoit.gielly@gmail.com>
"""
from __future__ import absolute_import
import logging
import bgdev.utils.decorator
from maya import cmds
LOG = logging.getLogger(__name__)
[docs]def get_shapes(node, intermediate=False, exclusive=False):
"""Get the shapes of given node.
Args:
node (str): Node to query its shapes
intermediate (bool): Get intermediate shapes when True.
exclusive (bool): Only return the intermediate shapes if True.
Please note that the intermediate flag must be True as well.
Returns:
list: The shapes found below given node.
"""
# if given node is a list, assume first element
if isinstance(node, list):
node = node[0]
LOG.info("Given node is a list. Using first element.")
# return as list if given node is already a shape
if cmds.objectType(node, isAType="shape"):
return [node]
# query shapes
shapes = (
cmds.listRelatives(
node,
shapes=True,
type="deformableShape",
)
or []
)
# separate shapes orig
orig = []
for each in list(shapes): # duplicated `shapes` object to remove safely
if cmds.ls(each, intermediateObjects=True):
orig.append(each)
shapes.remove(each)
if not intermediate:
return shapes
if exclusive:
return orig
return shapes + orig
[docs]def update_plug(source_shape, target_shape):
"""Update the plug.
Args:
source_shape (str): Name of the source shape.
target_shape (str): Name of the target shape to update.
"""
if cmds.nodeType(target_shape) == "mesh":
src_plug = "{0}.worldMesh".format(source_shape)
tgt_plug = "{0}.inMesh".format(target_shape)
elif cmds.nodeType(target_shape) in ("nurbsCurve", "nurbsSurface"):
src_plug = "{0}.worldSpace".format(source_shape)
tgt_plug = "{0}.create".format(target_shape)
else:
return
intermediate = cmds.getAttr(target_shape + ".intermediateObject")
cmds.setAttr(target_shape + ".intermediateObject", False)
cmds.connectAttr(src_plug, tgt_plug, force=True)
cmds.refresh()
# cmds.dgeval(tgt_plug) ? or target_shape + ".create"
# cmds.dgeval(destination + '.outMesh')
cmds.disconnectAttr(src_plug, tgt_plug)
cmds.setAttr(target_shape + ".intermediateObject", intermediate)
LOG.info("Replaced %s with %s.", target_shape, source_shape)