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 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 )
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 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
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