Skip to content

Instantly share code, notes, and snippets.

@Onefabis
Last active June 29, 2018 15:08
Show Gist options
  • Save Onefabis/d604c2aca27fbcbeb16408c2bd388efe to your computer and use it in GitHub Desktop.
Save Onefabis/d604c2aca27fbcbeb16408c2bd388efe to your computer and use it in GitHub Desktop.
Fast parent creator for rig controllers
import maya.cmds as mc
'''
Creates new attribute 'Parent_To', name of attribute will be assigned
from exact names of selected Parents
parentSet( 1 ) - means that you need to select parents first and then only the child.
Script will create the rest setup for you
parentSet( 0 ) - means that you need to select parents, then the child parent node
(group that will be assigned by constraint and will be triggered between parents) and
lastly the child node
parentSet(self=False, mode='Parent' ) - by default and means that the child will be
under parentConstraint, in other cases there will be orientConstraint
'''
def parentSet(self=False, mode='Parent' ):
selObjs = mc.ls(sl=1)
if self:
childCnt = selObjs.pop(-1)
parentCnt = childCnt
else:
parentCnt = selObjs.pop(-1)
childCnt = selObjs.pop(-1)
parNode = mc.listRelatives( childCnt, p=1 )
attr = ''
for n in xrange(len(selObjs)):
attr += selObjs[n] + '=' + str(n) + ':'
if not mc.attributeQuery( 'Parent_To', n=parentCnt, ex=1 ):
mc.addAttr( parentCnt, longName='Parent_To', k=1, en=attr, at='enum' )
else:
mc.addAttr( parentCnt+'.Parent_To', e=1, en=attr, at='enum' )
chPos = mc.xform( childCnt, q=1, ws=1, rp=1 )
chGrp = mc.group( em=1, n=childCnt+'_grp' )
mc.xform( chGrp, ws=1, rp=chPos )
mc.parent( childCnt, chGrp )
if parNode:
mc.parent( chGrp, parNode )
attachToParents( selObjs, parentCnt, chPos, mode, chGrp )
# Creates in-place parent groups that has the same position as the child group, i.e. child controller
# will not pop after the parent switching
def attachToParents( parents, parentCnt, chPos, mode, chGrp ):
for s in xrange( len(parents) ):
i = 0
while mc.objExists( parents[s] + '_grp' + str(i) ):
i += 1
grp = mc.group( em=1, n=parents[s]+'_grp' + str(i) )
mc.xform( grp, ws=1, rp=chPos )
mc.parent( grp, parents[s] )
cond = mc.createNode('condition', n=parents[s]+'cond')
mc.connectAttr(parentCnt+'.Parent_To', cond+'.firstTerm' )
mc.setAttr( cond+'.secondTerm', s )
mc.setAttr( cond+'.operation', 0 )
mc.setAttr( cond+'.colorIfTrueR', 1 )
mc.setAttr( cond+'.colorIfFalseR', 0 )
if mode=='Parent':
par = mc.parentConstraint( grp, chGrp, mo=1 )
else:
par = mc.orientConstraint( grp, chGrp, mo=1 )
attrs = mc.listAttr( par[0],ud=1 )
mc.connectAttr(cond+'.outColor.outColorR', par[0]+'.'+attrs[s] )
# Rename the new 'Parent_To' attribute with nice names
def renameAttrs( name ):
cnt = mc.ls( sl=1 )
for cAttr in mc.listAttr( cnt[0],ud=1 ):
if 'Parent_To' in cAttr:
mc.addAttr( '.Parent_To', e=1, enumName=':'.join(name) )
parentSet( 0 )
#renameAttrs( ['Right_Hand','Left_Hand','Root'] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment