def ConnectThroughRig(src, attr, root, rigObject): name = src.name() grp = pm.group(name='%s_squash_GRP' % (name), empty=True, parent=src.getParent()) grp.setTranslation(src.getTranslation(space='world'), space='world') for child in src.getChildren(type=pm.Transform): child.setParent(grp) md = pm.MultiplyDivide(name='%s_squashInvert_MD' % (name)) md.input1.set(-1, -1, -1) for i, axis in enumerate(['X', 'Y', 'Z']): src.attr('%s%s' % (attr, axis)) >> md.attr('input2%s' % axis) md.attr('output%s' % axis) >> rigObject['inattrs'][i] md = pm.MultiplyDivide(name='%s_squashScaleOffset_MD' % (name)) rigObject['outattrs'] >> md.input1 md.input2.set(3, 3, 3) md.output >> grp.scale pm.pointConstraint(src, grp, mo=False) pm.orientConstraint(src, grp, mo=False) return {'control': src, 'root': root, 'rig': rigObject['group']}
def displayConnect(self, obj): # Create curve for display, between selected objects # Return [curve, locator1, locator2] if len(obj) == 0: obj = pm.ls(transforms=1, sl=1) if len(obj) < 2: pm.error(u"KH_curve_Connect : 선택된 오브젝트가 없습니다.") pointA = obj[0] pointB = obj[1] curve = pm.curve(p=[(0, 0, 0), (0, 0, 0)], k=[0, 1], d=1) pointCurveConstraint1 = pm.pointCurveConstraint(curve + ".ep[0]", ch=True) pointCurveConstraint2 = pm.pointCurveConstraint(curve + ".ep[1]", ch=True) pointCostraint1 = pm.pointConstraint(pointA, pointCurveConstraint1[0]) pointCostraint2 = pm.pointConstraint(pointB, pointCurveConstraint2[0]) locShape1 = pm.listRelatives(pointCurveConstraint1[0], s=1) locShape2 = pm.listRelatives(pointCurveConstraint2[0], s=1) pm.setAttr(locShape1[0] + ".visibility", 0) pm.setAttr(locShape2[0] + ".visibility", 0) return [ curve, pm.PyNode(pointCurveConstraint1[0]), pm.PyNode(pointCurveConstraint2[0]), pointCostraint1, pointCostraint2 ]
def orient( self, _orientchildless=True, _rotateOrder=None ) : # get the rotation order we're after if( not _rotateOrder ) : _rotateOrder = settings.rotationorder # check we have a child to aim to and decide of aim vectors aimvector = utils.aim_axis_to_vectors( _rotateOrder )[0] upvector = utils.aim_axis_to_vectors( _rotateOrder )[1] aim = aimvector children = self.getChildren() parent = self.getParent() if( not parent ) : parent = self if( len( children ) < 1 ) : if( not _orientchildless ) : utils.wrn( '%s has no children. Skipping orient...' % ( self.name() ) ) return False else : aim = [ a * b for a, b in zip( aimvector, [-1] * 3 ) ] pm.select( None ) # create children average aim locator childrenlocator = pm.spaceLocator() if( len( children ) ) : pm.delete( pm.pointConstraint( children + [ childrenlocator ], mo=False ) ) else : childrenlocator.setTranslation( parent.getTranslation( space='world' ), space='world' ) # create up aim locator and aim self to it uplocator = pm.spaceLocator() pm.delete( pm.pointConstraint( [ parent, self, childrenlocator, uplocator ], mo=False ) ) pm.delete( pm.aimConstraint( [ self, uplocator ], mo=False, wut='object', wuo=parent ) ) uplocator.translateBy( ( 0, 0, 0.5 ) ) # unparent children, aim the joint to the average of it's children, then reparent children for joint in children : joint.setParent( None ) pm.delete( pm.aimConstraint( [ childrenlocator, self ], mo=False, wut='object', wuo=uplocator, upVector=upvector, aim=aim ) ) pm.makeIdentity( self, a=True, r=True ) for joint in children : joint.setParent( self ) # tidy up pm.delete( childrenlocator ) pm.delete( uplocator ) pm.select( self ) return True
def autoOrientXKeepZ(self, obj ): assert isinstance(obj, pymel.core.nodetypes.Transform) unrepar = Unreparenter( obj ) pm.makeIdentity( obj, apply=True, t=1, r=1, s=1, n=0, pn=1 ) target = pm.createNode('joint') cons = [ pm.pointConstraint( obj, target ), pm.orientConstraint( obj, target ), pm.scaleConstraint( obj, target ) ] pm.delete( cons ) unrepar.reparent() pm.joint( obj, edit=True, oj='xzy', secondaryAxisOrient='yup', zeroScaleOrient=True, children=False ) unrepar.unparent( ) pm.makeIdentity( obj, apply=True, t=1, r=1, s=1, n=0, pn=1 ) self.rotateOnXToMatchZ(obj,target) pm.delete( target ) pm.makeIdentity( obj, apply=True, t=1, r=1, s=1, n=0, pn=1 ) unrepar.reparent(clean=True)
def constrainSlavesToSelected(self): oSel = pm.ls(sl=True) objs = oSel[:] for ctrl in objs: try: posSlaveAttr = ctrl.posSlave connectedAttrOfPosSlave = posSlaveAttr.inputs()[0] posSlave = connectedAttrOfPosSlave.node() pm.pointConstraint( ctrl, posSlave ) except: print( traceback.format_exc() ) try: rotSlaveAttr = ctrl.rotSlave connectedAttrOfRotSlave = rotSlaveAttr.inputs()[0] rotSlave = connectedAttrOfRotSlave.node() pm.orientConstraint( ctrl, rotSlave ) except: print( traceback.format_exc() )
def CreateScaleRig(name): outgrp = pm.group(name='%s_squashRig_GRP' % (name), empty=True) outloc = pm.spaceLocator(name='%s_squashOut_LOC' % (name)) outloc.setParent(outgrp) aims = [] inattrs = [] pm.cycleCheck(e=False) for axis in ['X', 'Y', 'Z']: loc = pm.spaceLocator(name='%s_squashIn%s_LOC' % (name, axis)) grp = pm.group(name='%s_squashLoc%s_GRP' % (name, axis), empty=True) loc.setParent(grp) grp.setAttr('translate%s' % (axis), 1) pm.pointConstraint(loc, outloc, mo=False) aims.append(pm.aimConstraint(outloc, grp)) grp.setParent(outgrp) inattrs.append(loc.translateX) for aim in aims: pm.delete(aim) pm.cycleCheck(e=True) return {'group': outgrp, 'inattrs': inattrs, 'outattrs': outloc.translate}
def delCon(**kwargs): """ Delete selected constraints on selected objects if exists """ print "deleteCon" sel = pm.ls(selection=True) #con_typ = kwargs.get("constraint_type", None) prFlg = kwargs.get("pr_con", False) scFlg = kwargs.get("sc_con", False) ptFlg = kwargs.get("pt_con", False) orFlg = kwargs.get("or_con", False) for obj in sel: if prFlg: const = pm.parentConstraint(obj, query=True) if const: pm.delete(const) else: print str(obj) + " parent delete skipped" if orFlg: const = pm.orientConstraint(obj, query=True) if const: pm.delete(const) else: print str(obj) + " orient delete skipped" if ptFlg: const = pm.pointConstraint(obj, query=True) if const: pm.delete(const) else: print str(obj) + " point delete skipped" #if con_typ == "aim": # const = pm.aimConstraint(obj, query=True) # if const: # pm.delete(const) if scFlg: const = pm.scaleConstraint(obj, query=True) if const: pm.delete(const) else: print str(obj) + " scale delete skipped" return None
def createTwistJointToSelectedChild(): objs = pm.ls(selection=True) ## Make an empty list to store joints in newJoints = [] for obj in objs: pm.select( clear=True ) child = obj parentJnt = obj.getParent() jnt = pm.joint(position=[0,0,0]) pc = pm.pointConstraint( [parentJnt, child] , jnt ) oc = pm.orientConstraint( parentJnt, jnt ) pm.delete( pc ) pm.delete( oc ) pm.parent( jnt, parentJnt ) ## Put out new joint in the list! newJoints.append( jnt )
def fixJointComplexXforms( self, obj ): assert isinstance(obj, pymel.core.nodetypes.Transform) unrepar = Unreparenter( obj ) pm.makeIdentity( obj, apply=True, t=1, r=1, s=1, n=0, pn=1 ) tmp = pm.createNode('joint') cons = [ pm.pointConstraint( obj, tmp ), pm.orientConstraint( obj, tmp ), pm.scaleConstraint( obj, tmp ) ] pm.delete( cons ) helperZ = pm.createNode('transform') helperX = pm.createNode('transform') pm.parent( helperX, tmp ) pm.parent( helperZ, tmp ) helperX.translate.set( [1,0,0] ) helperZ.translate.set( [0,0,1] ) obj.jointOrient.set( 0,0,0 ) obj.rotateAxis.set( 0,0,0 ) con = pm.aimConstraint( helperX, obj, worldUpType='object', worldUpObject=helperZ, aimVector=[1,0,0], upVector=[0,0,1], ) pm.delete( con ) pm.delete( [helperX, helperZ] ) pm.delete( tmp ) pm.makeIdentity( obj, apply=True, t=1, r=1, s=1, n=0, pn=1 ) unrepar.reparent( )
def makeAnimCtrlAndZero(self, doConnectPosSlave=True, doConnectRotSlave=True): ctrls = [] objs = pm.ls(selection=True) for obj in objs: ## really, you have to find the first occurance of the numbered name ## that didn't exist in the scene as either a zero or a control goodNumber = None iter = 0 while goodNumber == None and iter < 99: iter+=1 if iter==0: foundZ = pm.ls( obj.name() + "_zero" ) foundC = pm.ls( obj.name() + "_ctrl" ) if len(foundZ)==0 and len( foundC )==0: goodNumber = 0 else: foundZ = pm.ls( obj.name() + str(iter) + "_zero" ) ## could use .zfill(2) foundC = pm.ls( obj.name() + str(iter) + "_ctrl" ) if len(foundZ)==0 and len( foundC )==0: goodNumber = iter if goodNumber == 0: basename = obj.name() else: basename = obj.name() + str(goodNumber) try: jointRadius=obj.radius.get() except: print( traceback.format_exc() ) jointRadius=16 radiusToUse = self.ctrlSizeFloatField.getValue() if radiusToUse <= 0.0: radiusToUse = 8*jointRadius ctrlList = pm.circle(normal=[1,0,0], radius=radiusToUse, ch=False) #ctrlCircle = ctrlList[1] ctrl = ctrlList[0] pm.rename( ctrl, basename + "_ctrl" ) pCon = pm.pointConstraint( obj, ctrl ) oCon = pm.orientConstraint( obj, ctrl ) ## beware, pymel returns constraints back directly, not in list pm.delete( [pCon, oCon] ) ## delte constraints zeroList = pm.duplicate( ctrl ) zero = zeroList[0] pm.rename( zero, basename + "_zero") pm.delete( zero.getShape() ) pm.parent( ctrl, zero ) #pCon = pm.pointConstraint( ctrl, obj ) #oCon = pm.orientConstraint( ctrl, obj ) pm.addAttr( ctrl, ln='posSlave', at='message' ) pm.addAttr( ctrl, ln='rotSlave', at='message' ) if doConnectPosSlave==True: obj.message >> ctrl.posSlave if doConnectRotSlave==True: obj.message >> ctrl.rotSlave ctrls.append( ctrl ) pm.select( ctrls )
import pymel.all as pm objs = pm.ls(selection=True) ## Make an empty list to store joints in newJoints = [] for obj in objs: pm.select( clear=True ) child = obj parentJnt = obj.getParent() jnt = pm.joint(position=[0,0,0]) pc = pm.pointConstraint( [parentJnt, child] , jnt ) oc = pm.orientConstraint( parentJnt, jnt ) pm.delete( pc ) pm.delete( oc ) pm.parent( jnt, parentJnt ) ## Put out new joint in the list! newJoints.append( jnt ) pm.select( newJoints )
import pymel.all as pm objs = pm.ls(selection=True) ## Make an empty list to store joints in newJoints = [] for obj in objs: pm.select( clear=True ) jnt = pm.joint(position=[0,0,0]) children = obj.getChildren() child = children[0] pc = pm.pointConstraint( [obj, child] , jnt ) oc = pm.orientConstraint( obj, jnt ) pm.delete( pc ) pm.delete( oc ) pm.parent( jnt, obj ) ## Put out new joint in the list! newJoints.append( jnt ) pm.select( newJoints )
def create( self, _jointchain=None ) : super( IkRig, self ).create( _jointchain ) jointchains = self.tree_children( 'jointchain' ) if( len( jointchains ) != 1 ) : utils.err( 'IkRig children includes more than ONE jointchain. Not sure which to use. Skipping...' ) return False jointchain = jointchains[0] # create a simple joint chain simplejointchain = jointchain.duplicate_jointchain( self.PARTNAME, 'driver', _simple=True ) self.add_child( simplejointchain ) pm.PyNode( 'leftUpperArm_1_IKJ_DRIVER' ).hide() pm.PyNode( 'leftUpperArm_1_j' ).hide() for i in range( len( simplejointchain.rigjoints ) - 1 ) : # create a curve between each pair of simple rigjoints rigjoint1 = simplejointchain.rigjoints[i] rigjoint2 = simplejointchain.rigjoints[i+1] v1 = pm.datatypes.Vector( rigjoint1.getTranslation( space='world' ) ) v2 = pm.datatypes.Vector( rigjoint2.getTranslation( space='world' ) ) curvelength = float( v1.distanceTo( v2 ) ) dirvector = [ a * b for a, b in zip( ( curvelength, curvelength, curvelength ), utils.aim_axis_to_vectors( settings.rotationorder )[0] ) ] curve = pm.curve( degree=1, point=[ ( 0, 0, 0 ), dirvector # v1, v2 ], name=utils.name_from_tags( rigjoint1, 'curve' ) ) # rebuild with numspan 2 and 3 degree pm.rebuildCurve( curve, degree=3, spans=2 ) # move vtx[1] and vtx[-2] to respective ends of curve curve.cv[1].setPosition( curve.cv[0].getPosition( space='world' ), space='world' ) curve.cv[-2].setPosition( curve.cv[-1].getPosition( space='world' ), space='world' ) ribbonlength = 0.2 ribbon = pm.extrude( curve, polygon=0, useProfileNormal=1, extrudeType=0, length=ribbonlength, ch=False, name=utils.name_from_tags( rigjoint1, 'nurbs' ) )[0] ribbon.setTranslation( ( 0, 0, -(ribbonlength)/2 ) ) ribbon.setPivots( ( 0, 0, 0 ), worldSpace=True ) pm.makeIdentity( ribbon, apply=True ) pm.delete( ribbon, ch=True ) pm.delete( curve ) utils.create_zero_sdk_groups( ribbon, _replacelast=False ) startcluster = pm.cluster( ribbon.cv[0:1][0:1], name=utils.name_from_tags( rigjoint1, 'start', 'cluster') )[1] midcluster = pm.cluster( ribbon.cv[2][0:1], name=utils.name_from_tags( rigjoint1, 'mid', 'cluster' ) )[1] endcluster = pm.cluster( ribbon.cv[-2:][0:1], name=utils.name_from_tags( rigjoint1, 'end', 'cluster' ) )[1] # parent clusters to respective rigjoints pm.parentConstraint( [ rigjoint1, startcluster ], mo=False ) pm.parentConstraint( [ rigjoint2, endcluster ], mo=False ) # group then point/parent constrain middle cluster to end clusters sdkgroup, zerogroup = utils.create_zero_sdk_groups( midcluster, _replacelast=False ) zerogroup.setRotation( rigjoint1.getRotation( space='world' ), space='world' ) pm.pointConstraint( [ rigjoint1, rigjoint2, zerogroup ], mo=False ) pm.orientConstraint( [ rigjoint1, zerogroup ], mo=False ) jointsToAttachToCurve = [ jointchain.rigjoints[i] ] jointsToAttachToCurve += jointchain.minorrigjoints[ jointsToAttachToCurve[0] ] jointsToAttachToCurve += [ jointchain.rigjoints[i+1] ] for rigjoint in jointsToAttachToCurve : p = rigjoint.getTranslation( space='world' ) posi = pm.nodetypes.ClosestPointOnSurface() ribbon.worldSpace >> posi.inputSurface posi.inPosition.set( p ) u = min( max( posi.u.get(), 0.001 ), 0.999 ) v = min( max( posi.v.get(), 0.001 ), 0.999 ) pm.delete( posi ) follicleshape = pm.nodetypes.Follicle() ribbon.local >> follicleshape.inputSurface ribbon.worldMatrix[0] >> follicleshape.inputWorldMatrix follicleshape.parameterU.set( u ) follicleshape.parameterV.set( v ) follicle = follicleshape.getParent() follicle.rename( utils.name_from_tags( rigjoint, 'follicle' ) ) follicleshape.rename( follicle.name() + 'Shape' ) follicleshape.outRotate >> follicle.rotate follicleshape.outTranslate >> follicle.translate # remove any constraints already on the joint pm.delete( rigjoint.getChildren( type='constraint' ) ) pm.parentConstraint( [ follicle, rigjoint ], mo=True ) return True
import pymel.all as pm objs = pm.ls(selection=True) ## Make an empty list to store joints in newJoints = [] for obj in objs: pm.select(clear=True) child = obj parentJnt = obj.getParent() jnt = pm.joint(position=[0, 0, 0]) pc = pm.pointConstraint([parentJnt, child], jnt) oc = pm.orientConstraint(parentJnt, jnt) pm.delete(pc) pm.delete(oc) pm.parent(jnt, parentJnt) ## Put out new joint in the list! newJoints.append(jnt) pm.select(newJoints)
import pymel.all as pm objs = pm.ls(selection=True) ## Make an empty list to store joints in newJoints = [] for obj in objs: pm.select(clear=True) jnt = pm.joint(position=[0, 0, 0]) children = obj.getChildren() child = children[0] pc = pm.pointConstraint([obj, child], jnt) oc = pm.orientConstraint(obj, jnt) pm.delete(pc) pm.delete(oc) pm.parent(jnt, obj) ## Put out new joint in the list! newJoints.append(jnt) pm.select(newJoints)