Beispiel #1
0
        private void dynMeshToMayaMesh(string name, Mesh dynMesh = null, TSplineSurface tsMesh = null)
        {
            bool     nodeExists = false;
            MDagPath node       = null;
            Task     unpackTask = null;
            Task     mobjTask   = null;

            try
            {
                node       = DMInterop.getDagNode(name);
                nodeExists = true;
            }
            catch (Exception)
            {
                nodeExists = false;
            }

            MIntArray        faceCnx   = new MIntArray();
            MFloatPointArray verticies = new MFloatPointArray();
            MIntArray        faceVtxCt = new MIntArray();
            int numVert = 0;
            int numPoly = 0;

            if (dynMesh != null)
            {
                numVert    = dynMesh.VertexPositions.Length;
                numPoly    = dynMesh.FaceIndices.Length;
                unpackTask = Task.Factory.StartNew(() => unpackDynMesh(dynMesh, out faceCnx, out verticies, out faceVtxCt));
                unpackTask.Wait(4000);
            }

            if (tsMesh != null)
            {
                numVert    = tsMesh.VerticesCount;
                numPoly    = tsMesh.FacesCount;
                unpackTask = Task.Factory.StartNew(() => unpackTsMesh(tsMesh, out faceCnx, out verticies, out faceVtxCt));
                unpackTask.Wait(4000);
            }



            if (nodeExists)
            {
                try
                {
                    meshFn = new MFnMesh(node);
                    meshFn.createInPlace(numVert, numPoly, verticies, faceVtxCt, faceCnx);
                }
                catch (Exception e)
                {
                    MGlobal.displayWarning(e.Message);
                }
            }
            else
            {
                try
                {
                    dagMod = new MDagModifier();
                    // Create a mesh data wrapper to hold the new geometry.
                    MFnMeshData dataFn      = new MFnMeshData();
                    MObject     dataWrapper = dataFn.create();

                    // Create the mesh geometry and put it into the wrapper.
                    meshFn = new MFnMesh();
                    MObject dataObj   = meshFn.create(numVert, numPoly, verticies, faceVtxCt, faceCnx, dataWrapper);
                    MObject transform = dagMod.createNode("mesh", MObject.kNullObj);

                    dagMod.doIt();

                    renameNodes(transform, name);
                    dagMod.doIt();
                    assignShadingGroup(transform, "initialShadingGroup");
                    dagMod.doIt();
                    setMeshData(transform, dataWrapper);
                }
                catch (Exception e)
                {
                    MGlobal.displayWarning(e.Message);
                }
            }

            //GC.Collect();
            //GC.WaitForPendingFinalizers();
        }