Esempio n. 1
0
        /// <summary>
        /// Get for each vertex the weights for all influence objects, including zero weights.
        /// </summary>
        /// <param name="vertexWeights"></param>
        /// <param name="influenceObjects"></param>
        /// <param name="meshPath"></param>
        private static void GetMeshWeightData(List <MDoubleArray> vertexWeights, MDagPathArray influenceObjects, MDagPath meshPath)
        {
            var fnMesh = new MFnMesh(meshPath);

            // Get any attached skin cluster
            var hasSkinCluster = false;

            // Search the skin cluster affecting this geometry
            var kDepNodeIt = new MItDependencyNodes(MFn.Type.kSkinClusterFilter);

            // Go through each skin cluster in the scene until we find the one connected to this mesh
            while (!kDepNodeIt.isDone && !hasSkinCluster)
            {
                MGlobal.displayInfo("Processing skin cluster...");
                var kObject         = kDepNodeIt.thisNode;
                var kSkinClusterFn  = new MFnSkinCluster(kObject);
                var uiNumGeometries = kSkinClusterFn.numOutputConnections;
                kSkinClusterFn.influenceObjects(influenceObjects);
                MGlobal.displayInfo("\t uiNumGeometries : " + uiNumGeometries);
                MGlobal.displayInfo("\t influenceOBjects number : " + influenceObjects.Count);

                // Go through each connection on the skin cluster until we get the one connecting to this mesh
                MGlobal.displayInfo("Mesh we are looking for : " + fnMesh.fullPathName);
                for (uint uiGeometry = 0; uiGeometry < uiNumGeometries && !hasSkinCluster; uiGeometry++)
                {
                    var uiIndex       = kSkinClusterFn.indexForOutputConnection(uiGeometry);
                    var kInputObject  = kSkinClusterFn.inputShapeAtIndex(uiIndex);
                    var kOutputObject = kSkinClusterFn.outputShapeAtIndex(uiIndex);
                    if (!kOutputObject.hasFn(MFn.Type.kMesh))
                    {
                        continue;
                    }
                    var fnOutput = new MFnMesh(MDagPath.getAPathTo(kOutputObject));
                    MGlobal.displayInfo("Output object : " + fnOutput.fullPathName);

                    if (fnOutput.fullPathName != fnMesh.fullPathName)
                    {
                        continue;
                    }

                    hasSkinCluster = true;
                    MGlobal.displayInfo("\t==> A connected skin cluster has been found.");

                    // Go through each vertex (== each component) and save the weights for each one
                    var kGeometryIt = new MItGeometry(kInputObject);
                    while (!kGeometryIt.isDone)
                    {
                        var  kComponent      = kGeometryIt.currentItem;
                        var  kWeightArray    = new MDoubleArray();
                        uint uiNumInfluences = 0;
                        kSkinClusterFn.getWeights(meshPath, kComponent, kWeightArray, ref uiNumInfluences);
                        vertexWeights.Add(kWeightArray);

                        kGeometryIt.next();
                    }
                }
                kDepNodeIt.next();
            }
        }
        public override void doIt(MArgList args)
        {
            // parse args to get the file name from the command-line
            //
            parseArgs(args);
            uint count = 0;

            // Iterate through graph and search for skinCluster nodes
            //
            MItDependencyNodes iter = new MItDependencyNodes( MFn.Type.kInvalid);
            for ( ; !iter.isDone; iter.next() )
            {
                MObject obj = iter.item;
                if (obj.apiType == MFn.Type.kSkinClusterFilter)
                {
                    count++;

                    // For each skinCluster node, get the list of influence objects
                    //
                    MFnSkinCluster skinCluster = new MFnSkinCluster(obj);
                    MDagPathArray infs = new MDagPathArray();
                    uint nInfs;
                    try
                    {
                        nInfs = skinCluster.influenceObjects(infs);
                    }
                    catch (Exception)
                    {
                        MGlobal.displayInfo("Error getting influence objects.");
                        continue;
                    }
                    if (0 == nInfs)
                    {
                        MGlobal.displayInfo("Error: No influence objects found.");
                        continue;
                    }

                    // loop through the geometries affected by this cluster
                    //
                    uint nGeoms = skinCluster.numOutputConnections;
                    for (uint ii = 0; ii < nGeoms; ++ii)
                    {
                        uint index;
                        try
                        {
                            index = skinCluster.indexForOutputConnection(ii);
                        }
                        catch (Exception)
                        {
                            MGlobal.displayInfo("Error getting geometry index.");
                            continue;
                        }

                        // get the dag path of the ii'th geometry
                        //
                        MDagPath skinPath = new MDagPath();
                        try{
                        skinCluster.getPathAtIndex(index,skinPath);
                        }
                        catch (Exception)
                        {
                            MGlobal.displayInfo("Error getting geometry path.");
                            continue;
                        }

                        // iterate through the components of this geometry
                        //
                        MItGeometry gIter = new MItGeometry(skinPath);

                        // print out the path name of the skin, vertexCount & influenceCount
                        //
                        UnicodeEncoding uniEncoding = new UnicodeEncoding();
                        string res = String.Format("{0} {1} {2}\n",skinPath.partialPathName,gIter.count,nInfs);
                        file.Write(uniEncoding.GetBytes(res),0,uniEncoding.GetByteCount(res));

                        // print out the influence objects
                        //
                        for (int kk = 0; kk < nInfs; ++kk)
                        {
                            res = String.Format("{0} ", infs[kk].partialPathName);
                            file.Write(uniEncoding.GetBytes(res),0,uniEncoding.GetByteCount(res));
                        }
                        res = "\n";
                        file.Write(uniEncoding.GetBytes(res), 0, uniEncoding.GetByteCount(res));

                        for ( /* nothing */ ; !gIter.isDone; gIter.next() ) {
                            MObject comp;
                            try
                            {
                                comp = gIter.component;
                            }
                            catch (Exception)
                            {
                                MGlobal.displayInfo("Error getting geometry path.");
                                continue;
                            }

                            // Get the weights for this vertex (one per influence object)
                            //
                            MDoubleArray wts = new MDoubleArray();
                            uint infCount = 0;
                            try
                            {
                                skinCluster.getWeights(skinPath, comp, wts, ref infCount);
                            }
                            catch (Exception)
                            {
                                displayError("Error getting weights.");
                                continue;
                            }
                            if (0 == infCount)
                            {
                                displayError("Error: 0 influence objects.");
                            }

                            // Output the weight data for this vertex
                            //
                            res = String.Format("{0} ",gIter.index);
                            file.Write(uniEncoding.GetBytes(res), 0, uniEncoding.GetByteCount(res));
                            for (int jj = 0; jj < infCount ; ++jj )
                            {
                                res = String.Format("{0} ", wts[jj]);
                                file.Write(uniEncoding.GetBytes(res), 0, uniEncoding.GetByteCount(res));
                            }
                            file.Write(uniEncoding.GetBytes("\n"), 0, uniEncoding.GetByteCount("\n"));
                        }
                    }
                }
            }

            if (0 == count)
            {
                displayError("No skinClusters found in this scene.");
            }
            file.Close();
            return;
        }
        public override void doIt(MArgList args)
        {
            // parse args to get the file name from the command-line
            //
            parseArgs(args);
            uint count = 0;


            // Iterate through graph and search for skinCluster nodes
            //
            MItDependencyNodes iter = new MItDependencyNodes(MFn.Type.kInvalid);

            for ( ; !iter.isDone; iter.next())
            {
                MObject obj = iter.item;
                if (obj.apiType == MFn.Type.kSkinClusterFilter)
                {
                    count++;

                    // For each skinCluster node, get the list of influence objects
                    //
                    MFnSkinCluster skinCluster = new MFnSkinCluster(obj);
                    MDagPathArray  infs        = new MDagPathArray();
                    uint           nInfs;
                    try
                    {
                        nInfs = skinCluster.influenceObjects(infs);
                    }
                    catch (Exception)
                    {
                        MGlobal.displayInfo("Error getting influence objects.");
                        continue;
                    }
                    if (0 == nInfs)
                    {
                        MGlobal.displayInfo("Error: No influence objects found.");
                        continue;
                    }

                    // loop through the geometries affected by this cluster
                    //
                    uint nGeoms = skinCluster.numOutputConnections;
                    for (uint ii = 0; ii < nGeoms; ++ii)
                    {
                        uint index;
                        try
                        {
                            index = skinCluster.indexForOutputConnection(ii);
                        }
                        catch (Exception)
                        {
                            MGlobal.displayInfo("Error getting geometry index.");
                            continue;
                        }

                        // get the dag path of the ii'th geometry
                        //
                        MDagPath skinPath = new MDagPath();
                        try{
                            skinCluster.getPathAtIndex(index, skinPath);
                        }
                        catch (Exception)
                        {
                            MGlobal.displayInfo("Error getting geometry path.");
                            continue;
                        }

                        // iterate through the components of this geometry
                        //
                        MItGeometry gIter = new MItGeometry(skinPath);

                        // print out the path name of the skin, vertexCount & influenceCount
                        //
                        UnicodeEncoding uniEncoding = new UnicodeEncoding();
                        string          res         = String.Format("{0} {1} {2}\n", skinPath.partialPathName, gIter.count, nInfs);
                        file.Write(uniEncoding.GetBytes(res), 0, uniEncoding.GetByteCount(res));

                        // print out the influence objects
                        //
                        for (int kk = 0; kk < nInfs; ++kk)
                        {
                            res = String.Format("{0} ", infs[kk].partialPathName);
                            file.Write(uniEncoding.GetBytes(res), 0, uniEncoding.GetByteCount(res));
                        }
                        res = "\n";
                        file.Write(uniEncoding.GetBytes(res), 0, uniEncoding.GetByteCount(res));

                        for (/* nothing */; !gIter.isDone; gIter.next())
                        {
                            MObject comp;
                            try
                            {
                                comp = gIter.component;
                            }
                            catch (Exception)
                            {
                                MGlobal.displayInfo("Error getting geometry path.");
                                continue;
                            }


                            // Get the weights for this vertex (one per influence object)
                            //
                            MDoubleArray wts      = new MDoubleArray();
                            uint         infCount = 0;
                            try
                            {
                                skinCluster.getWeights(skinPath, comp, wts, ref infCount);
                            }
                            catch (Exception)
                            {
                                displayError("Error getting weights.");
                                continue;
                            }
                            if (0 == infCount)
                            {
                                displayError("Error: 0 influence objects.");
                            }

                            // Output the weight data for this vertex
                            //
                            res = String.Format("{0} ", gIter.index);
                            file.Write(uniEncoding.GetBytes(res), 0, uniEncoding.GetByteCount(res));
                            for (int jj = 0; jj < infCount; ++jj)
                            {
                                res = String.Format("{0} ", wts[jj]);
                                file.Write(uniEncoding.GetBytes(res), 0, uniEncoding.GetByteCount(res));
                            }
                            file.Write(uniEncoding.GetBytes("\n"), 0, uniEncoding.GetByteCount("\n"));
                        }
                    }
                }
            }

            if (0 == count)
            {
                displayError("No skinClusters found in this scene.");
            }
            file.Close();
            return;
        }