def ins_jnt(**kwargs): num = kwargs.get("num_jts", 1) sel = pm.ls(selection=True) chld = pm.listRelatives(sel[0], children=True) if not chld[0].type() == "joint": print "end joint reached" return None #position_lst = [] pos1 = pm.xform(sel[0], query=True, worldSpace=True, translation=True) pos2 = pm.xform(chld, query=True, worldSpace=True, translation=True) len_vec1 = OpenMaya.MVector(pos1[0], pos1[1], pos1[2]) len_vec2 = OpenMaya.MVector(pos2[0], pos2[1], pos2[2]) len_vec = len_vec2 - len_vec1 print len_vec.x, len_vec.y, len_vec.z part_vec = len_vec / (num + 1) pos_vec = len_vec1 + len_vec / (num + 1) cur_jnt = sel[0] for i in range(num): new_position = [pos_vec.x, pos_vec.y, pos_vec.z] #print new_position new_joint = pm.insertJoint(cur_jnt) pm.joint(new_joint, edit=True, component=True, position=new_position) #pm.xform(new_joint, translation=new_position) pos_vec += part_vec cur_jnt = new_joint return None
def createController(object): # object = pma.ls(sl=True) pivotObj = pma.xform(object, query=True, t=True, worldSpace=True) edges = pma.filterExpand(pma.polyListComponentConversion(te=1), sm=32, ex=1) # convert edges to curve ancd create one object for edge in edges: vtx = pma.ls(pma.polyListComponentConversion(edge, fe=1, tv=1), fl=1) p1 = pma.pointPosition(vtx[0]) p2 = pma.pointPosition(vtx[1]) curves = pma.curve(n="line_ctrl_curve", d=1, p=(p1, p2)) ctrl = pma.curve(n="bool_ctrl", d=1, ws=True, p=pivotObj) pma.xform(centerPivots=True) for curveEdge in pma.ls("line_ctrl*"): pma.parent(curveEdge, ctrl, s=1, r=1) pma.rename(curveEdge, "shapeunused") transforms = pma.ls(type='transform') deleteList = [] for tran in transforms: if pma.nodeType(tran) == 'transform': children = pma.listRelatives(tran, c=True) if children is None: # print '%s, has no childred' %(tran) deleteList.append(tran) if not deleteList: pma.delete(deleteList) return ctrl
def putObjOnSnappableSpacing(obj, snappableSpacing): oSel = pm.ls(selection=True) destinationObj = obj usedObj = obj pm.select(destinationObj) t = pm.xform(query=True, rotatePivot=True, worldSpace=True) vt = pm.core.datatypes.Vector(t[0], t[1], t[2]) vt = onSnappableSpacingVec(vt, snappableSpacing) pm.select(usedObj) pm.move(vt, worldSpace=True, absolute=True, worldSpaceDistance=True) ## We compensate by getting the *object being moved*'s ## pivot t2 = pm.xform(query=True, rotatePivot=True, worldSpace=True) vt2 = pm.core.datatypes.Vector(t2[0], t2[1], t2[2]) ## vExtra is the additional amount compensated vExtra = vt - vt2 vDest = vt + vExtra vFinal = vDest pm.move(vFinal, worldSpace=True, absolute=True, worldSpaceDistance=True) pm.select(oSel)
def set_scale_param(**kwargs): total_len = 0.0 joint_values = {} sel_jnts = kwargs.get("joint_list", None) if not sel_jnts: sel_jnts = pm.ls(selection=True) vec1 = OpenMaya.MVector(0, 0, 0) vec2 = OpenMaya.MVector(0, 0, 0) vec3 = OpenMaya.MVector(0, 0, 0) joint_values[sel_jnts[0]] = 0.0 for index in range(len(sel_jnts) - 1): pos = pm.xform(sel_jnts[index], query=True, worldSpace=True, translation=True) vec1.x = pos[0] vec1.y = pos[1] vec1.z = pos[2] pos = pm.xform(sel_jnts[index + 1], query=True, worldSpace=True, translation=True) vec2.x = pos[0] vec2.y = pos[1] vec2.z = pos[2] vec3 = vec2 - vec1 total_len += vec3.length() joint_values[sel_jnts[index + 1]] = total_len mid = total_len / 2 for key in joint_values.keys(): joint_values[key] = joint_values[key] - mid return joint_values, total_len
def dense_chain(**kwargs): import pymel.core.datatypes as dt joints = kwargs.get("joints", None) joints_inbetween = kwargs.get("joints_inbetween", 5) if not joints: joints = pm.ls(selection=True) joints.pop(-1) for jnt in joints: child = jnt.getChildren() pos = pm.xform(jnt, query=True, translation=True, worldSpace=True) vpos1 = dt.Vector(pos) pos = pm.xform(child[0], query=True, translation=True, worldSpace=True) vpos2 = dt.Vector(pos) vpos = vpos2 - vpos1 div_vec = vpos / (joints_inbetween + 1) out_vec = vpos1 cur_jnt = jnt for i in range(joints_inbetween): out_vec = (out_vec + div_vec) pos = [out_vec.x, out_vec.y, out_vec.z] new_jnt = pm.insertJoint(cur_jnt) pm.joint(new_jnt, edit=True, component=True, position=pos, name=str(i)) cur_jnt = new_jnt return None
def copyObjects(**kwargs): """ Input object to be copied select positions where the objects needs be copied and run the script the copied object constraints the position objects if options are selected """ print "TEST" obj = kwargs.get("obj", "") prFlg = kwargs.get("prFlg", False) scFlg = kwargs.get("scFlg", False) sel = pm.ls(selection=True, flatten=True) ch = None for comp in sel: pos = pm.xform(comp, query=True, worldSpace=True, translation=True) new_obj = pm.duplicate(obj) pm.xform(new_obj, worldSpace=True, translation=pos) if prFlg or scFlg: typ = str(pm.nodeType(comp)) if typ == "transform": ch = comp else: shp = pm.ls(comp, objectsOnly=True)[0] trn = pm.listRelatives(shp, parent=True)[0] ch = trn if prFlg: pm.parentConstraint(new_obj, ch, maintainOffset=True) if scFlg: pm.scaleConstraint(new_obj, ch, maintainOffset=True) return None
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 update(self): #Get position of joint in Maya (world-space) mayaPos = pm.xform(self.mayaID, query=True, ws=True, t=True) #//in an array [x,y,z] #Update position reference for joint self.pos = Leap.Vector(mayaPos[0], mayaPos[1], mayaPos[2]) #Get Rotatation of joint in Maya mayaRot = pm.xform(self.mayaID, query=True, rotation=True) self.rot = Leap.Vector(mayaRot[0], mayaRot[1], mayaRot[2])
def set_composite_matrix(self, mat): #Convert into a maya matrix mat = mat.asMatrix() matAsFloatTuple = ( mat.a00, mat.a01, mat.a02, mat.a03, mat.a10, mat.a11, mat.a12, mat.a13, mat.a20, mat.a21, mat.a22, mat.a23, mat.a30, mat.a31, mat.a32, mat.a33 ) #Set the composition matrix on the object pm.xform(self.mayaID, ws=True, matrix=matAsFloatTuple) #Removed Euler euler=True because it was rotating about local axes pm.refresh(force=True)
def midPosVec(**kwargs): """ Returns the mid point from selected positions(objects or components) """ selected_items = kwargs.get("objects", []) if not selected_items: selected_items = pm.ls(selection=True, flatten=True) if not isinstance(selected_items, list): print "please provide objects list" pm.displayInfo("please provide objects list") return None number_of_items = len(selected_items) position_vector = [] final_vector = OpenMaya.MVector(0, 0, 0) for index in range(number_of_items): pos = pm.xform(selected_items[index], query=True, worldSpace=True, translation=True) vec = OpenMaya.MVector(pos[0], pos[1], pos[2]) position_vector.append(vec) for vector_index in range(len(position_vector)): final_vector = final_vector + position_vector[vector_index] final_vector = final_vector / len(position_vector) mid_position = [final_vector.x, final_vector.y, final_vector.z] return mid_position
def curve_through_points(**kwargs): selection_points = kwargs.get("selection_points", None) curve_degree = kwargs.get("curve_degree", 3) curve_name = kwargs.get("curve_name", "Curve") if not selection_points: selection_points = pm.ls(selection=True) if not selection_points: pm.displayInfo("Please select reference points") return None if len(selection_points) < curve_degree + 1: pm.displayInfo("please select more than " + str(curve_degree + 1) + " points") return None points_locations = [] for point in selection_points: points_locations.append( pm.xform(point, query=True, translation=True, worldSpace=True)) pm.select(clear=True) current_curve = pm.curve(degree=curve_degree, worldSpace=True, point=points_locations) pm.rename(current_curve, curve_name) return current_curve
def update(self): #Get the Current Target Position mayaPos = pm.xform(self.mayaID, query=True, t=True, ws=True) self.pos = Leap.Vector(mayaPos[0], mayaPos[1], mayaPos[2]) #PRoject to be in range of the armLength armLength = 50 #sum of the arm lengths sphereCenter = Leap.Vector(0,0,0)
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 obtain_selection_position(): """This method creates a list of positions from user selection returns : position_list """ user_selection = pm.ls(selection=True, flatten=True) position_list = [] for selected in user_selection: position_list.append( pm.xform(selected, query=True, worldSpace=True, translation=True)) return position_list
def get_composite_matrix(self): mVals = pm.xform(self.mayaID, query=True, matrix=True, ws=True) #Construct matrix from values of our composite matrix mat = [ [ float(mVals[0]), float(mVals[1]), float(mVals[2]), float(mVals[3]) ], [ float(mVals[4]), float(mVals[5]), float(mVals[6]), float(mVals[7]) ], [ float(mVals[8]), float(mVals[9]), float(mVals[10]), float(mVals[11]) ], [ float(mVals[12]), float(mVals[13]), float(mVals[14]), float(mVals[15]) ] ] #Turn mat into a transformation Matrix mat = dt.TransformationMatrix(dt.Matrix(mat)) return mat
def create_group(*args): """This method creates empty group 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 empty roup and move the empty group to # selected position, and increment the ID value to name # the next group in loop for objectPosition in position_list: group_name = object_name + str(object_id) + '_Group' created_group = pm.group(name=group_name, empty=True) pm.xform(created_group, translation=objectPosition, worldSpace=True) object_id = object_id + 1 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 at_selection(self, **kwargs): # get inputs setup_name = kwargs.get("name", None) path_name = kwargs.get("path", None) sample_obj = kwargs.get("sample", None) obj_lst = kwargs.get("selection_list", None) full_length = pm.arclen(path_name) paramVal = [] uVal = [] for obj in obj_lst: pos = pm.xform(obj, query=True, translation=True, worldSpace=True) param = self.getuParamVal(pnt=pos, crv=path_name) paramVal.append(param) crv_shp = pm.listRelatives(path_name, shapes=True)[0] arcLen = pm.arcLengthDimension(crv_shp + ".u[0]") 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) path_anim_list = [] if not self.get_use_selection(): obj_lst = [] if sample_obj: for i in uVal: obj_lst.append( pm.duplicate(sample_obj, name=setup_name + str(i + 1) + "_OBJECT")) else: for i in uVal: pm.select(clear=True) obj_lst.append( pm.joint(name=setup_name + str(i + 1) + "_JNT")) index = 0 for u in uVal: pm.select(clear=True) pathanim = pm.pathAnimation(obj_lst[index], curve=path_name, fractionMode=True, follow=True, followAxis="x", worldUpType="vector", worldUpVector=(0, 1, 0)) index += 1 path_anim_list.append(pathanim) pm.setAttr(str(pathanim) + ".uValue", u) pm.disconnectAttr(str(pathanim) + ".u") return (obj_lst, path_anim_list)
def center_position2(**kwargs): selected_items = kwargs.get("selected_items", []) #selected_items = pm.ls(selection=True, flatten=True) number_of_items = len(selected_items) print number_of_items position_vector = [] final_vector = OpenMaya.MVector(0, 0, 0) for index in range(number_of_items): pos = pm.xform(selected_items[index], query=True, worldSpace=True, translation=True) vec = OpenMaya.MVector(pos[0], pos[1], pos[2]) position_vector.append(vec) for vector_index in range(len(position_vector)): final_vector = final_vector + position_vector[vector_index] final_vector = final_vector / len(position_vector) mid_position = [final_vector.x, final_vector.y, final_vector.z] return mid_position
def curve_through_points(**kwargs): selection_points = pm.ls(selection=True) mt_pth = kwargs.get("motion_path", False) if len(selection_points) < 2: print "please select more than one points" return None curve_name = kwargs.get("curve_name", "") curve_degree = kwargs.get("curve_degree", 1) points_locations = [] for point in selection_points: points_locations.append( pm.xform(point, query=True, translation=True, worldSpace=True)) pm.select(clear=True) current_curve = pm.curve(degree=curve_degree, worldSpace=True, point=points_locations) pm.rename(current_curve, curve_name) print "CRV", current_curve if mt_pth: motion_path(objs=selection_points, path_curve=current_curve) return None
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 run(self, **kwargs): obj = kwargs.get("cur_obj", None) ctrl = kwargs.get("cur_ctrl", None) ctr_mode = kwargs.get("ctr_mode", None).lower() con_opn = kwargs.get("con_opn", None) connect_ctrl = kwargs.get("connect_ctrl", None) from_txt = kwargs.get("from_txt", "") to_txt = kwargs.get("to_txt", "") ctrl_nm_opn = kwargs.get("ctrl_nm_opn", None) ctrl_nm_txt = kwargs.get("ctrl_nm_txt", "") ctrl_sufx = kwargs.get("ctrl_sfx_txt", "") zero_node_opn = kwargs.get("zero_node_opn", None) zero_node_nm = kwargs.get("zero_nd_nm", "") lock_scl_opn = kwargs.get("lock_scl_opn", None) scale_check = kwargs.get("scale_check", False) ctrl_sz_offset = str(kwargs.get("ctrl_sz_offset", "0.5")) id_num = str(kwargs.get("id_num", "")) position_flag = kwargs.get("obj_pos_flag", False) pos_sel_lbl = kwargs.get("pos_sel_lbl", None) try: ctrl_sz_offset = float(ctrl_sz_offset) except ValueError: pm.displayError("invalid input for size offset value") return None z_frm_txt = "" if ctrl_nm_opn: z_frm_txt = ctrl_sufx else: z_frm_txt = to_txt self.set_control_position(cur_obj=obj, ctr=ctrl) if not position_flag: bound_box = obj.getBoundingBox() pos = self.get_boundary_position(bnd_bx=bound_box, pos_sel=pos_sel_lbl) ctr_pos = pm.xform(ctrl, query=True, translation=True, worldSpace=True) if pos_sel_lbl.find("X") > -1: ctr_pos[0] = pos elif pos_sel_lbl.find("Y") > -1: ctr_pos[1] = pos elif pos_sel_lbl.find("Z") > -1: ctr_pos[2] = pos #base_location = (bound_box[0])[1] ctrl.translate.set(ctr_pos) if scale_check: self.scale_ctr_to_obj(cur_obj=obj, ctr=ctrl, size_offset=ctrl_sz_offset) else: self.scale_uniform(ctr=ctrl, size_offset=ctrl_sz_offset) if not ctrl_nm_opn: self.rename_ctrl_from_obj(cur_obj=obj, ctr=ctrl, replace_from_str=from_txt, replace_to_str=to_txt) else: self.name_control(ctr=ctrl, ctrl_nm=ctrl_nm_txt, ctrl_sfx=ctrl_sufx, id_val=id_num) if zero_node_opn: self.create_zero_group(ctr=ctrl, from_nm=z_frm_txt, zero_nm=zero_node_nm) if connect_ctrl: if ctr_mode == "skin": self.skin(cur_obj=obj, ctr=ctrl) else: if con_opn[0]: pm.parentConstraint(ctrl, obj, maintainOffset=True) if con_opn[1]: pm.scaleConstraint(ctrl, obj, maintainOffset=True) if lock_scl_opn: self.lock_scale_vis_rad(ctr=ctrl) return None
def set_position(self, position): pm.xform(self.mayaID, t=(position[0],position[1], position[2]), ws=True) self.pos = position return True
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 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