public static MDagPath[] BindIKControl(MDagPath rootDagPath, MDagPath endDagPath, JointProcess.IKSolverType iKSolverType = JointProcess.IKSolverType.RotatePlane, MDagPath ctlDagPath = null) { MFnTransform endTrans = new MFnTransform(endDagPath); MDagPath middleDagPath = MDagPath.getAPathTo(endTrans.parent(0)); if (ctlDagPath == null) { ctlDagPath = BasicFunc.AddChildCircle(endDagPath); BasicFunc.UnparentTransform(ctlDagPath); BasicFunc.FreezeTransform(new MFnTransform(ctlDagPath)); } //string resultStr = MGlobal.executeCommandStringResult("ikHandle -sj " + rootObject.fullPathName() + " -ee " + endObject.fullPathName() + " -sol ikRPsolver -n ik_" + rootObject.partialPathName() + "_" + endObject.partialPathName(),true); //string resultStr = MGlobal.executePythonCommandStringResult("cmds.ikHandle(sj='" + rootDagPath.fullPathName + "',ee='" + endDagPath.fullPathName + "',sol='ikRPsolver',n='ik_" + rootDagPath.partialPathName + "_" + endDagPath.partialPathName + "')"); //[u'ik_joint1_joint4', u'effector1'] string[] resultArr = JointProcess.AddIKHandle(rootDagPath, endDagPath, iKSolverType, ctlDagPath.fullPathName); MGlobal.executeCommandStringResult("pointConstraint " + ctlDagPath.fullPathName + " " + resultArr[0]); if (iKSolverType == JointProcess.IKSolverType.RotatePlane) { MDagPath locDagPath = AddRPIKPole(middleDagPath); if (locDagPath != null) { BasicFunc.FreezeTransform(new MFnTransform(locDagPath)); //begin to add constriant BasicFunc.AddConstraint(locDagPath.fullPathName, resultArr[0], ConstantValue.ConstraintType.PoleVector); //string poleConstraintResult = MGlobal.executeCommandStringResult("poleVectorConstraint " + locDagPath.fullPathName + " " + resultArr[0]); //Debug.Log(poleConstraintResult); return(new MDagPath[3] { BasicFunc.GetDagPathByName(resultArr[0]), ctlDagPath, locDagPath }); } } return(new MDagPath[2] { BasicFunc.GetDagPathByName(resultArr[0]), ctlDagPath }); }
public static List <MDagPath> AddBonesCTL(MSelectionList jointList = null, MFnTransform parentTrans = null) { if (jointList == null) { jointList = BasicFunc.GetSelectedList(); } if (parentTrans == null) { parentTrans = new MFnTransform(BasicFunc.CreateEmptyGroup("grp_bonesCTL")); } if (jointList.length == 0) { return(null); } List <MDagPath> jointDags = new List <MDagPath>(); for (int i = 0; i < jointList.length; i++) { MDagPath dag = new MDagPath(); jointList.getDagPath((uint)i, dag); jointDags.Add(dag); } int count = jointDags.Count; MFnTransform[] jointTrans = new MFnTransform[count]; MFnTransform[] jointParentTrans = new MFnTransform[count]; for (int i = 0; i < jointDags.Count; i++) { jointTrans[i] = new MFnTransform(jointDags[i]); jointParentTrans[i] = new MFnTransform(MDagPath.getAPathTo(jointTrans[i].parent(0))); } MVector[] jointWorldPositions = new MVector[jointDags.Count]; MVector centerPos = MVector.zero; for (int i = 0; i < count; i++) { jointWorldPositions[i] = jointTrans[i].getTranslation(MSpace.Space.kWorld); centerPos += jointWorldPositions[i]; } centerPos = centerPos * (1.0f / count); double[] minDist_y = new double[count]; double[] minDist_x = new double[count]; if (count > 1) { for (int i = 0; i < count; i++) { double closestY = double.MaxValue, closestX = double.MaxValue; //int minDistIndex = 0; for (int j = 0; j < count; j++) { if (i == j) { continue; } MVector direct = jointWorldPositions[i] - jointWorldPositions[j]; direct.x = Math.Abs(direct.x); direct.y = Math.Abs(direct.y); if (direct.x >= direct.y) { if (direct.x < closestX) { closestX = direct.x; //minDistIndex = j; } } if (direct.y >= direct.x) { if (direct.y < closestY) { closestY = direct.y; } } } minDist_y[i] = closestY; minDist_x[i] = closestX; } } else { minDist_x[0] = 1; minDist_y[0] = 1; } List <MDagPath> curves = new List <MDagPath>(); for (int i = 0; i < count; i++) { float width = (float)minDist_x[i] / 2, height = (float)minDist_y[i] / 2; MDagPath curve = BasicFunc.CreateCTL_Square("ctl_" + jointDags[i].partialPathName, height, width); MFnTransform curveTrans = new MFnTransform(curve); BasicFunc.SetTransformParent(curveTrans, parentTrans); curveTrans.setTranslation(jointWorldPositions[i] - centerPos, MSpace.Space.kTransform); BasicFunc.FreezeTransform(curveTrans); BasicFunc.SetTranslateLimit(curveTrans, -width / 2, -height / 2, 0, width / 2, height / 2, 0); MPlug plug_curveTX = curveTrans.findPlug(ConstantValue.plugName_tx); MPlug plug_curveTY = curveTrans.findPlug(ConstantValue.plugName_ty); MPlug plug_jointRY = jointParentTrans[i].findPlug(ConstantValue.plugName_ry); MPlug plug_jointRZ = jointParentTrans[i].findPlug(ConstantValue.plugName_rz); BindAttr.ProjectPlug(plug_curveTX, plug_jointRY, -width / 2, width / 2, -45, 45); BindAttr.ProjectPlug(plug_curveTY, plug_jointRZ, -height / 2, height / 2, -45, 45); curves.Add(curve); } return(curves); }