def applyPathCnsLocal(target, curve, u): cns = applyop.pathCns(target, curve, cnsType=False, u=u, tangent=False) pm.connectAttr(curve.attr("local"), cns.attr("geometryPath"), f=True) # tobe local space comp_node = pm.createNode("composeMatrix") cns.attr("allCoordinates") >> comp_node.attr("inputTranslate") cns.attr("rotate") >> comp_node.attr("inputRotate") cns.attr("rotateOrder") >> comp_node.attr("inputRotateOrder") mul_node = pm.createNode("multMatrix") comp_node2 = pm.createNode("composeMatrix") pos = target.getTranslation(space="world") if pos.x < -0.001: pm.setAttr(comp_node2.attr("inputScaleX"), -1.0) pm.setAttr(comp_node2.attr("inputRotateX"), 90.0) pm.setAttr(comp_node2.attr("inputRotateZ"), 90.0) comp_node2.attr("outputMatrix") >> mul_node.attr("matrixIn[0]") comp_node.attr("outputMatrix") >> mul_node.attr("matrixIn[1]") curve.attr("matrix") >> mul_node.attr("matrixIn[2]") decomp_node = pm.createNode("decomposeMatrix") mul_node.attr("matrixSum") >> decomp_node.attr("inputMatrix") decomp_node.attr("outputTranslate") >> target.attr("translate") decomp_node.attr("outputRotate") >> target.attr("rotate") decomp_node.attr("outputScale") >> target.attr("scale") return cns
def addOperators(self): """Create operators and set the relations for the component rig Apply operators, constraints, expressions to the hierarchy. In order to keep the code clean and easier to debug, we shouldn't create any new object in this method. """ dm_node_scl = node.createDecomposeMatrixNode(self.root.worldMatrix) step = 1.000 / (self.settings["div"] - 1) u = 0.000 for i in range(self.settings["div"]): applyop.pathCns(self.upv_cns[i], self.upv_crv, cnsType=False, u=u, tangent=False) cns = applyop.pathCns(self.div_cns[i], self.mst_crv, False, u, True) # Connectiong the scale for scaling compensation for axis, AX in zip("xyz", "XYZ"): pm.connectAttr(dm_node_scl.attr("outputScale{}".format(AX)), self.div_cns[i].attr("s{}".format(axis))) cns.setAttr("worldUpType", 1) cns.setAttr("frontAxis", 0) cns.setAttr("upAxis", 1) pm.connectAttr(self.upv_cns[i].attr("worldMatrix[0]"), cns.attr("worldUpMatrix")) u += step for ctl in [self.base_ctl, self.tan0_ctl, self.tan1_ctl, self.tip_ctl]: for shp in ctl.getShapes(): pm.connectAttr(self.ctlVis_att, shp.attr("visibility")) for tweak_ctl in self.extratweak_ctl: for shp in tweak_ctl.getShapes(): pm.connectAttr(self.tweakVis_att, shp.attr("visibility"))
def applyPathCnsLocal(target, curve, u): cns = applyop.pathCns(target, curve, cnsType=False, u=u, tangent=False) pm.connectAttr(curve.attr("local"), cns.attr("geometryPath"), f=True) # tobe local space comp_node = pm.createNode("composeMatrix") cns.attr("allCoordinates") >> comp_node.attr("inputTranslate") cns.attr("rotate") >> comp_node.attr("inputRotate") cns.attr("rotateOrder") >> comp_node.attr("inputRotateOrder") mul_node = pm.createNode("multMatrix") comp_node.attr("outputMatrix") >> mul_node.attr("matrixIn[0]") curve.attr("matrix") >> mul_node.attr("matrixIn[1]") decomp_node = pm.createNode("decomposeMatrix") mul_node.attr("matrixSum") >> decomp_node.attr("inputMatrix") decomp_node.attr("outputTranslate") >> target.attr("translate") decomp_node.attr("outputRotate") >> target.attr("rotate") return cns
def addOperators(self): """Create operators and set the relations for the component rig Apply operators, constraints, expressions to the hierarchy. In order to keep the code clean and easier to debug, we shouldn't create any new object in this method. """ # Auto bend ---------------------------- if self.settings["autoBend"]: mul_node = node.createMulNode( [self.autoBendChain[0].ry, self.autoBendChain[0].rz], [self.sideBend_att, self.frontBend_att]) mul_node.outputX >> self.ik1autoRot_lvl.rz mul_node.outputY >> self.ik1autoRot_lvl.rx self.ikHandleAutoBend = primitive.addIkHandle( self.autoBend_ctl, self.getName("ikHandleAutoBend"), self.autoBendChain, "ikSCsolver") # Tangent position --------------------------------- # common part d = vector.getDistance(self.guide.apos[0], self.guide.apos[-1]) dist_node = node.createDistNode(self.ik0_ctl, self.ik1_ctl) rootWorld_node = node.createDecomposeMatrixNode( self.root.attr("worldMatrix")) div_node = node.createDivNode(dist_node + ".distance", rootWorld_node + ".outputScaleX") div_node = node.createDivNode(div_node + ".outputX", d) # tan0 mul_node = node.createMulNode(self.tan0_att, self.tan0_npo.getAttr("ty")) res_node = node.createMulNode(mul_node + ".outputX", div_node + ".outputX") pm.connectAttr(res_node + ".outputX", self.tan0_npo.attr("ty")) # tan1 mul_node = node.createMulNode(self.tan1_att, self.tan1_npo.getAttr("ty")) res_node = node.createMulNode(mul_node + ".outputX", div_node + ".outputX") pm.connectAttr(res_node + ".outputX", self.tan1_npo.attr("ty")) # Tangent Mid -------------------------------------- if self.settings["centralTangent"]: tanIntMat = applyop.gear_intmatrix_op( self.tan0_npo.attr("worldMatrix"), self.tan1_npo.attr("worldMatrix"), .5) applyop.gear_mulmatrix_op( tanIntMat.attr("output"), self.tan_npo.attr("parentInverseMatrix[0]"), self.tan_npo) pm.connectAttr(self.tan_ctl.attr("translate"), self.tan0_off.attr("translate")) pm.connectAttr(self.tan_ctl.attr("translate"), self.tan1_off.attr("translate")) # Curves ------------------------------------------- op = applyop.gear_curveslide2_op(self.slv_crv, self.mst_crv, 0, 1.5, .5, .5) pm.connectAttr(self.position_att, op + ".position") pm.connectAttr(self.maxstretch_att, op + ".maxstretch") pm.connectAttr(self.maxsquash_att, op + ".maxsquash") pm.connectAttr(self.softness_att, op + ".softness") # Volume driver ------------------------------------ crv_node = node.createCurveInfoNode(self.slv_crv) # Division ----------------------------------------- for i in range(self.settings["division"]): # References u = i / (self.settings["division"] - 1.0) if i == 0: # we add extra 10% to the first vertebra u = (1.0 / (self.settings["division"] - 1.0)) / 10 cns = applyop.pathCns(self.div_cns[i], self.slv_crv, False, u, True) cns.setAttr("frontAxis", 1) # front axis is 'Y' cns.setAttr("upAxis", 0) # front axis is 'X' # Roll intMatrix = applyop.gear_intmatrix_op( self.ik0_ctl + ".worldMatrix", self.ik1_ctl + ".worldMatrix", u) dm_node = node.createDecomposeMatrixNode(intMatrix + ".output") pm.connectAttr(dm_node + ".outputRotate", self.twister[i].attr("rotate")) pm.parentConstraint(self.twister[i], self.ref_twist[i], maintainOffset=True) pm.connectAttr(self.ref_twist[i] + ".translate", cns + ".worldUpVector") # compensate scale reference div_node = node.createDivNode([1, 1, 1], [ rootWorld_node + ".outputScaleX", rootWorld_node + ".outputScaleY", rootWorld_node + ".outputScaleZ" ]) # Squash n Stretch op = applyop.gear_squashstretch2_op(self.scl_transforms[i], self.root, pm.arclen(self.slv_crv), "y", div_node + ".output") pm.connectAttr(self.volume_att, op + ".blend") pm.connectAttr(crv_node + ".arcLength", op + ".driver") pm.connectAttr(self.st_att[i], op + ".stretch") pm.connectAttr(self.sq_att[i], op + ".squash") # Controlers if i == 0: mulmat_node = applyop.gear_mulmatrix_op( self.div_cns[i].attr("worldMatrix"), self.root.attr("worldInverseMatrix")) dm_node = node.createDecomposeMatrixNode(mulmat_node + ".output") pm.connectAttr(dm_node + ".outputTranslate", self.fk_npo[i].attr("t")) else: mulmat_node = applyop.gear_mulmatrix_op( self.div_cns[i].attr("worldMatrix"), self.div_cns[i - 1].attr("worldInverseMatrix")) dm_node = node.createDecomposeMatrixNode(mulmat_node + ".output") mul_node = node.createMulNode(div_node + ".output", dm_node + ".outputTranslate") pm.connectAttr(mul_node + ".output", self.fk_npo[i].attr("t")) pm.connectAttr(dm_node + ".outputRotate", self.fk_npo[i].attr("r")) # Orientation Lock if i == 0: dm_node = node.createDecomposeMatrixNode(self.ik0_ctl + ".worldMatrix") blend_node = node.createBlendNode( [dm_node + ".outputRotate%s" % s for s in "XYZ"], [cns + ".rotate%s" % s for s in "XYZ"], self.lock_ori0_att) self.div_cns[i].attr("rotate").disconnect() pm.connectAttr(blend_node + ".output", self.div_cns[i] + ".rotate") elif i == self.settings["division"] - 1: dm_node = node.createDecomposeMatrixNode(self.ik1_ctl + ".worldMatrix") blend_node = node.createBlendNode( [dm_node + ".outputRotate%s" % s for s in "XYZ"], [cns + ".rotate%s" % s for s in "XYZ"], self.lock_ori1_att) self.div_cns[i].attr("rotate").disconnect() pm.connectAttr(blend_node + ".output", self.div_cns[i] + ".rotate") # Connections (Hooks) ------------------------------ pm.parentConstraint(self.hip_lvl, self.cnx0) pm.scaleConstraint(self.hip_lvl, self.cnx0) pm.parentConstraint(self.scl_transforms[-1], self.cnx1) pm.scaleConstraint(self.scl_transforms[-1], self.cnx1)
def addOperators(self): """Create operators and set the relations for the component rig Apply operators, constraints, expressions to the hierarchy. In order to keep the code clean and easier to debug, we shouldn't create any new object in this method. """ # Tangent position --------------------------------- # common part d = vector.getDistance(self.guide.pos["root"], self.guide.pos["neck"]) dist_node = node.createDistNode(self.root, self.ik_ctl) rootWorld_node = node.createDecomposeMatrixNode( self.root.attr("worldMatrix")) div_node = node.createDivNode(dist_node + ".distance", rootWorld_node + ".outputScaleX") div_node = node.createDivNode(div_node + ".outputX", d) # tan0 mul_node = node.createMulNode(self.tan0_att, self.tan0_loc.getAttr("ty")) res_node = node.createMulNode(mul_node + ".outputX", div_node + ".outputX") pm.connectAttr(res_node + ".outputX", self.tan0_loc + ".ty") # tan1 mul_node = node.createMulNode(self.tan1_att, self.tan1_loc.getAttr("ty")) res_node = node.createMulNode(mul_node + ".outputX", div_node + ".outputX") pm.connectAttr(res_node + ".outputX", self.tan1_loc.attr("ty")) # Curves ------------------------------------------- op = applyop.gear_curveslide2_op(self.slv_crv, self.mst_crv, 0, 1.5, 0.5, 0.5) pm.connectAttr(self.maxstretch_att, op + ".maxstretch") pm.connectAttr(self.maxsquash_att, op + ".maxsquash") pm.connectAttr(self.softness_att, op + ".softness") # Volume driver ------------------------------------ crv_node = node.createCurveInfoNode(self.slv_crv) # Division ----------------------------------------- for i in range(self.divisions): # References u = i / (self.divisions - 1.0) cns = applyop.pathCns(self.div_cns[i], self.slv_crv, False, u, True) cns.setAttr("frontAxis", 1) # front axis is 'Y' cns.setAttr("upAxis", 2) # front axis is 'Z' # Roll intMatrix = applyop.gear_intmatrix_op( self.intMRef + ".worldMatrix", self.ik_ctl + ".worldMatrix", u) dm_node = node.createDecomposeMatrixNode(intMatrix + ".output") pm.connectAttr(dm_node + ".outputRotate", self.twister[i].attr("rotate")) pm.parentConstraint(self.twister[i], self.ref_twist[i], maintainOffset=True) pm.connectAttr(self.ref_twist[i] + ".translate", cns + ".worldUpVector") # Squash n Stretch op = applyop.gear_squashstretch2_op(self.fk_npo[i], self.root, pm.arclen(self.slv_crv), "y") pm.connectAttr(self.volume_att, op + ".blend") pm.connectAttr(crv_node + ".arcLength", op + ".driver") pm.connectAttr(self.st_att[i], op + ".stretch") pm.connectAttr(self.sq_att[i], op + ".squash") op.setAttr("driver_min", 0.1) # scl compas if i != 0: div_node = node.createDivNode( [1, 1, 1], [ self.fk_npo[i - 1] + ".sx", self.fk_npo[i - 1] + ".sy", self.fk_npo[i - 1] + ".sz", ], ) pm.connectAttr(div_node + ".output", self.scl_npo[i] + ".scale") # Controlers if i == 0: mulmat_node = applyop.gear_mulmatrix_op( self.div_cns[i].attr("worldMatrix"), self.root.attr("worldInverseMatrix"), ) else: mulmat_node = applyop.gear_mulmatrix_op( self.div_cns[i].attr("worldMatrix"), self.div_cns[i - 1].attr("worldInverseMatrix"), ) dm_node = node.createDecomposeMatrixNode(mulmat_node + ".output") pm.connectAttr(dm_node + ".outputTranslate", self.fk_npo[i].attr("t")) pm.connectAttr(dm_node + ".outputRotate", self.fk_npo[i].attr("r")) # Orientation Lock if i == self.divisions - 1: dm_node = node.createDecomposeMatrixNode(self.ik_ctl + ".worldMatrix") blend_node = node.createBlendNode( [dm_node + ".outputRotate%s" % s for s in "XYZ"], [cns + ".rotate%s" % s for s in "XYZ"], self.lock_ori_att, ) self.div_cns[i].attr("rotate").disconnect() pm.connectAttr(blend_node + ".output", self.div_cns[i] + ".rotate") # Head --------------------------------------------- self.fk_ctl[-1].addChild(self.head_cns) # scale compensation dm_node = node.createDecomposeMatrixNode(self.scl_npo[0] + ".parentInverseMatrix") pm.connectAttr(dm_node + ".outputScale", self.scl_npo[0] + ".scale")
def rig(edge_loop="", up_vertex="", low_vertex="", name_prefix="", thickness=0.3, do_skin=True, rigid_loops=5, falloff_loops=8, head_joint=None, jaw_joint=None, parent_node=None, control_name="ctl", upper_lip_ctl=None, lower_lip_ctl=None): ###### # Var ###### FRONT_OFFSET = .02 NB_ROPE = 15 ################## # Helper functions ################## def setName(name, side="C", idx=None): namesList = [name_prefix, side, name] if idx is not None: namesList[1] = side + str(idx) name = "_".join(namesList) return name ############### # Checkers ############## # Loop if edge_loop: try: edge_loop = [pm.PyNode(e) for e in edge_loop.split(",")] except pm.MayaNodeError: pm.displayWarning( "Some of the edges listed in edge loop can not be found") return else: pm.displayWarning("Please set the edge loop first") return # Vertex if up_vertex: try: up_vertex = pm.PyNode(up_vertex) except pm.MayaNodeError: pm.displayWarning("%s can not be found" % up_vertex) return else: pm.displayWarning("Please set the upper lip central vertex") return if low_vertex: try: low_vertex = pm.PyNode(low_vertex) except pm.MayaNodeError: pm.displayWarning("%s can not be found" % low_vertex) return else: pm.displayWarning("Please set the lower lip central vertex") return # skinnign data if do_skin: if not head_joint: pm.displayWarning("Please set the Head Jnt or unCheck Compute " "Topological Autoskin") return else: try: head_joint = pm.PyNode(head_joint) except pm.MayaNodeError: pm.displayWarning( "Head Joint: %s can not be found" % head_joint ) return if not jaw_joint: pm.displayWarning("Please set the Jaw Jnt or unCheck Compute " "Topological Autoskin") return else: try: jaw_joint = pm.PyNode(jaw_joint) except pm.MayaNodeError: pm.displayWarning("Jaw Joint: %s can not be found" % jaw_joint) return # check if the rig already exist in the current scene if pm.ls(setName("root")): pm.displayWarning("The object %s already exist in the scene. Please " "choose another name prefix" % setName("root")) return ##################### # Root creation ##################### lips_root = primitive.addTransform(None, setName("root")) lipsCrv_root = primitive.addTransform(lips_root, setName("crvs")) lipsRope_root = primitive.addTransform(lips_root, setName("rope")) ##################### # Geometry ##################### geo = pm.listRelatives(edge_loop[0], parent=True)[0] ##################### # Groups ##################### try: ctlSet = pm.PyNode("rig_controllers_grp") except pm.MayaNodeError: pm.sets(n="rig_controllers_grp", em=True) ctlSet = pm.PyNode("rig_controllers_grp") try: defset = pm.PyNode("rig_deformers_grp") except pm.MayaNodeError: pm.sets(n="rig_deformers_grp", em=True) defset = pm.PyNode("rig_deformers_grp") ##################### # Curves creation ##################### # get extreme position using the outer loop extr_v = meshNavigation.getExtremeVertexFromLoop(edge_loop) upPos = extr_v[0] lowPos = extr_v[1] inPos = extr_v[2] outPos = extr_v[3] edgeList = extr_v[4] vertexList = extr_v[5] upPos = up_vertex lowPos = low_vertex # upper crv upLip_edgeRange = meshNavigation.edgeRangeInLoopFromMid(edgeList, upPos, inPos, outPos) upCrv = curve.createCuveFromEdges(upLip_edgeRange, setName("upperLip"), parent=lipsCrv_root) # store the closest vertex by curv cv index. To be use fo the auto skining upLip_closestVtxList = [] # offset upper lip Curve cvs = upCrv.getCVs(space="world") for i, cv in enumerate(cvs): closestVtx = meshNavigation.getClosestVertexFromTransform(geo, cv) upLip_closestVtxList.append(closestVtx) if i == 0: # we know the curv starts from right to left offset = [cv[0] - thickness, cv[1], cv[2] - thickness] elif i == len(cvs) - 1: offset = [cv[0] + thickness, cv[1], cv[2] - thickness] else: offset = [cv[0], cv[1] + thickness, cv[2]] upCrv.setCV(i, offset, space='world') # lower crv lowLip_edgeRange = meshNavigation.edgeRangeInLoopFromMid(edgeList, lowPos, inPos, outPos) lowCrv = curve.createCuveFromEdges(lowLip_edgeRange, setName("lowerLip"), parent=lipsCrv_root) lowLip_closestVtxList = [] # offset lower lip Curve cvs = lowCrv.getCVs(space="world") for i, cv in enumerate(cvs): closestVtx = meshNavigation.getClosestVertexFromTransform(geo, cv) lowLip_closestVtxList.append(closestVtx) if i == 0: # we know the curv starts from right to left offset = [cv[0] - thickness, cv[1], cv[2] - thickness] elif i == len(cvs) - 1: offset = [cv[0] + thickness, cv[1], cv[2] - thickness] else: # we populate the closest vertext list here to skipt the first # and latest point offset = [cv[0], cv[1] - thickness, cv[2]] lowCrv.setCV(i, offset, space='world') upCrv_ctl = curve.createCurveFromCurve(upCrv, setName("upCtl_crv"), nbPoints=7, parent=lipsCrv_root) lowCrv_ctl = curve.createCurveFromCurve(lowCrv, setName("lowCtl_crv"), nbPoints=7, parent=lipsCrv_root) upRope = curve.createCurveFromCurve(upCrv, setName("upRope_crv"), nbPoints=NB_ROPE, parent=lipsCrv_root) lowRope = curve.createCurveFromCurve(lowCrv, setName("lowRope_crv"), nbPoints=NB_ROPE, parent=lipsCrv_root) upCrv_upv = curve.createCurveFromCurve(upCrv, setName("upCrv_upv"), nbPoints=7, parent=lipsCrv_root) lowCrv_upv = curve.createCurveFromCurve(lowCrv, setName("lowCrv_upv"), nbPoints=7, parent=lipsCrv_root) upRope_upv = curve.createCurveFromCurve(upCrv, setName("upRope_upv"), nbPoints=NB_ROPE, parent=lipsCrv_root) lowRope_upv = curve.createCurveFromCurve(lowCrv, setName("lowRope_upv"), nbPoints=NB_ROPE, parent=lipsCrv_root) # offset upv curves for crv in [upCrv_upv, lowCrv_upv, upRope_upv, lowRope_upv]: cvs = crv.getCVs(space="world") for i, cv in enumerate(cvs): # we populate the closest vertext list here to skipt the first # and latest point offset = [cv[0], cv[1], cv[2] + FRONT_OFFSET] crv.setCV(i, offset, space='world') rigCrvs = [upCrv, lowCrv, upCrv_ctl, lowCrv_ctl, upRope, lowRope, upCrv_upv, lowCrv_upv, upRope_upv, lowRope_upv] for crv in rigCrvs: crv.attr("visibility").set(False) ################## # Joints ################## lvlType = "transform" # upper joints upperJoints = [] cvs = upCrv.getCVs(space="world") pm.progressWindow(title='Creating Upper Joints', progress=0, max=len(cvs)) for i, cv in enumerate(cvs): pm.progressWindow(e=True, step=1, status='\nCreating Joint for %s' % cv) oTransUpV = pm.PyNode(pm.createNode( lvlType, n=setName("upLipRopeUpv", idx=str(i).zfill(3)), p=lipsRope_root, ss=True)) oTrans = pm.PyNode( pm.createNode(lvlType, n=setName("upLipRope", idx=str(i).zfill(3)), p=lipsRope_root, ss=True)) oParam, oLength = curve.getCurveParamAtPosition(upRope, cv) uLength = curve.findLenghtFromParam(upRope, oParam) u = uLength / oLength applyop.pathCns( oTransUpV, upRope_upv, cnsType=False, u=u, tangent=False) cns = applyop.pathCns( oTrans, upRope, cnsType=False, u=u, tangent=False) cns.setAttr("worldUpType", 1) cns.setAttr("frontAxis", 0) cns.setAttr("upAxis", 1) pm.connectAttr(oTransUpV.attr("worldMatrix[0]"), cns.attr("worldUpMatrix")) # getting joint parent if head_joint and isinstance(head_joint, (str, string_types)): try: j_parent = pm.PyNode(head_joint) except pm.MayaNodeError: j_parent = False elif head_joint and isinstance(head_joint, pm.PyNode): j_parent = head_joint else: j_parent = False jnt = rigbits.addJnt(oTrans, noReplace=True, parent=j_parent) upperJoints.append(jnt) pm.sets(defset, add=jnt) pm.progressWindow(e=True, endProgress=True) # lower joints lowerJoints = [] cvs = lowCrv.getCVs(space="world") pm.progressWindow(title='Creating Lower Joints', progress=0, max=len(cvs)) for i, cv in enumerate(cvs): pm.progressWindow(e=True, step=1, status='\nCreating Joint for %s' % cv) oTransUpV = pm.PyNode(pm.createNode( lvlType, n=setName("lowLipRopeUpv", idx=str(i).zfill(3)), p=lipsRope_root, ss=True)) oTrans = pm.PyNode(pm.createNode( lvlType, n=setName("lowLipRope", idx=str(i).zfill(3)), p=lipsRope_root, ss=True)) oParam, oLength = curve.getCurveParamAtPosition(lowRope, cv) uLength = curve.findLenghtFromParam(lowRope, oParam) u = uLength / oLength applyop.pathCns(oTransUpV, lowRope_upv, cnsType=False, u=u, tangent=False) cns = applyop.pathCns(oTrans, lowRope, cnsType=False, u=u, tangent=False) cns.setAttr("worldUpType", 1) cns.setAttr("frontAxis", 0) cns.setAttr("upAxis", 1) pm.connectAttr(oTransUpV.attr("worldMatrix[0]"), cns.attr("worldUpMatrix")) # getting joint parent if jaw_joint and isinstance(jaw_joint, (str, string_types)): try: j_parent = pm.PyNode(jaw_joint) except pm.MayaNodeError: pass elif jaw_joint and isinstance(jaw_joint, pm.PyNode): j_parent = jaw_joint else: j_parent = False jnt = rigbits.addJnt(oTrans, noReplace=True, parent=j_parent) lowerJoints.append(jnt) pm.sets(defset, add=jnt) pm.progressWindow(e=True, endProgress=True) ################## # Controls ################## # Controls lists upControls = [] upVec = [] upNpo = [] lowControls = [] lowVec = [] lowNpo = [] # controls options axis_list = ["sx", "sy", "sz", "ro"] upCtlOptions = [["corner", "R", "square", 4, .05, axis_list], ["upOuter", "R", "circle", 14, .03, []], ["upInner", "R", "circle", 14, .03, []], ["upper", "C", "square", 4, .05, axis_list], ["upInner", "L", "circle", 14, .03, []], ["upOuter", "L", "circle", 14, .03, []], ["corner", "L", "square", 4, .05, axis_list]] lowCtlOptions = [["lowOuter", "R", "circle", 14, .03, []], ["lowInner", "R", "circle", 14, .03, []], ["lower", "C", "square", 4, .05, axis_list], ["lowInner", "L", "circle", 14, .03, []], ["lowOuter", "L", "circle", 14, .03, []]] params = ["tx", "ty", "tz", "rx", "ry", "rz"] # upper controls cvs = upCrv_ctl.getCVs(space="world") pm.progressWindow(title='Upper controls', progress=0, max=len(cvs)) v0 = transform.getTransformFromPos(cvs[0]) v1 = transform.getTransformFromPos(cvs[-1]) distSize = vector.getDistance(v0, v1) * 3 for i, cv in enumerate(cvs): pm.progressWindow(e=True, step=1, status='\nCreating control for%s' % cv) t = transform.getTransformFromPos(cv) # Get nearest joint for orientation of controls joints = upperJoints + lowerJoints nearest_joint = None nearest_distance = None for joint in joints: distance = vector.getDistance( transform.getTranslation(joint), cv ) if nearest_distance is None or distance < nearest_distance: nearest_distance = distance nearest_joint = joint if nearest_joint: t = transform.setMatrixPosition( transform.getTransform(nearest_joint), cv ) temp = primitive.addTransform( lips_root, setName("temp"), t ) temp.rx.set(0) t = transform.getTransform(temp) pm.delete(temp) oName = upCtlOptions[i][0] oSide = upCtlOptions[i][1] o_icon = upCtlOptions[i][2] color = upCtlOptions[i][3] wd = upCtlOptions[i][4] oPar = upCtlOptions[i][5] npo = primitive.addTransform(lips_root, setName("%s_npo" % oName, oSide), t) upNpo.append(npo) ctl = icon.create(npo, setName("%s_%s" % (oName, control_name), oSide), t, icon=o_icon, w=wd * distSize, d=wd * distSize, ro=datatypes.Vector(1.57079633, 0, 0), po=datatypes.Vector(0, 0, .07 * distSize), color=color) upControls.append(ctl) name_split = control_name.split("_") if len(name_split) == 2 and name_split[-1] == "ghost": pass else: pm.sets(ctlSet, add=ctl) attribute.addAttribute(ctl, "isCtl", "bool", keyable=False) attribute.setKeyableAttributes(ctl, params + oPar) upv = primitive.addTransform(ctl, setName("%s_upv" % oName, oSide), t) upv.attr("tz").set(FRONT_OFFSET) upVec.append(upv) if oSide == "R": npo.attr("sx").set(-1) pm.progressWindow(e=True, endProgress=True) # lower controls cvs = lowCrv_ctl.getCVs(space="world") pm.progressWindow(title='Lower controls', progress=0, max=len(cvs)) for i, cv in enumerate(cvs[1:-1]): pm.progressWindow(e=True, step=1, status='\nCreating control for%s' % cv) t = transform.getTransformFromPos(cv) # Get nearest joint for orientation of controls joints = upperJoints + lowerJoints nearest_joint = None nearest_distance = None for joint in joints: distance = vector.getDistance( transform.getTranslation(joint), cv ) if nearest_distance is None or distance < nearest_distance: nearest_distance = distance nearest_joint = joint if nearest_joint: t = transform.setMatrixPosition( transform.getTransform(nearest_joint), cv ) temp = primitive.addTransform( lips_root, setName("temp"), t ) temp.rx.set(0) t = transform.getTransform(temp) pm.delete(temp) oName = lowCtlOptions[i][0] oSide = lowCtlOptions[i][1] o_icon = lowCtlOptions[i][2] color = lowCtlOptions[i][3] wd = lowCtlOptions[i][4] oPar = lowCtlOptions[i][5] npo = primitive.addTransform(lips_root, setName("%s_npo" % oName, oSide), t) lowNpo.append(npo) ctl = icon.create(npo, setName("%s_%s" % (oName, control_name), oSide), t, icon=o_icon, w=wd * distSize, d=wd * distSize, ro=datatypes.Vector(1.57079633, 0, 0), po=datatypes.Vector(0, 0, .07 * distSize), color=color) lowControls.append(ctl) name_split = control_name.split("_") if len(name_split) == 2 and control_name.split("_")[-1] == "ghost": pass else: pm.sets(ctlSet, add=ctl) attribute.addAttribute(ctl, "isCtl", "bool", keyable=False) attribute.setKeyableAttributes(ctl, params + oPar) upv = primitive.addTransform(ctl, setName("%s_upv" % oName, oSide), t) upv.attr("tz").set(FRONT_OFFSET) lowVec.append(upv) if oSide == "R": npo.attr("sx").set(-1) pm.progressWindow(e=True, endProgress=True) # reparentig controls pm.parent(upNpo[1], lowNpo[0], upControls[0]) pm.parent(upNpo[2], upNpo[4], upControls[3]) pm.parent(upNpo[-2], lowNpo[-1], upControls[-1]) pm.parent(lowNpo[1], lowNpo[3], lowControls[2]) # Connecting control crvs with controls applyop.gear_curvecns_op(upCrv_ctl, upControls) applyop.gear_curvecns_op(lowCrv_ctl, [upControls[0]] + lowControls + [upControls[-1]]) applyop.gear_curvecns_op(upCrv_upv, upVec) applyop.gear_curvecns_op(lowCrv_upv, [upVec[0]] + lowVec + [upVec[-1]]) # adding wires pm.wire(upCrv, w=upCrv_ctl, dropoffDistance=[0, 1000]) pm.wire(lowCrv, w=lowCrv_ctl, dropoffDistance=[0, 1000]) pm.wire(upRope, w=upCrv_ctl, dropoffDistance=[0, 1000]) pm.wire(lowRope, w=lowCrv_ctl, dropoffDistance=[0, 1000]) pm.wire(upRope_upv, w=upCrv_upv, dropoffDistance=[0, 1000]) pm.wire(lowRope_upv, w=lowCrv_upv, dropoffDistance=[0, 1000]) # setting constrains # up cns_node = pm.parentConstraint(upControls[0], upControls[3], upControls[1].getParent(), mo=True, skipRotate=["x", "y", "z"]) cns_node.attr(upControls[0].name() + "W0").set(.75) cns_node.attr(upControls[3].name() + "W1").set(.25) cns_node.interpType.set(0) # noFlip cns_node = pm.parentConstraint(upControls[0], upControls[3], upControls[2].getParent(), mo=True, skipRotate=["x", "y", "z"]) cns_node.attr(upControls[0].name() + "W0").set(.25) cns_node.attr(upControls[3].name() + "W1").set(.75) cns_node.interpType.set(0) # noFlip cns_node = pm.parentConstraint(upControls[3], upControls[6], upControls[4].getParent(), mo=True, skipRotate=["x", "y", "z"]) cns_node.attr(upControls[3].name() + "W0").set(.75) cns_node.attr(upControls[6].name() + "W1").set(.25) cns_node.interpType.set(0) # noFlip cns_node = pm.parentConstraint(upControls[3], upControls[6], upControls[5].getParent(), mo=True, skipRotate=["x", "y", "z"]) cns_node.attr(upControls[3].name() + "W0").set(.25) cns_node.attr(upControls[6].name() + "W1").set(.75) cns_node.interpType.set(0) # noFlip # low cns_node = pm.parentConstraint(upControls[0], lowControls[2], lowControls[0].getParent(), mo=True, skipRotate=["x", "y", "z"]) cns_node.attr(upControls[0].name() + "W0").set(.75) cns_node.attr(lowControls[2].name() + "W1").set(.25) cns_node.interpType.set(0) # noFlip cns_node = pm.parentConstraint(upControls[0], lowControls[2], lowControls[1].getParent(), mo=True, skipRotate=["x", "y", "z"]) cns_node.attr(upControls[0].name() + "W0").set(.25) cns_node.attr(lowControls[2].name() + "W1").set(.75) cns_node.interpType.set(0) # noFlip cns_node = pm.parentConstraint(lowControls[2], upControls[6], lowControls[3].getParent(), mo=True, skipRotate=["x", "y", "z"]) cns_node.attr(lowControls[2].name() + "W0").set(.75) cns_node.attr(upControls[6].name() + "W1").set(.25) cns_node.interpType.set(0) # noFlip cns_node = pm.parentConstraint(lowControls[2], upControls[6], lowControls[4].getParent(), mo=True, skipRotate=["x", "y", "z"]) cns_node.attr(lowControls[2].name() + "W0").set(.25) cns_node.attr(upControls[6].name() + "W1").set(.75) cns_node.interpType.set(0) # noFlip ########################################### # Connecting rig ########################################### if parent_node: try: if isinstance(parent_node, string_types): parent_node = pm.PyNode(parent_node) parent_node.addChild(lips_root) except pm.MayaNodeError: pm.displayWarning("The Lips rig can not be parent to: %s. Maybe " "this object doesn't exist." % parent_node) if head_joint and jaw_joint: try: if isinstance(head_joint, string_types): head_joint = pm.PyNode(head_joint) except pm.MayaNodeError: pm.displayWarning("Head Joint or Upper Lip Joint %s. Can not be " "fount in the scene" % head_joint) return try: if isinstance(jaw_joint, string_types): jaw_joint = pm.PyNode(jaw_joint) except pm.MayaNodeError: pm.displayWarning("Jaw Joint or Lower Lip Joint %s. Can not be " "fount in the scene" % jaw_joint) return ref_ctls = [head_joint, jaw_joint] if upper_lip_ctl and lower_lip_ctl: try: if isinstance(upper_lip_ctl, string_types): upper_lip_ctl = pm.PyNode(upper_lip_ctl) except pm.MayaNodeError: pm.displayWarning("Upper Lip Ctl %s. Can not be " "fount in the scene" % upper_lip_ctl) return try: if isinstance(lower_lip_ctl, string_types): lower_lip_ctl = pm.PyNode(lower_lip_ctl) except pm.MayaNodeError: pm.displayWarning("Lower Lip Ctl %s. Can not be " "fount in the scene" % lower_lip_ctl) return ref_ctls = [upper_lip_ctl, lower_lip_ctl] # in order to avoid flips lets create a reference transform # also to avoid flips, set any multi target parentConstraint to noFlip ref_cns_list = [] print (ref_ctls) for cns_ref in ref_ctls: t = transform.getTransformFromPos( cns_ref.getTranslation(space='world')) ref = pm.createNode("transform", n=cns_ref.name() + "_cns", p=cns_ref, ss=True) ref.setMatrix(t, worldSpace=True) ref_cns_list.append(ref) # right corner connection cns_node = pm.parentConstraint(ref_cns_list[0], ref_cns_list[1], upControls[0].getParent(), mo=True) cns_node.interpType.set(0) # noFlip # left corner connection cns_node = pm.parentConstraint(ref_cns_list[0], ref_cns_list[1], upControls[-1].getParent(), mo=True) cns_node.interpType.set(0) # noFlip # up control connection cns_node = pm.parentConstraint(ref_cns_list[0], upControls[3].getParent(), mo=True) # low control connection cns_node = pm.parentConstraint(ref_cns_list[1], lowControls[2].getParent(), mo=True) ########################################### # Auto Skinning ########################################### if do_skin: # eyelid vertex rows totalLoops = rigid_loops + falloff_loops vertexLoopList = meshNavigation.getConcentricVertexLoop(vertexList, totalLoops) vertexRowList = meshNavigation.getVertexRowsFromLoops(vertexLoopList) # we set the first value 100% for the first initial loop skinPercList = [1.0] # we expect to have a regular grid topology for r in range(rigid_loops): for rr in range(2): skinPercList.append(1.0) increment = 1.0 / float(falloff_loops) # we invert to smooth out from 100 to 0 inv = 1.0 - increment for r in range(falloff_loops): for rr in range(2): if inv < 0.0: inv = 0.0 skinPercList.append(inv) inv -= increment # this loop add an extra 0.0 indices to avoid errors for r in range(10): for rr in range(2): skinPercList.append(0.0) # base skin if head_joint: try: head_joint = pm.PyNode(head_joint) except pm.MayaNodeError: pm.displayWarning( "Auto skin aborted can not find %s " % head_joint) return # Check if the object has a skinCluster objName = pm.listRelatives(geo, parent=True)[0] skinCluster = skin.getSkinCluster(objName) if not skinCluster: skinCluster = pm.skinCluster(head_joint, geo, tsb=True, nw=2, n='skinClsEyelid') lipsJoints = upperJoints + lowerJoints closestVtxList = upLip_closestVtxList + lowLip_closestVtxList pm.progressWindow(title='Auto skinning process', progress=0, max=len(lipsJoints)) for i, jnt in enumerate(lipsJoints): pm.progressWindow(e=True, step=1, status='\nSkinning %s' % jnt) skinCluster.addInfluence(jnt, weight=0) v = closestVtxList[i] for row in vertexRowList: if v in row: for i, rv in enumerate(row): # find the deformer with max value for each vertex w = pm.skinPercent(skinCluster, rv, query=True, value=True) transJoint = pm.skinPercent(skinCluster, rv, query=True, t=None) max_value = max(w) max_index = w.index(max_value) perc = skinPercList[i] t_value = [(jnt, perc), (transJoint[max_index], 1.0 - perc)] pm.skinPercent(skinCluster, rv, transformValue=t_value) pm.progressWindow(e=True, endProgress=True)
def rope(DEF_nb=10, ropeName="rope", keepRatio=False, lvlType="transform", oSel=None): """Create rope rig based in 2 parallel curves. Args: DEF_nb (int): Number of deformer joints. ropeName (str): Name for the rope rig. keepRatio (bool): If True, the deformers will keep the length position when the curve is stretched. """ if oSel and len(oSel) == 2 and isinstance(oSel, list): oCrv = oSel[0] if isinstance(oCrv, str): oCrv = pm.PyNode(oCrv) oCrvUpV = oSel[1] if isinstance(oCrvUpV, str): oCrvUpV = pm.PyNode(oCrvUpV) else: if len(pm.selected()) != 2: print("You need to select 2 nurbsCurve") return oCrv = pm.selected()[0] oCrvUpV = pm.selected()[1] if (oCrv.getShape().type() != "nurbsCurve" or oCrvUpV.getShape().type() != "nurbsCurve"): print("One of the selected objects is not of type: 'nurbsCurve'") print(oCrv.getShape().type()) print(oCrvUpV.getShape().type()) return if keepRatio: arclen_node = pm.arclen(oCrv, ch=True) alAttr = pm.getAttr(arclen_node + ".arcLength") muldiv_node = pm.createNode("multiplyDivide") pm.connectAttr(arclen_node + ".arcLength", muldiv_node + ".input1X") pm.setAttr(muldiv_node + ".input2X", alAttr) pm.setAttr(muldiv_node + ".operation", 2) pm.addAttr(oCrv, ln="length_ratio", k=True, w=True) pm.connectAttr(muldiv_node + ".outputX", oCrv + ".length_ratio") root = pm.PyNode(pm.createNode(lvlType, n=ropeName + "_root", ss=True)) step = 1.000 / (DEF_nb - 1) i = 0.000 shds = [] for x in range(DEF_nb): oTransUpV = pm.PyNode( pm.createNode(lvlType, n=ropeName + str(x).zfill(3) + "_upv", p=root, ss=True)) oTrans = pm.PyNode( pm.createNode(lvlType, n=ropeName + str(x).zfill(3) + "_lvl", p=root, ss=True)) cnsUpv = applyop.pathCns(oTransUpV, oCrvUpV, cnsType=False, u=i, tangent=False) cns = applyop.pathCns(oTrans, oCrv, cnsType=False, u=i, tangent=False) if keepRatio: muldiv_node2 = pm.createNode("multiplyDivide") condition_node = pm.createNode("condition") pm.setAttr(muldiv_node2 + ".operation", 2) pm.setAttr(muldiv_node2 + ".input1X", i) pm.connectAttr(oCrv + ".length_ratio", muldiv_node2 + ".input2X") pm.connectAttr(muldiv_node2 + ".outputX", condition_node + ".colorIfFalseR") pm.connectAttr(muldiv_node2 + ".outputX", condition_node + ".secondTerm") pm.connectAttr(muldiv_node2 + ".input1X", condition_node + ".colorIfTrueR") pm.connectAttr(muldiv_node2 + ".input1X", condition_node + ".firstTerm") pm.setAttr(condition_node + ".operation", 4) pm.connectAttr(condition_node + ".outColorR", cnsUpv + ".uValue") pm.connectAttr(condition_node + ".outColorR", cns + ".uValue") cns.setAttr("worldUpType", 1) cns.setAttr("frontAxis", 0) cns.setAttr("upAxis", 1) pm.connectAttr(oTransUpV.attr("worldMatrix[0]"), cns.attr("worldUpMatrix")) shd = rigbits.addJnt(oTrans) shds.append(shd[0]) i += step return shds
def addOperators(self): """Create operators and set the relations for the component rig Apply operators, constraints, expressions to the hierarchy. In order to keep the code clean and easier to debug, we shouldn't create any new object in this method. """ # setup out channels. This channels are pass through for stack node.createPlusMinusAverage1D( [self.mid_wide_att, self.mid_wide_in_att, -1.0], 1, self.mid_wide_out_att) node.createPlusMinusAverage1D( [self.mid_depth_att, self.mid_depth_in_att, -1.0], 1, self.mid_depth_out_att) node.createPlusMinusAverage1D( [self.tip_wide_att, self.tip_wide_in_att, -1.0], 1, self.tip_wide_out_att) node.createPlusMinusAverage1D( [self.tip_depth_att, self.tip_depth_in_att, -1.0], 1, self.tip_depth_out_att) node.createPlusMinusAverage1D( [self.mid_twist_att, self.mid_twist_in_att], 1, self.mid_twist_out_att) node.createPlusMinusAverage1D( [self.tip_twist_att, self.tip_twist_in_att], 1, self.tip_twist_out_att) if not self.settings["simpleFK"]: dm_node_scl = node.createDecomposeMatrixNode(self.root.worldMatrix) if self.settings["keepLength"]: arclen_node = pm.arclen(self.mst_crv, ch=True) alAttr = pm.getAttr(arclen_node + ".arcLength") ration_node = node.createMulNode(self.length_ratio_att, alAttr) pm.addAttr(self.mst_crv, ln="length_ratio", k=True, w=True) node.createDivNode(arclen_node.arcLength, ration_node.outputX, self.mst_crv.length_ratio) div_node_scl = node.createDivNode(self.mst_crv.length_ratio, dm_node_scl.outputScaleX) step = 1.000 / (self.def_number - 1) step_mid = 1.000 / ((self.def_number - 1) / 2.0) u = 0.000 u_mid = 0.000 pass_mid = False for i in range(self.def_number): cnsUpv = applyop.pathCns(self.upv_cns[i], self.upv_crv, cnsType=False, u=u, tangent=False) cns = applyop.pathCns(self.div_cns[i], self.mst_crv, False, u, True) # Connecting the scale for scaling compensation # for axis, AX in zip("xyz", "XYZ"): for axis, AX in zip("x", "X"): pm.connectAttr( dm_node_scl.attr("outputScale{}".format(AX)), self.div_cns[i].attr("s{}".format(axis))) if self.settings["keepLength"]: div_node2 = node.createDivNode(u, div_node_scl.outputX) cond_node = node.createConditionNode( div_node2.input1X, div_node2.outputX, 4, div_node2.input1X, div_node2.outputX) pm.connectAttr(cond_node + ".outColorR", cnsUpv + ".uValue") pm.connectAttr(cond_node + ".outColorR", cns + ".uValue") cns.setAttr("worldUpType", 1) cns.setAttr("frontAxis", 0) cns.setAttr("upAxis", 1) pm.connectAttr(self.upv_cns[i].attr("worldMatrix[0]"), cns.attr("worldUpMatrix")) # Connect scale Wide and Depth # wide and Depth mid_mul_node = node.createMulNode( [self.mid_wide_att, self.mid_depth_att], [self.mid_wide_in_att, self.mid_depth_in_att]) mid_mul_node2 = node.createMulNode( [mid_mul_node.outputX, mid_mul_node.outputY], [u_mid, u_mid]) mid_mul_node3 = node.createMulNode( [mid_mul_node2.outputX, mid_mul_node2.outputY], [ dm_node_scl.attr("outputScaleX"), dm_node_scl.attr("outputScaleY") ]) tip_mul_node = node.createMulNode( [self.tip_wide_att, self.tip_depth_att], [self.tip_wide_in_att, self.tip_depth_in_att]) tip_mul_node2 = node.createMulNode( [tip_mul_node.outputX, tip_mul_node.outputY], [u, u]) node.createPlusMinusAverage1D([ mid_mul_node3.outputX, 1.0 - u_mid, tip_mul_node2.outputX, 1.0 - u, -1.0 ], 1, self.div_cns[i].attr("sy")) node.createPlusMinusAverage1D([ mid_mul_node3.outputY, 1.0 - u_mid, tip_mul_node2.outputY, 1.0 - u, -1.0 ], 1, self.div_cns[i].attr("sz")) # Connect Twist "cns.frontTwist" twist_mul_node = node.createMulNode( [self.mid_twist_att, self.tip_twist_att], [u_mid, u]) twist_mul_node2 = node.createMulNode( [self.mid_twist_in_att, self.tip_twist_in_att], [u_mid, u]) node.createPlusMinusAverage1D([ twist_mul_node.outputX, twist_mul_node.outputY, twist_mul_node2.outputX, twist_mul_node2.outputY, ], 1, cns.frontTwist) # u_mid calc if u_mid >= 1.0 or pass_mid: u_mid -= step_mid pass_mid = True else: u_mid += step_mid if u_mid > 1.0: u_mid = 1.0 # ensure the tip is never affected byt the mid if i == (self.def_number - 1): u_mid = 0.0 u += step if self.settings["keepLength"]: # add the safty distance offset self.tweakTip_npo.attr("tx").set(self.off_dist) # connect vis line ref for shp in self.line_ref.getShapes(): pm.connectAttr(self.ikVis_att, shp.attr("visibility")) # CONNECT STACK # master components mstr_global = self.settings["masterChainGlobal"] mstr_local = self.settings["masterChainLocal"] if mstr_global: mstr_global = self.rig.components[mstr_global] if mstr_local: mstr_local = self.rig.components[mstr_local] # connect twist and scale if mstr_global and mstr_local: node.createPlusMinusAverage1D([ mstr_global.root.mid_twist_out, mstr_local.root.mid_twist_out ], 1, self.mid_twist_in_att) node.createPlusMinusAverage1D([ mstr_global.root.tip_twist_out, mstr_local.root.tip_twist_out ], 1, self.tip_twist_in_att) node.createPlusMinusAverage1D([ mstr_global.root.mid_wide_out, mstr_local.root.mid_wide_out, -1 ], 1, self.mid_wide_in_att) node.createPlusMinusAverage1D([ mstr_global.root.tip_wide_out, mstr_local.root.tip_wide_out, -1 ], 1, self.tip_wide_in_att) node.createPlusMinusAverage1D([ mstr_global.root.mid_depth_out, mstr_local.root.mid_depth_out, -1 ], 1, self.mid_depth_in_att) node.createPlusMinusAverage1D([ mstr_global.root.tip_depth_out, mstr_local.root.tip_depth_out, -1 ], 1, self.tip_depth_in_att) elif mstr_local or mstr_global: for master_chain in [mstr_local, mstr_global]: if master_chain: pm.connectAttr(master_chain.root.mid_twist_out, self.mid_twist_in_att) pm.connectAttr(master_chain.root.tip_twist_out, self.tip_twist_in_att) pm.connectAttr(master_chain.root.mid_wide_out, self.mid_wide_in_att) pm.connectAttr(master_chain.root.tip_wide_out, self.tip_wide_in_att) pm.connectAttr(master_chain.root.mid_depth_out, self.mid_depth_in_att) pm.connectAttr(master_chain.root.tip_depth_out, self.tip_depth_in_att) # connect the fk chain ctls for e, ctl in enumerate(self.fk_ctl): # connect out out_loc = self.fk_local_out[e] applyop.gear_mulmatrix_op(ctl.attr("worldMatrix"), out_loc.attr("parentInverseMatrix[0]"), out_loc) out_glob = self.fk_global_out[e] out_ref = self.fk_global_ref[e] applyop.gear_mulmatrix_op(out_ref.attr("worldMatrix"), out_glob.attr("parentInverseMatrix[0]"), out_glob) # connect in global if mstr_global: self.connect_master(mstr_global.fk_global_out, self.fk_global_in, e, self.settings["cnxOffset"]) # connect in local if mstr_local: self.connect_master(mstr_local.fk_local_out, self.fk_local_in, e, self.settings["cnxOffset"]) for shp in ctl.getShapes(): pm.connectAttr(self.fkVis_att, shp.attr("visibility")) for ctl in self.tweak_ctl: for shp in ctl.getShapes(): pm.connectAttr(self.ikVis_att, shp.attr("visibility")) if self.settings["extraTweak"]: for tweak_ctl in self.extratweak_ctl: for shp in tweak_ctl.getShapes(): pm.connectAttr(self.tweakVis_att, shp.attr("visibility"))
def addFkOperator(self, i, rootWorld_node, crv_node): if i == 0 and self.settings["isSplitHip"]: s = self.fk_hip_ctl d = self.fk_local_npo[0], # maintainOffset, skipRotate, skipTranslate _ = pm.parentConstraint(s, d, mo=True, sr=("x", "y", "z"), st=()) s = self.ik_global_out[0] d = self.hip_fk_local_in, # maintainOffset, skipRotate, skipTranslate pm.parentConstraint(s, d, mo=True) # break FK hierarchical orient if i not in [len(self.guide.apos), 0]: s = self.fk_ctl[i - 1] s2 = self.fk_npo[i] d = self.fk_local_npo[i] mulmat_node = applyop.gear_mulmatrix_op(s2.attr("matrix"), s.attr("matrix")) mulmat_node2 = applyop.gear_mulmatrix_op(mulmat_node.attr("output"), s2.attr("inverseMatrix")) dm_node = node.createDecomposeMatrixNode(mulmat_node2 + ".output") pm.connectAttr(dm_node + ".outputTranslate", d.attr("t")) check_list = (pm.Attribute, unicode, str) # noqa cond = pm.createNode("condition") pm.setAttr(cond + ".operation", 4) # greater attribute.connectSet(self.fk_collapsed_att, cond + ".secondTerm", check_list) attribute.connectSet(dm_node + ".outputRotate", cond + ".colorIfTrue", check_list) pm.setAttr(cond + ".colorIfFalseR", 0.) pm.setAttr(cond + ".colorIfFalseG", 0.) pm.setAttr(cond + ".colorIfFalseB", 0.) pm.connectAttr(cond + ".outColor", d.attr("r")) # References if i == 0: # we add extra 10% to the first position u = (1.0 / (len(self.guide.apos) - 1.0)) / 1000 else: u = getCurveUAtPoint(self.slv_crv, self.guide.apos[i]) tmp_div_npo_transform = getTransform(self.div_cns_npo[i]) # to fix mismatch before/after later cns = applyop.pathCns(self.div_cns[i], self.slv_crv, False, u, True) cns.setAttr("frontAxis", 1) # front axis is 'Y' cns.setAttr("upAxis", 0) # front axis is 'X' # Roll # choose ik_ctls for _i, uv in enumerate(self.ik_uv_param): if u < uv: ik_a = self.ik_ctl[_i - 1] ik_b = self.ik_ctl[_i] if self.settings["isSplitHip"] and i == 0: u = (i + 1) / (len(self.guide.apos) - 1.0) ratio = u / uv * .5 else: ratio = u / uv break else: ik_a = self.ik_ctl[-2] ik_b = self.ik_ctl[-1] ratio = 1. intMatrix = applyop.gear_intmatrix_op( ik_a + ".worldMatrix", ik_b + ".worldMatrix", ratio) dm_node = node.createDecomposeMatrixNode(intMatrix + ".output") pm.connectAttr(dm_node + ".outputRotate", self.twister[i].attr("rotate")) pm.parentConstraint(self.twister[i], self.ref_twist[i], maintainOffset=True) pm.connectAttr(self.ref_twist[i] + ".translate", cns + ".worldUpVector") self.div_cns_npo[i].setMatrix(tmp_div_npo_transform, worldSpace=True) # compensate scale reference div_node = node.createDivNode( [1, 1, 1], [rootWorld_node + ".outputScaleX", rootWorld_node + ".outputScaleY", rootWorld_node + ".outputScaleZ"]) # Squash n Stretch op = applyop.gear_squashstretch2_op(self.scl_transforms[i], self.root, pm.arclen(self.slv_crv), "y", div_node + ".output") pm.connectAttr(self.volume_att, op + ".blend") pm.connectAttr(crv_node + ".arcLength", op + ".driver") pm.connectAttr(self.st_att[i], op + ".stretch") pm.connectAttr(self.sq_att[i], op + ".squash") # Controlers tmp_local_npo_transform = getTransform(self.fk_local_npo[i]) # to fix mismatch before/after later if i == 0: mulmat_node = applyop.gear_mulmatrix_op( self.div_cns_npo[i].attr("worldMatrix"), self.root.attr("worldInverseMatrix")) dm_node = node.createDecomposeMatrixNode(mulmat_node + ".output") pm.connectAttr(dm_node + ".outputTranslate", self.fk_npo[i].attr("t")) else: mulmat_node = applyop.gear_mulmatrix_op( self.div_cns_npo[i].attr("worldMatrix"), self.div_cns_npo[i - 1].attr("worldInverseMatrix")) dm_node = node.createDecomposeMatrixNode(mulmat_node + ".output") mul_node = node.createMulNode(div_node + ".output", dm_node + ".outputTranslate") pm.connectAttr(mul_node + ".output", self.fk_npo[i].attr("t")) pm.connectAttr(dm_node + ".outputRotate", self.fk_npo[i].attr("r")) self.addOperatorsOrientationLock(i, cns) self.fk_local_npo[i].setMatrix(tmp_local_npo_transform, worldSpace=True) # References if i < (len(self.fk_ctl) - 1): aim = pm.aimConstraint(self.div_cns_npo[i + 1], self.div_cns_npo[i], maintainOffset=False) pm.setAttr(aim + ".aimVectorX", 0) pm.setAttr(aim + ".aimVectorY", 1) pm.setAttr(aim + ".aimVectorZ", 0) pm.setAttr(aim + ".upVectorX", 0) pm.setAttr(aim + ".upVectorY", 1) pm.setAttr(aim + ".upVectorZ", 0)
def addOperators(self): """Create operators and set the relations for the component rig Apply operators, constraints, expressions to the hierarchy. In order to keep the code clean and easier to debug, we shouldn't create any new object in this method. """ dm_node_scl = node.createDecomposeMatrixNode(self.root.worldMatrix) if self.settings["keepLength"]: arclen_node = pm.arclen(self.mst_crv, ch=True) alAttr = pm.getAttr(arclen_node + ".arcLength") ration_node = node.createMulNode(self.length_ratio_att, alAttr) pm.addAttr(self.mst_crv, ln="length_ratio", k=True, w=True) node.createDivNode(arclen_node.arcLength, ration_node.outputX, self.mst_crv.length_ratio) div_node_scl = node.createDivNode(self.mst_crv.length_ratio, dm_node_scl.outputScaleX) step = 1.000 / (self.def_number - 1) u = 0.000 for i in range(self.def_number): cnsUpv = applyop.pathCns(self.upv_cns[i], self.upv_crv, cnsType=False, u=u, tangent=False) cns = applyop.pathCns(self.div_cns[i], self.mst_crv, False, u, True) # Connectiong the scale for scaling compensation for axis, AX in zip("xyz", "XYZ"): pm.connectAttr(dm_node_scl.attr("outputScale{}".format(AX)), self.div_cns[i].attr("s{}".format(axis))) if self.settings["keepLength"]: div_node2 = node.createDivNode(u, div_node_scl.outputX) cond_node = node.createConditionNode(div_node2.input1X, div_node2.outputX, 4, div_node2.input1X, div_node2.outputX) pm.connectAttr(cond_node + ".outColorR", cnsUpv + ".uValue") pm.connectAttr(cond_node + ".outColorR", cns + ".uValue") cns.setAttr("worldUpType", 1) cns.setAttr("frontAxis", 0) cns.setAttr("upAxis", 1) pm.connectAttr(self.upv_cns[i].attr("worldMatrix[0]"), cns.attr("worldUpMatrix")) u += step if self.settings["keepLength"]: # add the safty distance offset self.tweakTip_npo.attr("tx").set(self.off_dist) # connect vis line ref for shp in self.line_ref.getShapes(): pm.connectAttr(self.ikVis_att, shp.attr("visibility")) for ctl in self.tweak_ctl: for shp in ctl.getShapes(): pm.connectAttr(self.ikVis_att, shp.attr("visibility")) for ctl in self.fk_ctl: for shp in ctl.getShapes(): pm.connectAttr(self.fkVis_att, shp.attr("visibility")) if self.settings["extraTweak"]: for tweak_ctl in self.extratweak_ctl: for shp in tweak_ctl.getShapes(): pm.connectAttr(self.tweakVis_att, shp.attr("visibility"))
def addOperators(self): """Create operators and set the relations for the component rig Apply operators, constraints, expressions to the hierarchy. In order to keep the code clean and easier to debug, we shouldn't create any new object in this method. """ # Curves ------------------------------------------- op = applyop.gear_curveslide2_op( self.slv_crv, self.mst_crv, 0, 1.5, .5, .5) pm.connectAttr(self.position_att, op + ".position") pm.connectAttr(self.lenght_att, op + ".maxstretch") op = applyop.gear_curveslide2_op( self.slv_upv_crv, self.upv_crv, 0, 1.5, .5, .5) pm.connectAttr(self.position_att, op + ".position") pm.connectAttr(self.lenght_att, op + ".maxstretch") for tang in self.tangentsCtl: for shp in tang.getShapes(): pm.connectAttr(self.tangentsVis_att, shp.attr("visibility")) for twnpo, fkctl in zip(self.tweak_npo, self.fk_ctl): intMatrix = applyop.gear_intmatrix_op( fkctl.attr("worldMatrix"), fkctl.getParent().attr("worldMatrix"), .5) applyop.gear_mulmatrix_op(intMatrix.attr("output"), twnpo.attr("parentInverseMatrix[0]"), twnpo) dm_node_scl = node.createDecomposeMatrixNode(self.root.worldMatrix) if self.settings["keepLength"]: arclen_node = pm.arclen(self.slv_crv, ch=True) alAttr = pm.getAttr(arclen_node + ".arcLength") pm.addAttr(self.slv_crv, ln="length_ratio", k=True, w=True) node.createDivNode(arclen_node.arcLength, alAttr, self.slv_crv.length_ratio) div_node_scl = node.createDivNode(self.slv_crv.length_ratio, dm_node_scl.outputScaleX) step = 1.000 / (self.def_number - 1) u = 0.000 for i in range(self.def_number): mult_node = node.createMulNode(u, self.lenght_att) cnsUpv = applyop.pathCns(self.upv_cns[i], self.slv_upv_crv, cnsType=False, u=u, tangent=False) pm.connectAttr(mult_node.outputX, cnsUpv.uValue) cns = applyop.pathCns( self.div_cns[i], self.slv_crv, False, u, True) pm.connectAttr(mult_node.outputX, cns.uValue) # Connectiong the scale for scaling compensation for axis, AX in zip("xyz", "XYZ"): pm.connectAttr(dm_node_scl.attr("outputScale{}".format(AX)), self.div_cns[i].attr("s{}".format(axis))) if self.settings["keepLength"]: div_node2 = node.createDivNode(u, div_node_scl.outputX) cond_node = node.createConditionNode(div_node2.input1X, div_node2.outputX, 4, div_node2.input1X, div_node2.outputX) # pm.connectAttr(cond_node + ".outColorR", # cnsUpv + ".uValue") # pm.connectAttr(cond_node + ".outColorR", # cns + ".uValue") pm.connectAttr(cond_node + ".outColorR", mult_node + ".input1X", f=True) # Connect the scaling for self.Extra_tweak_npo et_npo = self.Extra_tweak_npo[i] pm.connectAttr(self.spin_att, et_npo + ".rz") base_node = node.createMulNode(self.baseSize_att, 1.00000 - u, output=None) tip_node = node.createMulNode(self.tipSize_att, u, output=None) sum_node = node.createPlusMinusAverage1D([base_node.outputX, tip_node.outputX]) # print et_npo pm.connectAttr(sum_node.output1D, et_npo.scaleX, f=True) pm.connectAttr(sum_node.output1D, et_npo.scaleY, f=True) pm.connectAttr(sum_node.output1D, et_npo.scaleZ, f=True) cns.setAttr("worldUpType", 1) cns.setAttr("frontAxis", 0) cns.setAttr("upAxis", 1) pm.connectAttr(self.upv_cns[i].attr("worldMatrix[0]"), cns.attr("worldUpMatrix")) u += step for et in self.Extra_tweak_ctl: for shp in et.getShapes(): pm.connectAttr(self.tweakVis_att, shp.attr("visibility")) if self.settings["keepLength"]: # add the safty distance offset self.tweakTip_npo.attr("tx").set(self.off_dist) # connect vis line ref for shp in self.line_ref.getShapes(): pm.connectAttr(self.ikVis_att, shp.attr("visibility")) for ctl in self.tweak_ctl: for shp in ctl.getShapes(): pm.connectAttr(self.ikVis_att, shp.attr("visibility")) for ctl in self.fk_ctl: for shp in ctl.getShapes(): pm.connectAttr(self.fkVis_att, shp.attr("visibility"))
def addFkOperator(self, i, rootWorld_node, crv_node): fk_local_npo_xfoms = [] if i not in [len(self.guide.apos), 0]: xform = getTransform(self.fk_local_npo[i]) fk_local_npo_xfoms.append(xform) # break FK hierarchical orient if i not in [len(self.guide.apos), 0]: s = self.fk_ctl[i - 1] s2 = self.fk_npo[i] d = self.fk_local_npo[i] mulmat_node = applyop.gear_mulmatrix_op(s2.attr("matrix"), s.attr("matrix")) mulmat_node2 = applyop.gear_mulmatrix_op(mulmat_node.attr("output"), s2.attr("inverseMatrix")) dm_node = node.createDecomposeMatrixNode(mulmat_node2 + ".output") pm.connectAttr(dm_node + ".outputTranslate", d.attr("t")) check_list = (pm.Attribute, unicode, str) # noqa cond = pm.createNode("condition") pm.setAttr(cond + ".operation", 4) # greater attribute.connectSet(self.fk_collapsed_att, cond + ".secondTerm", check_list) attribute.connectSet(dm_node + ".outputRotate", cond + ".colorIfTrue", check_list) pm.setAttr(cond + ".colorIfFalseR", 0.) pm.setAttr(cond + ".colorIfFalseG", 0.) pm.setAttr(cond + ".colorIfFalseB", 0.) pm.connectAttr(cond + ".outColor", d.attr("r")) # References if i == 0: # we add extra 10% to the first position u = (1.0 / (len(self.guide.apos) - 1.0)) / 10000 else: u = getCurveUAtPoint(self.slv_crv, self.guide.apos[i]) tmp_div_npo_transform = getTransform(self.div_cns_npo[i]) # to fix mismatch before/after later cns = applyop.pathCns(self.div_cns[i], self.slv_crv, False, u, True) cns.setAttr("frontAxis", 1) # front axis is 'Y' cns.setAttr("upAxis", 0) # front axis is 'X' # Roll # choose ik_ctls for _i, uv in enumerate(self.ik_uv_param): if u < uv: ik_a = self.ik_ctl[_i - 1] ik_b = self.ik_ctl[_i] roll_a = self.ik_decompose_rot[_i - 1] roll_b = self.ik_decompose_rot[_i] ratio = (uv - u) * (self.settings["ikNb"] - 1) break else: ik_a = self.ik_ctl[-2] ik_b = self.ik_ctl[-1] roll_a = self.ik_decompose_rot[-2] roll_b = self.ik_decompose_rot[-1] ratio = 1. intMatrix = applyop.gear_intmatrix_op( ik_a + ".worldMatrix", ik_b + ".worldMatrix", ratio) dm_node = node.createDecomposeMatrixNode(intMatrix + ".output") # pm.connectAttr(dm_node + ".outputRotate", self.twister[i].attr("rotate")) pm.parentConstraint(self.twister[i], self.ref_twist[i], maintainOffset=True) pm.connectAttr(self.ref_twist[i] + ".translate", cns + ".worldUpVector") self.div_cns_npo[i].setMatrix(tmp_div_npo_transform, worldSpace=True) # rotationdriver roll_ratio = (i + 1.00) / len(self.fk_ctl) mul1 = pm.createNode("multDoubleLinear") pm.connectAttr(roll_a.attr("outRoll"), mul1.attr("input1")) pm.setAttr(mul1.attr("input2"), ratio) mul2 = pm.createNode("multDoubleLinear") pm.connectAttr(roll_b.attr("outRoll"), mul2.attr("input1")) pm.setAttr(mul2.attr("input2"), (1. - ratio)) add = pm.createNode("addDoubleLinear") pm.connectAttr(mul1.attr("output"), add.attr("input1")) pm.connectAttr(mul2.attr("output"), add.attr("input2")) compose_rot = pm.createNode("composeRotate") pm.setAttr(compose_rot.attr("axisOrientX"), 90.0) pm.setAttr(compose_rot.attr("axisOrientZ"), 90.0) pm.connectAttr(add.attr("output"), compose_rot.attr("roll")) pm.connectAttr(compose_rot.attr("outRotate"), self.div_roll_npo[i].attr("rotate")) # compensate scale reference div_node = node.createDivNode( [1, 1, 1], [rootWorld_node + ".outputScaleX", rootWorld_node + ".outputScaleY", rootWorld_node + ".outputScaleZ"]) # Squash n Stretch op = applyop.gear_squashstretch2_op(self.scl_transforms[i], self.root, pm.arclen(self.slv_crv), "y", div_node + ".output") pm.connectAttr(self.volume_att, op + ".blend") pm.connectAttr(crv_node + ".arcLength", op + ".driver") # pm.connectAttr(self.st_att[i], op + ".stretch") # pm.connectAttr(self.sq_att[i], op + ".squash") # Controlers tmp_local_npo_transform = getTransform(self.fk_local_npo[i]) # to fix mismatch before/after later if i == 0: mulmat_node = applyop.gear_mulmatrix_op( self.div_roll_npo[i].attr("worldMatrix"), self.root.attr("worldInverseMatrix")) dm_node = node.createDecomposeMatrixNode(mulmat_node + ".output") pm.connectAttr(dm_node + ".outputTranslate", self.fk_npo[i].attr("t")) elif i != len(self.guide.apos) - 1: mulmat_node = applyop.gear_mulmatrix_op( self.div_roll_npo[i].attr("worldMatrix"), self.div_roll_npo[i - 1].attr("worldInverseMatrix")) dm_node = node.createDecomposeMatrixNode(mulmat_node + ".output") mul_node = node.createMulNode(div_node + ".output", dm_node + ".outputTranslate") pm.connectAttr(mul_node + ".output", self.fk_npo[i].attr("t")) else: pass if i == len(self.guide.apos) - 1: # pm.connectAttr(dm_node + ".outputRotate", self.fk_local_npo2.attr("r")) _ = pm.parentConstraint(self.ik_ctl[-1], self.fk_local_npo2, skipTranslate=("x", "y", "z"), maintainOffset=True) else: pm.connectAttr(dm_node + ".outputRotate", self.fk_npo[i].attr("r")) # self.addOperatorsOrientationLock(i, cns) self.fk_local_npo[i].setMatrix(tmp_local_npo_transform, worldSpace=True) # References if i < (len(self.fk_ctl) - 1): if self.negate: aim = (0., 1., 0.) upv = (0., 0., 1.) else: aim = (0., -1., 0.) upv = (0., 0., -1.) pm.aimConstraint(self.div_cns_npo[i + 1], self.div_cns_npo[i], mo=True, worldUpType="object", worldUpObject=self.fk_upvectors[i], worldUpVector=(0., 1., 0.), aimVector=aim, upVector=upv, )
def addOperators(self): """Create operators and set the relations for the component rig Apply operators, constraints, expressions to the hierarchy. In order to keep the code clean and easier to debug, we shouldn't create any new object in this method. """ # Curves ------------------------------------------- op = applyop.gear_curveslide2_op(self.slv_crv, self.mst_crv, 0, 1.5, .5, .5) pm.connectAttr(self.position_att, op + ".position") pm.connectAttr(self.maxstretch_att, op + ".maxstretch") pm.connectAttr(self.maxsquash_att, op + ".maxsquash") pm.connectAttr(self.softness_att, op + ".softness") # Division ----------------------------------------- rootWorld_node = node.createDecomposeMatrixNode( self.root.attr("worldMatrix")) for i in range(self.settings["fkNb"]): # References u = i / (self.settings["fkNb"] - 1.0) if i == 0: # we add extra 10% to the first position u = (1.0 / (self.settings["fkNb"] - 1.0)) / 10 cns = applyop.pathCns(self.div_cns[i], self.slv_crv, False, u, True) cns.setAttr("frontAxis", 0) # front axis is 'X' cns.setAttr("upAxis", 2) # front axis is 'Z' # Roll intMatrix = applyop.gear_intmatrix_op( self.ik_ctl[0] + ".worldMatrix", self.ik_ctl[-1] + ".worldMatrix", u) dm_node = node.createDecomposeMatrixNode(intMatrix + ".output") pm.connectAttr(dm_node + ".outputRotate", self.twister[i].attr("rotate")) pm.parentConstraint(self.twister[i], self.ref_twist[i], maintainOffset=True) pm.connectAttr(self.ref_twist[i] + ".translate", cns + ".worldUpVector") # compensate scale reference div_node = node.createDivNode([1, 1, 1], [ rootWorld_node + ".outputScaleX", rootWorld_node + ".outputScaleY", rootWorld_node + ".outputScaleZ" ]) # Controlers if i == 0: mulmat_node = applyop.gear_mulmatrix_op( self.div_cns[i].attr("worldMatrix"), self.root.attr("worldInverseMatrix")) dm_node = node.createDecomposeMatrixNode(mulmat_node + ".output") pm.connectAttr(dm_node + ".outputTranslate", self.fk_npo[i].attr("t")) else: mulmat_node = applyop.gear_mulmatrix_op( self.div_cns[i].attr("worldMatrix"), self.div_cns[i - 1].attr("worldInverseMatrix")) dm_node = node.createDecomposeMatrixNode(mulmat_node + ".output") mul_node = node.createMulNode(div_node + ".output", dm_node + ".outputTranslate") pm.connectAttr(mul_node + ".output", self.fk_npo[i].attr("t")) pm.connectAttr(dm_node + ".outputRotate", self.fk_npo[i].attr("r")) # Orientation Lock if i == 0: dm_node = node.createDecomposeMatrixNode(self.ik_ctl[0] + ".worldMatrix") blend_node = node.createBlendNode( [dm_node + ".outputRotate%s" % s for s in "XYZ"], [cns + ".rotate%s" % s for s in "XYZ"], 0) self.div_cns[i].attr("rotate").disconnect() pm.connectAttr(blend_node + ".output", self.div_cns[i] + ".rotate") elif i == self.settings["fkNb"] - 1: dm_node = node.createDecomposeMatrixNode(self.ik_ctl[-1] + ".worldMatrix") blend_node = node.createBlendNode( [dm_node + ".outputRotate%s" % s for s in "XYZ"], [cns + ".rotate%s" % s for s in "XYZ"], 0) self.div_cns[i].attr("rotate").disconnect() pm.connectAttr(blend_node + ".output", self.div_cns[i] + ".rotate")
def addOperators(self): """Create operators and set the relations for the component rig Apply operators, constraints, expressions to the hierarchy. In order to keep the code clean and easier to debug, we shouldn't create any new object in this method. """ # Tangent position --------------------------------- # common part d = vector.getDistance(self.guide.apos[0], self.guide.apos[1]) dist_node = node.createDistNode(self.ik0_ctl, self.ik1_ctl) rootWorld_node = node.createDecomposeMatrixNode( self.root.attr("worldMatrix")) div_node = node.createDivNode(dist_node + ".distance", rootWorld_node + ".outputScaleX") div_node = node.createDivNode(div_node + ".outputX", d) # tan0 mul_node = node.createMulNode(self.tan0_att, self.tan0_npo.getAttr("ty")) res_node = node.createMulNode(mul_node + ".outputX", div_node + ".outputX") pm.connectAttr(res_node + ".outputX", self.tan0_npo.attr("ty")) # tan1 mul_node = node.createMulNode(self.tan1_att, self.tan1_npo.getAttr("ty")) res_node = node.createMulNode(mul_node + ".outputX", div_node + ".outputX") pm.connectAttr(res_node + ".outputX", self.tan1_npo.attr("ty")) # Tangent Mid -------------------------------------- if self.settings["centralTangent"]: tanIntMat = applyop.gear_intmatrix_op( self.tan0_npo.attr("worldMatrix"), self.tan1_npo.attr("worldMatrix"), .5) applyop.gear_mulmatrix_op( tanIntMat.attr("output"), self.tan_npo.attr("parentInverseMatrix[0]"), self.tan_npo) pm.connectAttr(self.tan_ctl.attr("translate"), self.tan0_off.attr("translate")) pm.connectAttr(self.tan_ctl.attr("translate"), self.tan1_off.attr("translate")) # Curves ------------------------------------------- op = applyop.gear_curveslide2_op(self.slv_crv, self.mst_crv, 0, 1.5, .5, .5) pm.connectAttr(self.position_att, op + ".position") pm.connectAttr(self.maxstretch_att, op + ".maxstretch") pm.connectAttr(self.maxsquash_att, op + ".maxsquash") pm.connectAttr(self.softness_att, op + ".softness") # Volume driver ------------------------------------ crv_node = node.createCurveInfoNode(self.slv_crv) # Division ----------------------------------------- for i in range(self.settings["division"]): # References u = i / (self.settings["division"] - 1.0) cns = applyop.pathCns(self.div_cns[i], self.slv_crv, False, u, True) cns.setAttr("frontAxis", 1) # front axis is 'Y' cns.setAttr("upAxis", 0) # front axis is 'X' # Roll intMatrix = applyop.gear_intmatrix_op( self.ik0_ctl + ".worldMatrix", self.ik1_ctl + ".worldMatrix", u) dm_node = node.createDecomposeMatrixNode(intMatrix + ".output") pm.connectAttr(dm_node + ".outputRotate", self.twister[i].attr("rotate")) pm.parentConstraint(self.twister[i], self.ref_twist[i], maintainOffset=True) pm.connectAttr(self.ref_twist[i] + ".translate", cns + ".worldUpVector") # Squash n Stretch op = applyop.gear_squashstretch2_op(self.scl_transforms[i], self.root, pm.arclen(self.slv_crv), "y") pm.connectAttr(self.volume_att, op + ".blend") pm.connectAttr(crv_node + ".arcLength", op + ".driver") pm.connectAttr(self.st_att[i], op + ".stretch") pm.connectAttr(self.sq_att[i], op + ".squash") # Connections (Hooks) ------------------------------ pm.pointConstraint(self.scl_transforms[0], self.cnx0) pm.scaleConstraint(self.scl_transforms[0], self.cnx0) pm.orientConstraint(self.ik0_ctl, self.cnx0) pm.pointConstraint(self.scl_transforms[-1], self.cnx1) pm.scaleConstraint(self.scl_transforms[-1], self.cnx1) pm.orientConstraint(self.ik1_ctl, self.cnx1)
def addOperators(self): """Create operators and set the relations for the component rig Apply operators, constraints, expressions to the hierarchy. In order to keep the code clean and easier to debug, we shouldn't create any new object in this method. """ # Auto bend ---------------------------- if self.settings["autoBend"]: mul_node = node.createMulNode( [self.autoBendChain[0].ry, self.autoBendChain[0].rz], [self.sideBend_att, self.frontBend_att]) mul_node.outputX >> self.ik1autoRot_lvl.rz mul_node.outputY >> self.ik1autoRot_lvl.rx self.ikHandleAutoBend = primitive.addIkHandle( self.autoBend_ctl, self.getName("ikHandleAutoBend"), self.autoBendChain, "ikSCsolver") # Tangent position --------------------------------- # common part d = vector.getDistance(self.guide.apos[1], self.guide.apos[-2]) dist_node = node.createDistNode(self.ik0_ctl, self.ik1_ctl) rootWorld_node = node.createDecomposeMatrixNode( self.root.attr("worldMatrix")) div_node = node.createDivNode(dist_node + ".distance", rootWorld_node + ".outputScaleX") div_node = node.createDivNode(div_node + ".outputX", d) # tan0 mul_node = node.createMulNode(self.tan0_att, self.tan0_npo.getAttr("ty")) res_node = node.createMulNode(mul_node + ".outputX", div_node + ".outputX") pm.connectAttr(res_node + ".outputX", self.tan0_npo.attr("ty")) # tan1 mul_node = node.createMulNode(self.tan1_att, self.tan1_npo.getAttr("ty")) res_node = node.createMulNode(mul_node + ".outputX", div_node + ".outputX") pm.connectAttr(res_node + ".outputX", self.tan1_npo.attr("ty")) # Tangent Mid -------------------------------------- if self.settings["centralTangent"]: tanIntMat = applyop.gear_intmatrix_op( self.tan0_npo.attr("worldMatrix"), self.tan1_npo.attr("worldMatrix"), .5) applyop.gear_mulmatrix_op( tanIntMat.attr("output"), self.tan_npo.attr("parentInverseMatrix[0]"), self.tan_npo) pm.connectAttr(self.tan_ctl.attr("translate"), self.tan0_off.attr("translate")) pm.connectAttr(self.tan_ctl.attr("translate"), self.tan1_off.attr("translate")) # Curves ------------------------------------------- op = applyop.gear_curveslide2_op(self.slv_crv, self.mst_crv, 0, 1.5, .5, .5) pm.connectAttr(self.position_att, op + ".position") pm.connectAttr(self.maxstretch_att, op + ".maxstretch") pm.connectAttr(self.maxsquash_att, op + ".maxsquash") pm.connectAttr(self.softness_att, op + ".softness") # Volume driver ------------------------------------ crv_node = node.createCurveInfoNode(self.slv_crv) # Division ----------------------------------------- tangents = [None, "tan0", "tan1"] for i in range(self.settings["division"]): # References u = i / (self.settings["division"] - 1.0) # check the indx to calculate mid point based on number of division # we want to use the same spine for mannequin and metahuman spine if self.settings["division"] == 4 and i in [1, 2]: u_param = curve.getCurveParamAtPosition( self.slv_crv, self.guide.pos[tangents[i]])[0] cnsType = True elif self.settings["division"] == 3 and i in [1]: u_param = curve.getCurveParamAtPosition( self.slv_crv, self.guide.pos[tangents[i]])[0] cnsType = True else: u_param = u cnsType = False cns = applyop.pathCns(self.div_cns[i], self.slv_crv, cnsType, u_param, True) cns.setAttr("frontAxis", 1) # front axis is 'Y' cns.setAttr("upAxis", 0) # front axis is 'X' # Roll intMatrix = applyop.gear_intmatrix_op( self.ik0_ctl + ".worldMatrix", self.ik1_ctl + ".worldMatrix", u) dm_node = node.createDecomposeMatrixNode(intMatrix + ".output") pm.connectAttr(dm_node + ".outputRotate", self.twister[i].attr("rotate")) pm.parentConstraint(self.twister[i], self.ref_twist[i], maintainOffset=True) pm.connectAttr(self.ref_twist[i] + ".translate", cns + ".worldUpVector") # compensate scale reference div_node = node.createDivNode([1, 1, 1], [ rootWorld_node + ".outputScaleX", rootWorld_node + ".outputScaleY", rootWorld_node + ".outputScaleZ" ]) # Squash n Stretch op = applyop.gear_squashstretch2_op(self.scl_transforms[i], self.root, pm.arclen(self.slv_crv), "y", div_node + ".output") pm.connectAttr(self.volume_att, op + ".blend") pm.connectAttr(crv_node + ".arcLength", op + ".driver") pm.connectAttr(self.st_att[i], op + ".stretch") pm.connectAttr(self.sq_att[i], op + ".squash") # Controlers if i == 0: mulmat_node = applyop.gear_mulmatrix_op( self.div_cns[i].attr("worldMatrix"), self.root.attr("worldInverseMatrix")) dm_node = node.createDecomposeMatrixNode(mulmat_node + ".output") pm.connectAttr(dm_node + ".outputTranslate", self.fk_npo[i].attr("t")) else: mulmat_node = applyop.gear_mulmatrix_op( self.div_cns[i].attr("worldMatrix"), self.div_cns[i - 1].attr("worldInverseMatrix")) dm_node = node.createDecomposeMatrixNode(mulmat_node + ".output") mul_node = node.createMulNode(div_node + ".output", dm_node + ".outputTranslate") pm.connectAttr(mul_node + ".output", self.fk_npo[i].attr("t")) pm.connectAttr(dm_node + ".outputRotate", self.fk_npo[i].attr("r")) # Orientation Lock if i == 0: dm_node = node.createDecomposeMatrixNode(self.ik0_ctl + ".worldMatrix") blend_node = node.createBlendNode( [dm_node + ".outputRotate%s" % s for s in "XYZ"], [cns + ".rotate%s" % s for s in "XYZ"], self.lock_ori0_att) self.div_cns[i].attr("rotate").disconnect() pm.connectAttr(blend_node + ".output", self.div_cns[i] + ".rotate") elif i == self.settings["division"] - 1: dm_node = node.createDecomposeMatrixNode(self.ik1_ctl + ".worldMatrix") blend_node = node.createBlendNode( [dm_node + ".outputRotate%s" % s for s in "XYZ"], [cns + ".rotate%s" % s for s in "XYZ"], self.lock_ori1_att) self.div_cns[i].attr("rotate").disconnect() pm.connectAttr(blend_node + ".output", self.div_cns[i] + ".rotate") # change parent after operators applied pm.parent(self.scl_transforms[-1], self.fk_ctl[-1]) # Connections (Hooks) ------------------------------ pm.parentConstraint(self.pelvis_lvl, self.cnx0) pm.scaleConstraint(self.pelvis_lvl, self.cnx0) transform.matchWorldTransform(self.scl_transforms[-1], self.cnx1) t = transform.setMatrixPosition(transform.getTransform(self.cnx1), self.guide.apos[-1]) self.cnx1.setMatrix(t, worldSpace=True) pm.parentConstraint(self.scl_transforms[-1], self.cnx1, mo=True) pm.scaleConstraint(self.scl_transforms[-1], self.cnx1)