def replaceInstances(self, parentNode, instanceNodes): for instanceNode in instanceNodes: trans = pm.getAttr(str(instanceNode) + '.translate') rot = pm.getAttr(str(instanceNode) + '.rotate') scl = pm.getAttr(str(instanceNode) + '.scale') pm.delete(instanceNode) pm.duplicate(parentNode, name=str(instanceNode)) pm.setAttr(str(instanceNode) + '.translate', trans) pm.setAttr(str(instanceNode) + '.rotate', rot) pm.setAttr(str(instanceNode) + '.scale', scl) return
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 merge_cards(start, end, suit): cards = [] # suits = [ 'Hearts', 'Diamonds', 'Clubs', 'Spades' ] ranks = range(start, end) total_cards = len(ranks) i = 0 # for suit in suits : try: suit_group = pm.PyNode('%s_cards_GRP' % (suit)) except: suit_group = pm.group(name='%s_cards_GRP' % (suit), empty=True, world=True) for rank in ranks: print 'Processing card %s of %s' % (i, total_cards) # print rank, suit # print get_card_sprites( rank, suit ) # return card_sprites = pm.duplicate(get_card_sprites(rank, suit)) for card_sprite in card_sprites: card_sprite.setParent(None) card = pm.polyUnite(card_sprites, mergeUVSets=1, ch=True)[0] pm.delete(card, ch=True) for cs in card_sprites: try: pm.delete(cs) except: pass card.rename('%s_%s_G' % (suit, rank)) card.setParent(suit_group) cards.append(card) # pm.flushUndo() i += 1 return cards
def merge_cards( start, end, suit ) : cards = [] # suits = [ 'Hearts', 'Diamonds', 'Clubs', 'Spades' ] ranks = range( start, end ) total_cards = len( ranks ) i = 0 # for suit in suits : try : suit_group = pm.PyNode( '%s_cards_GRP' % ( suit ) ) except : suit_group = pm.group( name='%s_cards_GRP' % ( suit ), empty=True, world=True ) for rank in ranks : print 'Processing card %s of %s' % ( i, total_cards ) # print rank, suit # print get_card_sprites( rank, suit ) # return card_sprites = pm.duplicate( get_card_sprites( rank, suit ) ) for card_sprite in card_sprites : card_sprite.setParent(None) card = pm.polyUnite( card_sprites, mergeUVSets=1, ch=True )[0] pm.delete( card, ch=True ) for cs in card_sprites : try : pm.delete( cs ) except : pass card.rename( '%s_%s_G' % ( suit, rank ) ) card.setParent( suit_group ) cards.append( card ) # pm.flushUndo() i += 1 return cards
def dupXformNoChildren( obj ): ## takes only a single object assert type(obj) == pymel.core.nodetypes.Transform print( obj ) dupShapesFinal = [] tempDuplicatedXforms = [] for s in obj.getShapes(): s = obj.getShape() ## we need a temporary xform to move the shape to tempXform = pm.createNode('transform') pm.parent( s, tempXform, shape=True, relative=True ) ## Duplicate the original shape ## we'll get a new duplicate of the transform ## and our duplicated shape node will be its shape dups = pm.duplicate( s ) tempDuplicatedXform = dups[0] tempDuplicatedXforms.append( tempDuplicatedXform ) dupShapesFinal.append( tempDuplicatedXform.getShape() ) ## Now put the original xform back where we got it from! pm.parent( s, obj, shape=True, relative=True ) pm.delete( tempXform ) ## Now we should be back where we started, except with a new ## duplicated shape node at the origin, with a indentity ## xform node ## Now that we have duplicates of all the shapes, ## we need a duplicated xform node dupList = pm.duplicate( obj, parentOnly=True, returnRootsOnly=True ) dupXform = dupList[0] ## Now parent all the duplicated shapes to the duplicated xform for s in dupShapesFinal: pm.parent( s, dupXform, shape=True, relative=True ) ## We have to delete tempInplaceXform later, because we need it for a bit pm.delete( tempDuplicatedXforms )
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 uniform_distribution(self, **kwargs): setup_name = kwargs.get("name", None) path_name = kwargs.get("path", None) sample_obj = kwargs.get("sample", None) divisions = kwargs.get("divisions", None) count = 0 part = float(1) / float(divisions) init = 0 obj_lst = [] path_anim_list = [] if not sample_obj: for i in range(divisions): pm.select(clear=True) obj_lst.append(pm.joint(name=setup_name + str(i + 1) + "_JNT")) else: for i in range(divisions): obj_lst.append( pm.duplicate(sample_obj, name=setup_name + str(i + 1) + "_Object")) index = 0 while count < divisions: 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", init) pm.disconnectAttr(str(pathanim) + ".u") init += part count += 1 return (obj_lst, path_anim_list)
def duplicateShape( shape, selectDup=False ): ''' Duplicates the shape node, does not by default select the result. Takes as an argument a single pymel shape node. ''' oSel = pm.ls(selection=True) ## we need a temporary xform to move the shape to originalParent = shape.getParent() tempXform = pm.createNode('transform') pm.parent( shape, tempXform, shape=True, relative=True ) ## Duplicate the original shape ## we'll get a new duplicate of the transform ## and our duplicated shape node will be its shape ## it's guaranteed to be a simple one xform ## hierarchy because we started from the new xform we created dups = pm.duplicate( shape ) tempDuplicatedXform = dups[0] dupShape = tempDuplicatedXform.getShape() ## Now put the original xform back where we got it from! ## also parent new duplicated shape back to original xform pm.parent( shape, originalParent, shape=True, relative=True ) pm.parent( dupShape, originalParent, shape=True, relative=True ) ## Clean up the temporary xforms that were used pm.delete( tempXform ) pm.delete( tempDuplicatedXform ) ## restore selection and return result if selectDup==False: pm.select( oSel ) return dupShape
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 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
def project_curves_onto_mesh2(mesh, source_mesh, direction): # duplicate sources_mesh dup_source_mesh = pm.duplicate(source_mesh)[0] dup_source_mesh.setParent(None) # create curve around mesh edge_curves = [] for edge in mesh.getShape().e: edge_curves.append( pm.curve( # name=n, degree=1, ws=True, point=[ v.getPosition(space='world') for v in edge.connectedVertices() ])) merged_curve = pm.attachCurve(edge_curves, method=1, keepMultipleKnots=False, ch=False)[0] merged_curve = pm.duplicate(merged_curve) pm.delete(edge_curves) # project curve onto dup_source_mesh projected_curves = projected_curves = pm.polyProjectCurve( dup_source_mesh, merged_curve, direction=direction)[0] # pm.delete( projected_curves.getChildren()[1:] ) projected_curve = projected_curves.getChildren()[0] split_mesh = pm.polySplit(dup_source_mesh, detachEdges=0, projectedCurve=projected_curve) split_mesh = split_mesh[0] # delete faces not within mesh bounds # faces_to_delete = [] pm.select(None) for face in split_mesh.f: face_center = (0.0, 0.0, 0.0) for v in face.connectedVertices(): face_center += v.getPosition(space='world') face_center /= len(face.connectedVertices()) if (point_in_rect_bb(face_center, mesh.getBoundingBox(space='world'))): # faces_to_delete.append( face ) dot = face.getNormal(space='world').dot( mesh.f[0].getNormal(space='world')) if (dot > 0.0): pm.select(face, add=True) # for face in faces_to_delete : # dot = face.getNormal( space='world' ).dot( mesh.f[0].getNormal( space='world' ) ) # if( dot > 0.0 ) : # pm.select(face, add=True) pm.runtime.InvertSelection() pm.delete() # transfer UVs from mesh to dup_source_mesh pm.transferAttributes(mesh, split_mesh, transferUVs=2) # assign mesh material to dup_source_mesh # rename dup_source_mesh to mesh pm.delete(split_mesh, ch=True) n = mesh.name() p = mesh.getParent() pm.delete(mesh) split_mesh.rename(n) for attr in split_mesh.listAttr(): if attr.isLocked(): attr.setLocked(False) # cleanup pm.delete(projected_curves) pm.delete(merged_curve) pm.delete(dup_source_mesh) # split_mesh.centerPivots( True ) # t = split_mesh.getPivots( worldSpace=1 )[0] # split_mesh.setTranslation((-t[0], -t[1], -t[2]), space='world') # pm.makeIdentity( split_mesh, apply=True ) # split_mesh.setParent( p ) # split_mesh.setTranslation( ( 0,0,0 ) ) # pm.makeIdentity( split_mesh, apply=True ) # position bodge split_mesh.setTranslation((0, 0, 1), space='world') split_mesh.setParent(p) pm.polyTriangulate(split_mesh) if (not split_mesh.hasAttr(THU_MFT_SPRITE_ATTR)): split_mesh.addAttr(THU_MFT_SPRITE_ATTR, dt='string') split_mesh.setAttr(THU_MFT_SPRITE_ATTR, split_mesh.name().replace('.png', ''))
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 get_duplicate_path(self, **kwargs): path_crv = kwargs.get("path_crv", None) if path_crv: duplicate_crv = pm.duplicate(path_crv)[0] return duplicate_crv
import pymel.all as pm ## Get selected objects total = int(input()) oSel = pm.ls(selection=True) toothOrig = oSel[0] path = oSel[1] new_teeth = [] for n in range(total): new_tooth_list = pm.duplicate(toothOrig) new_tooth = new_tooth_list[0] new_tooth_name = new_tooth.name() new_teeth.append(new_tooth) mPathName = pm.pathAnimation(new_tooth, curve=path, follow=True, followAxis='x', upAxis='y', inverseUp=True, worldUpObject='inverse_up_ref_xform', worldUpType='object') mPath = pm.PyNode(mPathName) mPath.rename(new_tooth_name + '_mpath') mPathName = mPath.name() mPath.uValue.disconnect() exprName = new_tooth_name + '_expr'
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 ## Get selected objects total = int( input() ) oSel = pm.ls(selection=True) toothOrig = oSel[0] path = oSel[1] new_teeth = [] for n in range(total): new_tooth_list = pm.duplicate( toothOrig ) new_tooth = new_tooth_list[0] new_tooth_name = new_tooth.name() new_teeth.append( new_tooth ) mPathName = pm.pathAnimation( new_tooth, curve=path, follow=True, followAxis='x', upAxis='y', inverseUp=True, worldUpObject='inverse_up_ref_xform', worldUpType='object' ) mPath = pm.PyNode(mPathName) mPath.rename( new_tooth_name + '_mpath' )
def setup_ik_spline(**kwargs): curve = kwargs.get("curve", None) joint_chain = kwargs.get("joint_chain", None) auto_curve = kwargs.get("auto_curve", True) use_curve = kwargs.get("use_curve", None) spans = kwargs.get("number_of_spans", 4) ctrl_jnts = kwargs.get("num_control_joints", 3) ik_name = kwargs.get("ik_name", "ikHandle") scale_stretch = kwargs.get("scale_stretch", False) create_dense_chain = kwargs.get("dense_chain", False) dense_division = kwargs.get("dense_chain_divisions", 3) auto_simplify = kwargs.get("auto_simplify_curve", False) stretch_exp = kwargs.get("stretch_exp", False) global_scale_check = kwargs.get("global_scale_check", False) global_scale_attr = kwargs.get("global_scale_attr", None) pm.select(joint_chain, hierarchy=True) joint_chain = pm.ls(selection=True) if not isinstance(joint_chain[0], pm.Joint): pm.displayInfo("selection should be of type joint") return None if len(joint_chain) < 2: pm.displayInfo("Chain should consist of more than one joint") return None if (global_scale_check): if (global_scale_attr is None): pm.displayInfo("Please input global scale attribute") return None else: obj = global_scale_attr.split(".")[0] global_attr = global_scale_attr.split(".")[1] check_global_attr = pm.attributeQuery(global_attr, node=obj, exists=True) if not check_global_attr: pm.displayInfo("Invalid global scale attribute") return None start_jnt = joint_chain[0] end_joint = joint_chain[-1] if create_dense_chain: rep_chain = pm.duplicate(joint_chain) start_jnt = rep_chain[0] end_joint = rep_chain[-1] dense_chain(joints=rep_chain, joints_inbetween=dense_division) rep_chain.append(end_joint) for index in range(len(joint_chain)): pm.parentConstraint(rep_chain[index], joint_chain[index], maintainOffset=False) #pm.scaleConstraint(rep_chain[index], joint_chain[index], maintainOffset=False) pm.connectAttr( str(rep_chain[index]) + ".scale", str(joint_chain[index]) + ".scale") pm.select(start_jnt, hierarchy=True) new_chain = pm.ls(selection=True) crv = "" #print "START", start_jnt #print "END",end_joint if auto_curve: ik_handle, eff, crv = pm.ikHandle(startJoint=start_jnt, createCurve=auto_curve, solver="ikSplineSolver", numSpans=spans, endEffector=end_joint, simplifyCurve=auto_simplify) else: crv = pm.PyNode(use_curve) ik_handle, eff = pm.ikHandle(startJoint=start_jnt, curve=use_curve, solver="ikSplineSolver", endEffector=end_joint, createCurve=False) crv.inheritsTransform.set(0) pm.rename(ik_handle, ik_name + "IK_Handle") pm.rename(crv, ik_name + "IK_Curve") ik_curve_shp = crv.getShape() crv_info_node = pm.createNode("curveInfo") pm.connectAttr(ik_curve_shp + ".worldSpace", crv_info_node + ".inputCurve") ''' if stretch_exp: if create_dense_chain: stretch_expression(joints = new_chain, curve_info_node = crv_info_node, connect_scale = scale_stretch, expression_name = ik_name+"_stretch_expression") else: stretch_expression(joints = joint_chain, curve_info_node = crv_info_node, connect_scale = scale_stretch, expression_name = ik_name+"_stretch_expression") ''' if ctrl_jnts: if ctrl_jnts == 1: print "Minimum 2 joints needed as controllers" print "skipping control joint creation process" pm.displayInfo("Minimum 2 joints needed as controllers") else: ctrl_jnts = joints_along_curve(number_of_joints=ctrl_jnts, curve=crv, bind_curve_to_joint=True) pm.select(clear=True) ctr_jnt_gp = pm.group(ctrl_jnts, name="control_joints") #print "JNT NAME", ctr_jnt_gp if stretch_exp: pm.addAttr(ctrl_jnts[-1], longName="Stretch", attributeType="enum", enumName="off:on", keyable=True) print "ATTRIBUTE TO", str(ctrl_jnts[-1]) if create_dense_chain: stretch_expression(joints=new_chain, curve_info_node=crv_info_node, connect_scale=scale_stretch, expression_name=ik_name + "_stretch_expression", ctrl_attr=str(ctrl_jnts[-1]) + ".Stretch", glbl_scl_stat=global_scale_check, glbl_scl_attr=global_scale_attr) else: stretch_expression(joints=joint_chain, curve_info_node=crv_info_node, connect_scale=scale_stretch, expression_name=ik_name + "_stretch_expression", ctrl_attr=str(ctrl_jnts[-1]) + ".Stretch", glbl_scl_stat=global_scale_check, glbl_scl_attr=global_scale_attr) final_group = pm.group(name=ik_name + "_ik_group", empty=True) pm.parent(joint_chain[0], final_group) pm.parent(crv, final_group) pm.parent(ik_handle, final_group) if ctrl_jnts > 1: pm.parent(ctr_jnt_gp, final_group) if create_dense_chain: pm.select(clear=True) dense_grp = pm.group(start_jnt, name="dense_chain_group") pm.parent(dense_grp, final_group) return None
def project_curves_onto_mesh2( mesh, source_mesh, direction ) : # duplicate sources_mesh dup_source_mesh = pm.duplicate( source_mesh )[0] dup_source_mesh.setParent(None) # create curve around mesh edge_curves = [] for edge in mesh.getShape().e : edge_curves.append( pm.curve( # name=n, degree=1, ws=True, point=[ v.getPosition(space='world') for v in edge.connectedVertices() ] ) ) merged_curve = pm.attachCurve( edge_curves, method=1, keepMultipleKnots=False, ch=False )[0] merged_curve = pm.duplicate( merged_curve ) pm.delete( edge_curves ) # project curve onto dup_source_mesh projected_curves = projected_curves = pm.polyProjectCurve( dup_source_mesh, merged_curve, direction=direction )[0] # pm.delete( projected_curves.getChildren()[1:] ) projected_curve = projected_curves.getChildren()[0] split_mesh = pm.polySplit( dup_source_mesh, detachEdges=0, projectedCurve=projected_curve ) split_mesh = split_mesh[0] # delete faces not within mesh bounds # faces_to_delete = [] pm.select(None) for face in split_mesh.f : face_center = ( 0.0, 0.0, 0.0 ) for v in face.connectedVertices() : face_center += v.getPosition( space='world' ) face_center /= len( face.connectedVertices() ) if( point_in_rect_bb( face_center, mesh.getBoundingBox( space='world' ) ) ) : # faces_to_delete.append( face ) dot = face.getNormal( space='world' ).dot( mesh.f[0].getNormal( space='world' ) ) if( dot > 0.0 ) : pm.select(face, add=True) # for face in faces_to_delete : # dot = face.getNormal( space='world' ).dot( mesh.f[0].getNormal( space='world' ) ) # if( dot > 0.0 ) : # pm.select(face, add=True) pm.runtime.InvertSelection() pm.delete() # transfer UVs from mesh to dup_source_mesh pm.transferAttributes( mesh, split_mesh, transferUVs=2 ) # assign mesh material to dup_source_mesh # rename dup_source_mesh to mesh pm.delete( split_mesh, ch=True ) n = mesh.name() p = mesh.getParent() pm.delete( mesh ) split_mesh.rename( n ) for attr in split_mesh.listAttr() : if attr.isLocked() : attr.setLocked(False) # cleanup pm.delete( projected_curves ) pm.delete( merged_curve ) pm.delete( dup_source_mesh ) # split_mesh.centerPivots( True ) # t = split_mesh.getPivots( worldSpace=1 )[0] # split_mesh.setTranslation((-t[0], -t[1], -t[2]), space='world') # pm.makeIdentity( split_mesh, apply=True ) # split_mesh.setParent( p ) # split_mesh.setTranslation( ( 0,0,0 ) ) # pm.makeIdentity( split_mesh, apply=True ) # position bodge split_mesh.setTranslation( ( 0, 0, 1 ), space='world' ) split_mesh.setParent( p ) pm.polyTriangulate( split_mesh ) if( not split_mesh.hasAttr( THU_MFT_SPRITE_ATTR ) ) : split_mesh.addAttr( THU_MFT_SPRITE_ATTR, dt='string' ) split_mesh.setAttr( THU_MFT_SPRITE_ATTR, split_mesh.name().replace( '.png', '' ) )