def fixXforms(self): ## Get the first selected object objs = pm.ls(selection=True) for obj in objs: pm.parent( obj, world=True ) for obj in objs: ## Unparent the object ## Move the pivot to the origin pm.move ( obj.scalePivot , [0,0,0] ) pm.move ( obj.rotatePivot , [0,0,0] ) ## Freeze transforms pm.makeIdentity( obj, apply = True, normal = 0, preserveNormals = True ) ## Note that the options for makeIdentity were simply ## from the Mel script history or freeze transform ## Delete the history pm.delete (ch = True )
def main(): sel = pm.ls(os=True, fl=True) if len(sel) != 3: pm.warning("Select 3 vertices in order of origin, x, and y") return shape = pm.listRelatives(sel[0], parent=True)[0] transformNode = pm.listRelatives(shape, parent=True)[0] piv = pm.xform(transformNode, q=True, ws=True, rp=True) p0 = sel[0].getPosition() p1 = sel[1].getPosition() p2 = sel[2].getPosition() X = p1 - p0 # X-axis Y = p2 - p0 # Y-axis Z = X ^ Y # Z-axis P = pm.datatypes.Point(piv[0], piv[1], piv[2]) X.normalize() Y.normalize() Z.normalize() M = pm.datatypes.Matrix( X.x, X.y, X.z, 0, Y.x, Y.y, Y.z, 0, Z.x, Z.y, Z.z, 0, P.x, P.y, P.z, 1) pm.xform(transformNode, matrix=M.inverse()) pm.select(transformNode, r=True) pm.makeIdentity(apply=True, t=True, r=True, s=False, n=False) pm.xform(transformNode, ws=True, piv=(0, 0, 0)) pm.xform(transformNode, matrix=M)
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 scale_uniform(self, **kwargs): ctr = kwargs.get("ctr", None) size_offset = kwargs.get("size_offset", 0.5) cur_scl = ctr.getScale() new_scl = [ cur_scl[0] + size_offset, cur_scl[1] + size_offset, cur_scl[2] + size_offset ] pm.scale(ctr, new_scl, objectCenterPivot=True, worldSpace=True) pm.makeIdentity(ctr, apply=True, scale=True) return None
def CopyJntOri(**kwargs): """ Copy orientation of one joint to another """ sel = pm.ls(selection=True) sel_jnts = kwargs.get("joint_selection", sel) print sel_jnts ref = sel_jnts.pop(0) for jnt in sel_jnts: ori_cnst = pm.orientConstraint(ref, jnt, maintainOffset=False) pm.delete(ori_cnst) pm.makeIdentity(apply=True, rotate=True) return None
def pivotToZeroDeleteHistoryAndFreezeTransformsInWorldSpace(): original_selection = pm.ls(selection = True) objs = original_selection[:] for obj in objs: try: previous_parent = obj.getParent() pm.parent( obj, world=True ) pm.move ( obj.scalePivot , [0,0,0] ) pm.move ( obj.rotatePivot , [0,0,0] ) pm.makeIdentity (obj, apply = True, normal = 0, preserveNormals = True ) pm.delete (ch = True ) pm.parent( obj, previous_parent ) except: print( traceback.format_exc() )
def pivotToZeroDeleteHistoryAndFreezeTransformsInWorldSpace(): original_selection = pm.ls(selection=True) objs = original_selection[:] for obj in objs: try: previous_parent = obj.getParent() pm.parent(obj, world=True) pm.move(obj.scalePivot, [0, 0, 0]) pm.move(obj.rotatePivot, [0, 0, 0]) pm.makeIdentity(obj, apply=True, normal=0, preserveNormals=True) pm.delete(ch=True) pm.parent(obj, previous_parent) except: print(traceback.format_exc())
def copy_orientation(): sel = pm.ls(selection=True) prnt = sel.pop(0) for obj in sel: tmp_con = pm.orientConstraint(prnt, obj, maintainOffset=False, name="tmp") pm.delete(tmp_con) pm.makeIdentity(apply=True, translate=True, rotate=True, scale=True, normal=False, preserveNormals=True) return None
def go(self): ## Get a list of the selected objects in maya sel = pm.ls(selection=True) ## in mel: ls -sl ## Get the last object selected by ## popping it out of the list target = sel.pop(-1) for obj in sel: obj.rename( 'UCX_' + target.name() + '_00' ) ## rename based on the target's name pm.parent( obj, target ) ## parent this object to the target ## Freeze transforms pm.makeIdentity( target, apply=True ) ## Select target and all its children pm.select( target, hierarchy=True ) ## Delete history pm.delete( constructionHistory=True )
def match_ctrl(jnt, ctrl): control_name = ctrl.name(long=None) control_shape = ctrl.getShape() transform = pm.group(parent=jnt, empty=True) transform.zeroTransformPivots() pm.parent(transform, world=True) pm.parent(control_shape, transform, absolute=True, shape=True) new_trans = pm.listRelatives(control_shape, parent=True) pm.makeIdentity(new_trans, s=True, r=True, t=True, apply=True) pm.parent(control_shape, transform, relative=True, shape=True) pm.delete(new_trans, ctrl) transform.rename(control_name) pm.delete(transform, constructionHistory=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 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 scale_ctr_to_obj(self, **kwargs): obj = kwargs.get("cur_obj", None) ctr = kwargs.get("ctr", None) size_offset = kwargs.get("size_offset", 0.5) dup_obj = None attr_ip_chk = self.check_attr_input(chk_obj=obj) if attr_ip_chk: dup_obj = pm.duplicate(obj)[0] ch_nd = dup_obj.getChildren() sh_nd = dup_obj.getShape() for nd in ch_nd: if not nd == sh_nd: pm.delete(nd) obj = dup_obj obj_bnd_bx = obj.getBoundingBox() obj_x_y_z_param = self.get_bound_parameters(bound=obj_bnd_bx) if obj_x_y_z_param == self.FAIL: pm.displayError("Bound parameters not obtained") return None ctr_bnd_bx = ctr.getBoundingBox() ctr_x_y_z_param = self.get_bound_parameters(bound=ctr_bnd_bx) if ctr_x_y_z_param == self.FAIL: pm.displayError("Bound parameters not obtained") return None scale_val = [1, 1, 1] for index in range(len(ctr_x_y_z_param)): if round(float(ctr_x_y_z_param[index]), 2) > 0: scale_val[index] = float(obj_x_y_z_param[index] / ctr_x_y_z_param[index]) + size_offset else: scale_val[index] = 1 if dup_obj: pm.delete(dup_obj) pm.scale(ctr, scale_val, objectCenterPivot=True, worldSpace=True) pm.makeIdentity(ctr, apply=True, scale=True) return None
def freeze_transform(self): pymel.makeIdentity(self, a=True, t=1, r=1, s=1, n=0, pn=1)
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
def create_ctrl(self): if self.shape == CreateCtrl.triangle: ctrl = pm.curve(name=self.name, d=1, p=[(-1, 0, 1), (1, 0, 1), (0, 0, -1), (-1, 0, 1)], k=[ 0, 1, 2, 3, ]) elif self.shape == CreateCtrl.square: ctrl = pm.curve(name=self.name, degree=1, point=[(0, 2, 2), (0, 2, -2), (0, -2, -2), (0, -2, 2), (0, 2, 2)]) elif self.shape == CreateCtrl.octagon: ctrl = pm.curve(name=self.name, degree=1, point=[(0, 3, 1), (0, 3, -1), (0, 1, -3), (0, -1, -3), (0, -3, -1), (0, -3, 1), (0, -1, 3), (0, 1, 3), (0, 3, 1)]) elif self.shape == CreateCtrl.circle: ctrl = pm.circle(name=self.name, normalX=1, normalZ=0, radius=2, constructionHistory=False)[0] elif self.shape == CreateCtrl.cube: ctrl = pm.curve(name=self.name, degree=1, point=[(1, 1, 1), (1, 1, -1), (-1, 1, -1), (-1, 1, 1), (1, 1, 1), (1, -1, 1), (1, -1, -1), (-1, -1, -1), (-1, -1, 1), (1, -1, 1), (1, -1, -1), (1, 1, -1), (-1, 1, -1), (-1, -1, -1), (-1, -1, 1), (-1, 1, 1)]) elif self.shape == CreateCtrl.pyramid: ctrl = pm.curve(name=self.name, degree=1, point=[(1, 2, 1), (1, 2, -1), (-1, 2, -1), (-1, 2, 1), (1, 2, 1), (0, 0, 0), (1, 2, -1), (-1, 2, -1), (0, 0, 0), (-1, 2, 1)]) elif self.shape == CreateCtrl.cone: ctrl = pm.curve(name=self.name, d=1, p=[(-0.5, 2, 0.866025), (0, 0, 0), (0.5, 2, 0.866025), (-0.5, 2, 0.866025), (-1, 2, -1.5885e-07), (0, 0, 0), (-1, 2, -1.5885e-07), (-0.5, 2, -0.866026), (0, 0, 0), (0.5, 2, -0.866025), (-0.5, 2, -0.866026), (0.5, 2, -0.866025), (0, 0, 0), (1, 2, 0), (0.5, 2, -0.866025), (1, 2, 0), (0.5, 2, 0.866025)], k=[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ]) elif self.shape == CreateCtrl.diamond: ctrl = pm.curve(name=self.name, degree=1, point=[(1, 0, 1), (1, 0, -1), (-1, 0, -1), (-1, 0, 1), (1, 0, 1), (0, -2, 0), (1, 0, -1), (-1, 0, -1), (0, -2, 0), (-1, 0, 1), (1, 0, 1), (0, 2, 0), (1, 0, -1), (-1, 0, -1), (0, 2, 0), (-1, 0, 1)]) elif self.shape == CreateCtrl.one_arrow: ctrl = pm.curve(name=self.name, d=1, p=[(0, 1.003235, 0), (0.668823, 0, 0), (0.334412, 0, 0), (0.334412, -0.167206, 0), (0.334412, -0.501617, 0), (0.334412, -1.003235, 0), (-0.334412, -1.003235, 0), (-0.334412, -0.501617, 0), (-0.334412, -0.167206, 0), (-0.334412, 0, 0), (-0.668823, 0, 0), (0, 1.003235, 0)], k=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) elif self.shape == CreateCtrl.two_arrow: ctrl = pm.curve(name=self.name, d=1, p=[ (0, 1, 0), (1, 1, 0), (2, 1, 0), (3, 1, 0), (3, 2, 0), (4, 1, 0), (5, 0, 0), (4, -1, 0), (3, -2, 0), (3, -1, 0), (2, -1, 0), (1, -1, 0), (0, -1, 0), (-1, -1, 0), (-2, -1, 0), (-3, -1, 0), (-3, -2, 0), (-4, -1, 0), (-5, 0, 0), (-4, 1, 0), (-3, 2, 0), (-3, 1, 0), (-2, 1, 0), (-1, 1, 0), (0, 1, 0), ], k=[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 ]) elif self.shape == CreateCtrl.two_arrow_curved: ctrl = pm.curve(name=self.name, d=1, p=[(-0.251045, 0, -1.015808), (-0.761834, 0, -0.979696), (-0.486547, 0, -0.930468), (-0.570736, 0, -0.886448), (-0.72786, 0, -0.774834), (-0.909301, 0, -0.550655), (-1.023899, 0, -0.285854), (-1.063053, 0, 9.80765e-009), (-1.023899, 0, 0.285854), (-0.909301, 0, 0.550655), (-0.72786, 0, 0.774834), (-0.570736, 0, 0.886448), (-0.486547, 0, 0.930468), (-0.761834, 0, 0.979696), (-0.251045, 0, 1.015808), (-0.498915, 0, 0.567734), (-0.440202, 0, 0.841857), (-0.516355, 0, 0.802034), (-0.658578, 0, 0.701014), (-0.822676, 0, 0.498232), (-0.926399, 0, 0.258619), (-0.961797, 0, 8.87346e-009), (-0.926399, 0, -0.258619), (-0.822676, 0, -0.498232), (-0.658578, 0, -0.701014), (-0.516355, 0, -0.802034), (-0.440202, 0, -0.841857), (-0.498915, 0, -0.567734), (-0.251045, 0, -1.015808)], k=[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28 ]) elif self.shape == CreateCtrl.four_arrow: ctrl = pm.curve(name=self.name, d=1, p=[ (1, 0, 1), (3, 0, 1), (3, 0, 2), (5, 0, 0), (3, 0, -2), (3, 0, -1), (1, 0, -1), (1, 0, -3), (2, 0, -3), (0, 0, -5), (-2, 0, -3), (-1, 0, -3), (-1, 0, -1), (-3, 0, -1), (-3, 0, -2), (-5, 0, 0), (-3, 0, 2), (-3, 0, 1), (-1, 0, 1), (-1, 0, 3), (-2, 0, 3), (0, 0, 5), (2, 0, 3), (1, 0, 3), (1, 0, 1), ], k=[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 ]) elif self.shape == CreateCtrl.ring: ctrl = pm.curve(name=self.name, d=1, p=[(-0.707107, 0.0916408, 0.707107), (0, 0.0916408, 1), (0, -0.0916408, 1), (-0.707107, -0.0916408, 0.707107), (-0.707107, 0.0916408, 0.707107), (-1, 0.0916408, 0), (-1, -0.0916408, 0), (-0.707107, -0.0916408, 0.707107), (-1, -0.0916408, 0), (-0.707107, -0.0916408, -0.707107), (-0.707107, 0.0916408, -0.707107), (-1, 0.0916408, 0), (-0.707107, 0.0916408, -0.707107), (0, 0.0916408, -1), (0, -0.0916408, -1), (-0.707107, -0.0916408, -0.707107), (-0.707107, 0.0916408, -0.707107), (-0.707107, -0.0916408, -0.707107), (0, -0.0916408, -1), (0.707107, -0.0916408, -0.707107), (0.707107, 0.0916408, -0.707107), (0, 0.0916408, -1), (0.707107, 0.0916408, -0.707107), (1, 0.0916408, 0), (1, -0.0916408, 0), (0.707107, -0.0916408, -0.707107), (1, -0.0916408, 0), (0.707107, -0.0916408, 0.707107), (0.707107, 0.0916408, 0.707107), (1, 0.0916408, 0), (0.707107, 0.0916408, 0.707107), (0, 0.0916408, 1), (0, -0.0916408, 1), (0.707107, -0.0916408, 0.707107)], k=[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 ]) elif self.shape == CreateCtrl.sun: ctrl = pm.circle(name=self.name, s=16, nr=[0, 1, 0])[0] pm.select((ctrl + '.cv[1]'), (ctrl + '.cv[3]'), (ctrl + '.cv[5]'), (ctrl + '.cv[7]'), (ctrl + '.cv[9]'), (ctrl + '.cv[11]'), (ctrl + '.cv[13]'), (ctrl + '.cv[15]'), (ctrl + '.cv[17]'), (ctrl + '.cv[19]'), r=True) pm.scale(0.3, 0.3, 0.3, p=[0, 0, 0], r=True) pm.makeIdentity(ctrl, apply=True, t=1, r=1, s=1, n=0) pm.xform(ctrl, cp=True) print(ctrl) self.obj = ctrl return self.obj