Skip to content

Instantly share code, notes, and snippets.

@patwooky
Created June 7, 2019 17:36
Show Gist options
  • Save patwooky/4a7498fd9d6a78a1d50f3fd33ef6f71b to your computer and use it in GitHub Desktop.
Save patwooky/4a7498fd9d6a78a1d50f3fd33ef6f71b to your computer and use it in GitHub Desktop.
This Maya script takes a source object and creates duplicates or instances, and for each target, move the duplicated/instanced source to match position, orientation and scale.
from pymel.core import *
'''
written by Patrick Woo (patrickwoo@yahoo.com)
This Maya script takes a source object and creates duplicates or instances, and for each target,
move the duplicated/instanced source to match position, orientation and scale.
'''
def dupObjs(inList, dupMode):
'''
inList - <list> a list of transforms. last item is the source, and all other items are targets that the source will get duplicated to
dupMode - <str> can be 'duplicate' or 'instance'to create duplicates or instances respectively
'''
errStrInsufficnetInput = '\nselect all targets transforms, then source object transform last, then run the script\n'
if len(inList) < 2:
print(errStrInsufficnetInput)
return
# selTargetList - <list> a list of transform nodes
# selSource - <transform node> the source transform to duplicate to each target node
selSource, selTargetList = ([PyNode(x) for x in ls(inList[-1], type='transform')],
[PyNode(x) for x in ls(inList[:-1], type='transform')])
if selSource and selTargetList:
print('good to go')
else:
print(errStrInsufficnetInput)
return
newObjsList = []
newGrp = group(empty=True, name='dupObjs_grp')
for tgtObj in selTargetList:
print('tgtObj is {}'.format(tgtObj))
if 'dup' in dupMode.lower():
newObj = duplicate(selSource)[0] # duplicateion returns a list
else:
newObj = instance(selSource)[0] # instancing returns a list
parent(newObj, newGrp)
newObj.rename(tgtObj.shortName())
newObjsList.append(newObj)
pc = parentConstraint(tgtObj, newObj, maintainOffset=False)
delete(pc)
sc = scaleConstraint(tgtObj, newObj, maintainOffset=False)
delete(sc)
# print ('newObjsList is {}'.format(newObjsList))
print('\nDuplication operation completed.\n')
return
dupObjs(ls(sl=True), 'instance')
@Abendstern51
Copy link

Man I love you. I've been trying to find it for so long.

@patwooky
Copy link
Author

patwooky commented Mar 8, 2023

I am glad it's able to help you, Abendstern! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment