Ejemplo n.º 1
0
        public static MDagPath AddRPIKPole(MDagPath middleDagPath = null)
        {
            if (middleDagPath == null)
            {
                middleDagPath = BasicFunc.GetSelectedDagPath(0);
                if (middleDagPath == null)
                {
                    Debug.Log("please select middle joint");
                    return(null);
                }
            }

            MDagPath   rootDagPath = new MDagPath(), endDagPath = new MDagPath();
            MFnIkJoint middleJoint = new MFnIkJoint(middleDagPath);

            if (middleJoint.parentCount > 0)
            {
                MDagPath.getAPathTo(middleJoint.parent(0), rootDagPath);
                MFnIkJoint rootJoint = new MFnIkJoint(rootDagPath);
                if (middleJoint.childCount > 0)
                {
                    MDagPath.getAPathTo(middleJoint.child(0), endDagPath);
                    MFnIkJoint endJoint  = new MFnIkJoint(endDagPath);
                    MVector    rootPos   = rootJoint.getTranslation(MSpace.Space.kWorld);
                    MVector    middlePos = middleJoint.getTranslation(MSpace.Space.kWorld);
                    MVector    endPos    = endJoint.getTranslation(MSpace.Space.kWorld);


                    //double len0 = (middlePos - rootPos).length;
                    //double len1 = (endPos - middlePos).length;

                    MVector fitLinePos            = (rootPos + endPos) * 0.5;
                    MVector direct_pole           = middlePos - fitLinePos;
                    MVector direct_fitLine        = rootPos - endPos;
                    MVector direct_projectPolePos = BasicFunc.VerticalProject(direct_pole, direct_fitLine).normal;

                    //MVector nmPos = (rootPos * len0 + endPos * len1) * (1 / (len0 + len1));
                    float   factor  = (float)((rootPos - endPos).length / 3);
                    MVector polePos = factor * direct_projectPolePos + middlePos;

                    string locName = "loc_" + rootJoint.name + "_" + endJoint.name;
                    return(BasicFunc.CreateLocator(polePos, locName));
                }
            }
            return(null);
        }
Ejemplo n.º 2
0
        public static bool BindFinger(MDagPath rootJointDagPath, string fingerTag, bool useIK = false)
        {
            MFnIkJoint rootJoint = new MFnIkJoint(rootJointDagPath);

            if (rootJoint.childCount > 0)
            {
                MObject    middleJointObject  = rootJoint.child(0);
                MDagPath   middleJointDagPath = MDagPath.getAPathTo(middleJointObject);
                MFnIkJoint middleJoint        = new MFnIkJoint(middleJointObject);
                if (middleJoint.childCount > 0)
                {
                    MObject  finalJointObject  = middleJoint.child(0);
                    MDagPath finalJointDagPath = MDagPath.getAPathTo(finalJointObject);
                    //MFnIkJoint finalJoint(finalJointObject);
                    //enough, start control
                    return(BindFinger(rootJointDagPath, middleJointDagPath, finalJointDagPath, fingerTag, useIK));
                }
            }
            return(true);
        }
Ejemplo n.º 3
0
        private static Dictionary <string, MayaM2Bone> ExtractJoints(List <MayaM2Sequence> seqList)
        {
            var jointMap = new Dictionary <string, MayaM2Bone>();

            //Goal of iteration : Extract raw joint data and store it in intermediate object, MayaM2Bone
            var processedJoints = new HashSet <string>();

            for (var jointIter = new MItDag(MItDag.TraversalType.kDepthFirst, MFn.Type.kJoint); !jointIter.isDone; jointIter.next())
            {
                var jointPath = new MDagPath();
                jointIter.getPath(jointPath);
                if (processedJoints.Contains(jointPath.fullPathName))
                {
                    continue;
                }
                MGlobal.displayInfo("Extracting raw data of " + jointPath.fullPathName);
                var joint    = new MFnIkJoint(jointPath);
                var mayaBone = new MayaM2Bone();
                jointMap[jointPath.fullPathName] = mayaBone;

                // Hierarchy
                var isRoot = joint.parentCount == 0 || !joint.parent(0).hasFn(MFn.Type.kJoint);
                if (!isRoot)
                {
                    var parentPath = new MDagPath();
                    MDagPath.getAPathTo(joint.parent(0), parentPath);
                    if (!jointMap.ContainsKey(parentPath.fullPathName))
                    {
                        MGlobal.displayError("\tParent is not referenced. Crash incoming. Path : " + parentPath.fullPathName);
                    }
                    jointMap[jointPath.fullPathName].Parent = jointMap[parentPath.fullPathName];
                }
                //Note : M2Bone.submesh_id is wrong in the wiki. Do not try to compute it.
                // Label
                jointMap[jointPath.fullPathName].Type      = MGlobal.executeCommandStringResult("getAttr -asString " + joint.fullPathName + ".type");
                jointMap[jointPath.fullPathName].OtherType = MGlobal.executeCommandStringResult("getAttr -asString " + joint.fullPathName + ".otherType");
                jointMap[jointPath.fullPathName].Side      = MGlobal.executeCommandStringResult("getAttr -asString " + joint.fullPathName + ".side");

                // Base translation is used to compute position
                MAnimControl.currentTime = 0;
                jointMap[jointPath.fullPathName].BaseTranslation = joint.getTranslation(MSpace.Space.kTransform);

                foreach (var seq in seqList)
                {
                    var transData = new List <Tuple <uint, MVector> >();
                    var rotData   = new List <Tuple <uint, MQuaternion> >();
                    var scaleData = new List <Tuple <uint, MVector> >();
                    for (var i = seq.Start; i < seq.End; i += 33) //TODO FIXME What if not multiple of 33 ?
                    {
                        //Get data for this joint for this frame
                        MAnimControl.currentTime = new MTime(i, MTime.Unit.kMilliseconds);

                        var translation = joint.getTranslation(MSpace.Space.kTransform);
                        var rotation    = new MQuaternion();
                        joint.getRotation(rotation, MSpace.Space.kTransform);
                        var scaleArray = new double[3];
                        joint.getScale(scaleArray);
                        var scale = new MVector(scaleArray);

                        if (!translation.isEquivalent(MVector.zero, Epsilon))
                        {
                            var previousIsTheSame = transData.Count > 0 &&
                                                    transData.Last().Item2.isEquivalent(translation, Epsilon);
                            if (!previousIsTheSame)
                            {
                                transData.Add(new Tuple <uint, MVector>((uint)(i - seq.Start), translation));
                            }
                        }
                        if (!rotation.isEquivalent(MQuaternion.identity, Epsilon))
                        {
                            var previousIsTheSame = rotData.Count > 0 &&
                                                    rotData.Last().Item2.isEquivalent(rotation, Epsilon);
                            if (!previousIsTheSame)
                            {
                                rotData.Add(new Tuple <uint, MQuaternion>((uint)(i - seq.Start), rotation));
                            }
                        }
                        if (!scale.isEquivalent(MVector.one, Epsilon))
                        {
                            var previousIsTheSame = scaleData.Count > 0 &&
                                                    scaleData.Last().Item2.isEquivalent(scale, Epsilon);
                            if (!previousIsTheSame)
                            {
                                scaleData.Add(new Tuple <uint, MVector>((uint)(i - seq.Start), scale));
                            }
                        }
                    }
                    if (transData.Count > 0)
                    {
                        jointMap[joint.fullPathName].Translation.Add(transData);
                    }
                    if (rotData.Count > 0)
                    {
                        jointMap[joint.fullPathName].Rotation.Add(rotData);
                    }
                    if (scaleData.Count > 0)
                    {
                        jointMap[joint.fullPathName].Scale.Add(scaleData);
                    }
                }


                processedJoints.Add(jointPath.fullPathName);
            }

            //Goal of iteration : apply transformations to joint data & their children
            processedJoints.Clear();
            for (var jointIter = new MItDag(MItDag.TraversalType.kBreadthFirst, MFn.Type.kJoint);
                 !jointIter.isDone;
                 jointIter.next())
            {
                var jointPath = new MDagPath();
                jointIter.getPath(jointPath);
                if (processedJoints.Contains(jointPath.fullPathName))
                {
                    continue;
                }
                MGlobal.displayInfo("Applying joint orient of " + jointPath.fullPathName);
                var joint       = new MFnIkJoint(jointPath);
                var jointOrient = new MQuaternion();
                joint.getOrientation(jointOrient);

                for (uint i = 0; i < joint.childCount; i++)
                {
                    if (!joint.child(i).hasFn(MFn.Type.kJoint))
                    {
                        continue;
                    }
                    var childFn = new MFnIkJoint(joint.child(i));
                    MGlobal.displayInfo("\tto " + childFn.fullPathName + ";");
                    jointMap[childFn.fullPathName].RotateTranslation(jointOrient);
                }

                processedJoints.Add(jointPath.fullPathName);
            }
            return(jointMap);
        }