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 makeCamPlaneForDrawing(self): drawPlaneGroup = pm.createNode( "transform" ); size = 1024 planeList = pm.polyPlane( w=size,h=size, sx=1,sy=1, n="drawPlane", axis=[0,0,1] ) #print( planeList ) planeXform = planeList[0] planeShape = planeXform.getShape() planeShape.overrideEnabled.set(1) planeShape.overrideShading.set(0) locatorXform = pm.spaceLocator(n="drawPlaneLocator") locatorShape = locatorXform.getShape() locatorShape.localScale.set( [128,128,128] ) camList = pm.camera( name="drawPlaneCam" ) #print( camList ) camXform = camList[0] camXform.tz.set(256) pm.parent( planeXform, locatorXform ) pm.parent( locatorXform, drawPlaneGroup ) pm.parent( camXform, drawPlaneGroup ) pm.orientConstraint( camXform, planeXform, ) ##aimVector=[0,1,0], upVector=[0,0,1] ) ## Look through cam pm.select( camXform ) panel = pm.getPanel( withFocus=True ) pm.mel.eval( "lookThroughSelected 0 " + panel +";") pm.makeLive( planeXform )
def set_poleLocator(**kwargs): """ Select 3 joints parent to child in order to which pole vector has to be placed Select 3 joints and ik handle in order, to create the pole vector object and constraint """ distance_scale = kwargs.get("distance_scale", 1) pole_vector_locator = kwargs.get("pole_vector", "poleLocator") selected_joint = pm.ls(selection=True) if len(selected_joint) < 3: print("please select 3 joints") return None joint1_position = pm.xform(selected_joint[0], query=True, worldSpace=True, translation=True) joint2_position = pm.xform(selected_joint[1], query=True, worldSpace=True, translation=True) joint3_position = pm.xform(selected_joint[2], query=True, worldSpace=True, translation=True) joint1_vector = openmaya.MVector(joint1_position[0], joint1_position[1], joint1_position[2]) joint2_vector = openmaya.MVector(joint2_position[0], joint2_position[1], joint2_position[2]) joint3_vector = openmaya.MVector(joint3_position[0], joint3_position[1], joint3_position[2]) joint1_joint2_vector = joint2_vector - joint1_vector joint1_joint2_vector.normalize() joint2_joint3_vector = joint3_vector - joint2_vector joint2_joint3_vector.normalize() plane_normal = joint1_joint2_vector ^ joint2_joint3_vector plane_normal.normalize() joint1_joint3_vector = joint3_vector - joint1_vector joint1_joint3_vector.normalize pole_vector = joint1_joint3_vector ^ plane_normal mag_vec = pole_vector * distance_scale pole_position = joint2_vector + mag_vec loc_pos = [pole_position.x, pole_position.y, pole_position.z] pole_locator = pm.spaceLocator(name=pole_vector_locator) locator_group = pm.group(pole_locator, name=pole_vector_locator + "_group") pm.xform(locator_group, translation=loc_pos) print("Pole object placed at ", str(loc_pos)) if len(selected_joint) == 4: ik_handle = selected_joint[3] pm.poleVectorConstraint(pole_locator, ik_handle) print("Pole Vector Constraint applied") return None
def build_box(car_name): """Method 1 - This buider make box strucutre (not strong) """ sel = pm.ls(sl=True) group = pm.group(name=car_name + '_group', empty=True) nodes_group = pm.group(name='nodes_group', empty=True, parent=group) beams_group = pm.group(name='beams_group', empty=True, parent=group) if not sel: return trans = sel[0] mesh = trans.getShape() pm.hide(trans) d = {} verts = mesh.verts for index, vtx in enumerate(verts): loc = pm.spaceLocator() loc.rename('b' + str(index)) loc.t.set(vtx.getPosition(space='world')) loc.overrideEnabled.set(True) loc.overrideColor.set(17) loc.setParent(nodes_group) index = vtx.index() d[index] = {'loc': loc, 'connected': []} for c in vtx.connectedVertices(): c_index = c.index() if c_index < index: continue d[index]['connected'].append(c_index) index = 0 for each in d: loc = d[each]['loc'] for other in d[each]['connected']: other_loc = d[other]['loc'] curve = pm.curve(p=[(0, 0, 0), (1, 0, 0)], degree=1) curve.rename('beam_' + str(index)) shape = curve.getShape() curve.overrideEnabled.set(True) curve.overrideColor.set(15) lock_hide(curve) loc.t >> shape.controlPoints[0] other_loc.t >> shape.controlPoints[1] curve.setParent(beams_group) index += 1 pm.select(clear=True)
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 make_node(name, parent, point=None, vtx=None): """Make a Node from a vertex location """ if pm.objExists(name): return loc = pm.spaceLocator() loc.rename(name) if point: loc.t.set([point[0] * 100, point[2] * 100, point[1] * -100]) elif vtx: loc.t.set(vtx.getPosition(space='world')) loc.overrideEnabled.set(True) loc.overrideColor.set(17) loc.setParent(parent)
def makePoleVector(self, constrain=True): iks = pm.ls(selection = True ) originalSelection = iks locators = [] for ik in iks: loc = pm.spaceLocator() pm.rename( loc, 'poleVectorTarget' ) pm.parent( loc, ik ) ik.poleVectorX >> loc.translateX ik.poleVectorY >> loc.translateY ik.poleVectorZ >> loc.translateZ loc.translateX.disconnect() loc.translateY.disconnect() loc.translateZ.disconnect() pm.parent( loc, world=True ) if constrain==True: pm.poleVectorConstraint( loc, ik, weight=1 )
def motion_path(**kwargs): objs = kwargs.get("objs", None) point = int(len(objs) / 2) print objs[point] loc = pm.spaceLocator(name="up_loc") tmp_con = pm.parentConstraint(objs[point], loc, maintainOffset=False) pm.delete(tmp_con) pm.move(loc, 10, moveY=True, objectSpace=True) parts = 1.0 / float((len(objs) - 1)) crv = kwargs.get("path_curve", None) print objs #print crv initU = 0.0 pm.select(clear=True) #for obj in objs: #path_anim = pm.pathAnimation(obj, curve = crv, follow=True, followAxis = "x", upAxis = "y", # worldUpType = "object", worldUpObject = loc, fractionMode=True) #path_anim = pm.pathAnimation(obj, curve = crv, follow=True, followAxis = "x", upAxis = "y", # worldUpType = "vector", worldUpVector = [0,1,0], fractionMode=True) #pm.disconnectAttr(str(path_anim)+".uValue") #pm.setAttr(str(path_anim)+".uValue", initU) #initU += float(parts) return None
def create_locator(*args): """This methos creates Locators at user selected positions function call format : create_locator("String", Integer) User Inputs: args[0] : Object name args[1] : object Id to start with Returns : None """ # If number of parameters mismatch, return with message if len(args) != 2: print "arguments mismatch" return None # Call to method to obtain list of selected position position_list = obtain_selection_position() # If user selected nothing, return with message display if not position_list: print "please make selection" return None # Obtain object name from *args object_name = args[0] # Obtain the object ID from *args as integer object_id = int(args[1]) # create Locators at selected positions and # increment the ID to name the next locator in loop for object_position in position_list: locator_name = object_name + str(object_id) + '_Locator' space_locator = pm.spaceLocator(name=locator_name) pm.xform(space_locator, translation=object_position) object_id = object_id + 1 return None
def treadAtPoints(**kwargs): #get inputs tread_name = kwargs.get("tr_name", "Tread") crv = kwargs.get("path_crv", None) crv = str(pm.duplicate(crv, name=str(tread_name) + "PathCrv")[0]) pm.xform(crv, centerPivots=True) #obtain curve length full_length = pm.arclen(crv) paramVal = [] uVal = [] # get param value on the curve at each position selected (locators) sel_obj = pm.ls(selection=True) for obj in sel_obj: pos = pm.xform(obj, query=True, translation=True, worldSpace=True) param = getuParamVal(pos, crv) paramVal.append(param) crv_shp = pm.listRelatives(crv, shapes=True)[0] # create arc length dimension tool arcLen = pm.arcLengthDimension(crv_shp + ".u[0]") # for each param value obtained set the arc length tool attribute and # store the length of curve at that param value # normalize the curve to obtain the motion path U value at each position for val in paramVal: pm.setAttr(str(arcLen) + ".uParamValue", val) len_at_pos = pm.getAttr(str(arcLen) + ".arcLength") uVal.append(len_at_pos / full_length) pm.delete(arcLen) mthPthLst = [] jntLst = [] # create joints, assign motion path and set U value obtained for u in uVal: pm.select(clear=True) jnt = pm.joint() jntLst.append(jnt) pathanim = pm.pathAnimation(jnt, curve=crv, fractionMode=True, follow=True, followAxis="x", worldUpType="vector", worldUpVector=(0, 1, 0)) mthPthLst.append(pathanim) pm.setAttr(str(pathanim) + ".uValue", u) pm.disconnectAttr(str(pathanim) + ".u") # create up locator at mid point of all joints #loc_pos = midPos(selected_items = jntLst) #loc_pos = pm.xform(crv, query=True, translation=True, worldSpace=True) loc_pos = midPosVec(objects=jntLst) loc = pm.spaceLocator() pm.xform(loc, translation=loc_pos, worldSpace=True) for mtPth in mthPthLst: pm.pathAnimation(mtPth, edit=True, worldUpType="object", worldUpObject=loc) # create control curve, add run and speed attributes control_crv = pm.circle(name=tread_name + "CTRL", normalX=1, normalY=0, normalZ=0) pm.xform(control_crv, translation=loc_pos) pm.select(clear=True) pm.addAttr(control_crv, longName="run", attributeType="float", keyable=True) pm.addAttr(control_crv, longName="speed", attributeType="float", keyable=True, minValue=0.0, defaultValue=0.5) # group the tread setup pm.parent(crv, control_crv) pm.parent(loc, control_crv) pm.select(clear=True) gp = pm.group(name=tread_name + "GP") pm.select(clear=True) jnt_gp = pm.group(jntLst, name=tread_name + "JNTGP") pm.xform(gp, translation=loc_pos) pm.parent(control_crv, gp) pm.parent(jnt_gp, gp) createTreadExpression(mtnPth=mthPthLst, runAttr=str(control_crv[0]) + ".run", speedAttr=str(control_crv[0]) + ".speed", exp_nm=tread_name) return None #treadAtPoints(pathCrv = "nurbsCircle1", tr_name = "testTread")
def __enter__(self): self.tmpObject = pm.spaceLocator( ) self.tmpObjectShape = self.tmpObject.getShape() return tmpObject
def __enter__(self): self.tmpObject = pm.spaceLocator() self.tmpObjectShape = self.tmpObject.getShape() return tmpObject
## We can use ik ## get the ik by name #ik = pm.PyNode('ik_heel') ## or get it by selection selection = pm.ls(selection=True) ik = selection[0] ## get the first things in ## the list of selected objects ## get a locator by name #loc = pm.PyNode('locator1') loc = pm.spaceLocator() ## the locator first get's parented to ## the ik handle, just so it's in the right ## coordsys ## parenting order is (slave, master) ## or (child, parent) pm.parent( loc, ik ) ## now we need to put the locator ## at the one exact "magic" ## place where the pole vector ## is for the ik handle ## it's magic, because it's the only
## pole vector attribute import pymel.all as pm ## We can use ik ## get the ik by name #ik = pm.PyNode('ik_heel') ## or get it by selection selection = pm.ls(selection=True) ik = selection[0] ## get the first things in ## the list of selected objects ## get a locator by name #loc = pm.PyNode('locator1') loc = pm.spaceLocator() ## the locator first get's parented to ## the ik handle, just so it's in the right ## coordsys ## parenting order is (slave, master) ## or (child, parent) pm.parent(loc, ik) ## now we need to put the locator ## at the one exact "magic" ## place where the pole vector ## is for the ik handle ## it's magic, because it's the only ## place that won't "pop" out of the pose ##
def setup_motion_path(self): setup_name = self.get_setup_name() path_name = self.get_path_name() sample_obj = self.get_sample_objects() duplicate_flag = self.get_duplicate_flag() placement_type = self.get_placement_type() division_count = self.get_division_count() if setup_name == self.INVALID_INPUT_FAIL: pm.displayError("Invalid Input Entered for setup name") return None if path_name == self.INVALID_INPUT_FAIL: pm.displayError("Invalid Input Entered for path name") return None if path_name == self.NO_OBJECT_FAIL: pm.displayError("path Curve does not exist") return None if path_name == self.DATA_TYPE_FAIL: pm.displayError("Path can be only Nurb Curves") return None if division_count == self.INVALID_INPUT_FAIL: pm.displayError("Invalid Input Entered for divisions") return None if division_count == self.DATA_TYPE_FAIL: pm.displayError("Divisions can take only integer values") return None if sample_obj == self.NO_OBJECT_FAIL: pm.displayError("Sample Object not found") return None obj_list = [] path_anim_list = [] sel_objs = pm.ls(selection=True) if duplicate_flag: path_name = self.get_duplicate_path(path_crv=path_name) path_name = pm.rename(path_name, setup_name + "_path_CRV") if placement_type == "uniform": obj_list, path_anim_list = self.uniform_distribution( name=setup_name, path=path_name, sample=sample_obj, divisions=division_count) else: if not sel_objs: pm.displayError("No Objects selected") for obj in sel_objs: if not pm.objExists(obj): pm.displayWarning(str(obj), "does not exist") return None obj_list, path_anim_list = self.at_selection( name=setup_name, path=path_name, sample=sample_obj, selection_list=sel_objs) loc_pos = CustomScripts.midPos(selected_items=path_name) loc = pm.spaceLocator(name=setup_name + "_up_loc") pm.xform(loc, translation=loc_pos) control_crv = pm.circle(name=setup_name + "CTRL", normalX=1, normalY=0, normalZ=0) pm.xform(control_crv[0], translation=loc_pos, worldSpace=True) pm.select(clear=True) # add run and speed attributes on parent nurb curve pm.addAttr(control_crv[0], longName="run", attributeType="float", keyable=True) pm.addAttr(control_crv[0], longName="speed", attributeType="float", keyable=True, minValue=0.0, defaultValue=0.5) # edit the existing motion path to assign up locator for mtPth in path_anim_list: pm.pathAnimation(mtPth, edit=True, worldUpType="object", worldUpObject=loc) # parent the setup under the parent nurb curve pm.parent(path_name, control_crv[0]) pm.parent(loc, control_crv[0]) pm.select(clear=True) gp = pm.group(name=setup_name + "GP") pm.xform(gp, translation=loc_pos) pm.select(clear=True) obj_gp = pm.group(obj_list, name=setup_name + "object_GP") pm.parent(control_crv[0], gp) pm.parent(obj_gp, gp) # call to create expression function self.createTreadExpression(mtnPth=path_anim_list, runAttr=str(control_crv[0]) + ".run", speedAttr=str(control_crv[0]) + ".speed", exp_nm=setup_name) return None
def createTread(**kwargs): # get inputs divisions = kwargs.get("no_of_joints", 0) tread_name = kwargs.get("tr_name", "Tread") path_crv = kwargs.get("path_crv", None) # duplicate the existing curve to use for tread creation path_crv = str(pm.duplicate(path_crv, name=str(tread_name) + "PathCrv")[0]) pm.xform(path_crv, centerPivots=True) count = 0 part = float(1) / float(divisions) init = 0 path_anim_list = [] jnt_lst = [] # create joints and place them on curve using motion path at equal distance while count < divisions: pm.select(clear=True) jnt = pm.joint() jnt_lst.append(jnt) pathanim = pm.pathAnimation(jnt, curve=path_crv, fractionMode=True, follow=True, followAxis="x", worldUpType="vector", worldUpVector=(0, 1, 0)) path_anim_list.append(pathanim) pm.setAttr(str(pathanim) + ".uValue", init) pm.disconnectAttr(str(pathanim) + ".u") init += part count += 1 # obtain the midpoint of all joints to create an up locator and position it at midpoint #loc_pos = midPos(selected_items = jnt_lst) #loc_pos = pm.xform(path_crv, query=True, translation=True, worldSpace=True) loc_pos = midPosVec(objects=jnt_lst) loc = pm.spaceLocator(name=tread_name + "_up_loc") pm.xform(loc, translation=loc_pos) # create a nurb circle to act as parent controller control_crv = pm.circle(name=tread_name + "CTRL", normalX=1, normalY=0, normalZ=0) pm.xform(control_crv, translation=loc_pos) pm.select(clear=True) # add unr and speed attributes on parent nurb curve pm.addAttr(control_crv, longName="run", attributeType="float", keyable=True) pm.addAttr(control_crv, longName="speed", attributeType="float", keyable=True, minValue=0.0, defaultValue=0.5) #edit the existing motion path to assign up locator for mtPth in path_anim_list: pm.pathAnimation(mtPth, edit=True, worldUpType="object", worldUpObject=loc) #parent the setup under the parent nurb curve pm.parent(path_crv, control_crv) pm.parent(loc, control_crv) pm.select(clear=True) gp = pm.group(name=tread_name + "GP") pm.select(clear=True) jnt_gp = pm.group(jnt_lst, name=tread_name + "JNTGP") pm.xform(gp, translation=loc_pos) pm.parent(control_crv, gp) pm.parent(jnt_gp, gp) # call to create expression function createTreadExpression(mtnPth=path_anim_list, runAttr=str(control_crv[0]) + ".run", speedAttr=str(control_crv[0]) + ".speed", exp_nm=tread_name) return None