Skip to content

Instantly share code, notes, and snippets.

@patwooky
Last active August 14, 2017 11:18
Show Gist options
  • Save patwooky/0802e8598e47940a2fbd6971690bc371 to your computer and use it in GitHub Desktop.
Save patwooky/0802e8598e47940a2fbd6971690bc371 to your computer and use it in GitHub Desktop.
maya - Transfers ownership of elements in a set from one set to another
import maya.cmds as mc
from pymel.core import *
def setAssignmentTransfer(shaderCurr, shaderNew, module='pymel'):
'''
transfer ownership of elements in a set from one set to another
works on shaders and regular sets
shaderCurr - <objectSet> the current set that holds the collection of objects to be transferred over
shaderNew - <objectSet> the target set which to transfer the objects under
module - <str> can be 'pymel' or 'cmds'
'''
def setAssignmentTransferCmds():
sTime = mc.timerX()
print 'shaderAssignmentTransfer start'
sTimer = mc.timerX()
# currObjsList = mc.ls(mc.sets(shaderCurr, q=True), flatten=False)
# print currObjsList
mc.sets(mc.sets(shaderCurr, q=True), e=True, forceElement=shaderNew)
print 'time taken {}\n\n'.format(mc.timerX(startTime=sTimer))
def setAssignmentTransferPymel():
sTime = timerX()
print 'shaderAssignmentTransfer start'
sTimer = timerX()
# currObjsList = ls(sets(shaderCurr, q=True), flatten=False)
# print currObjsList
sets(shaderNew, e=True, forceElement=sets(shaderCurr, q=True))
print 'time taken {}\n\n'.format(timerX(startTime=sTimer))
if not len(mc.ls([shaderCurr, shaderNew], type='objectSet'))==2:
print 'Selection must be sets: Select current incoming set, then outgoing sets to transfer members to, then run the script again!'
return
if 'cmds' in module.lower():
setAssignmentTransferCmds()
else:
setAssignmentTransferPymel()
return
# run the script using pymel commands
# setAssignmentTransfer(mc.ls(sl=True)[0], mc.ls(sl=True)[1], module='pymel') # 0.55 seconds
# run the script using maya.cmds
setAssignmentTransfer(mc.ls(sl=True)[0], mc.ls(sl=True)[1], module='cmds') # 0.25 seconds
# cmds is almost twice as fast as pymel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment