Skip to content

Instantly share code, notes, and snippets.

@danvas
Created October 13, 2011 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danvas/1283512 to your computer and use it in GitHub Desktop.
Save danvas/1283512 to your computer and use it in GitHub Desktop.
PyMel function that copies the value(s) of the attribute from sourceObj to targetObj. Handles multi-attributes.
import pymel.core as pm
sourceObj = pm.ls('aFluidShape')[0]
targetObj = pm.ls('myFluidShape')[0]
def hlCopyAttr(sourceObj, targetObj, attribute):
"""Copies the value(s) of the attribute from sourceObj to targetObj. Handles multi-attributes."""
if pm.attributeQuery(attribute, node = targetObj.name(), exists=True): # Execute only if the entered attribute exists
multiAttrCheck = pm.attributeQuery(attribute, node=targetObj.name(), m=1) # Check if it's a multi-attribute or not
if multiAttrCheck: # If the attribute is a multi-attribute, copy the values from each instance
extraInst = set(sourceObj.getAttr(attribute, mi=1)).symmetric_difference( set(targetObj.getAttr(attribute, mi=1)) ) # Get the set of extra instances in multi-attribute
for ins in extraInst: # Remove these extra instances
pm.removeMultiInstance('{0}.{1}[{2}]'.format(targetObj.name(), attribute, ins) )
multiAttr = pm.attributeQuery(attribute, node=targetObj.name(), listChildren=1) # Get list of multi-attribute children
for attrChild in multiAttr:
for ind in sourceObj.getAttr(attribute, mi=1):
attrString = '{attr}[{index}].{child}'.format( attr = attribute,
index = ind,
child = attrChild )
attrVal = sourceObj.getAttr( attrString )
targetObj.setAttr( attrString, attrVal )
print( '// {objName}.{attr} was set to {val} //'.format(objName = targetObj.name(),
attr = attrString,
val = attrVal) )
else: # If not a multi-attribute, copy individual value
attrVal = sourceObj.getAttr(attribute)
targetObj.setAttr(attribute, attrVal)
print('// {0}.{1} was set to {2} //\n'.format(targetObj.name(), attribute, attrVal) )
else:
print('!! {0} does not exist !!\n'.format(attribute))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment