Beispiel #1
0
        protected MObject createMesh(MTime time, ref MObject outData)
        {
            int              numVertices, frame;
            float            cubeSize;
            MFloatPointArray points = new MFloatPointArray();
            MFnMesh          meshFS = new MFnMesh();

            // Scale the cube on the frame number, wrap every 10 frames.
            frame = (int)time.asUnits(MTime.Unit.kFilm);
            if (frame == 0)
            {
                frame = 1;
            }
            cubeSize = 0.5f * (float)(frame % 10);

            const int numFaces = 6;

            numVertices = 8;

            MFloatPoint vtx_1 = new MFloatPoint(-cubeSize, -cubeSize, -cubeSize);
            MFloatPoint vtx_2 = new MFloatPoint(cubeSize, -cubeSize, -cubeSize);
            MFloatPoint vtx_3 = new MFloatPoint(cubeSize, -cubeSize, cubeSize);
            MFloatPoint vtx_4 = new MFloatPoint(-cubeSize, -cubeSize, cubeSize);
            MFloatPoint vtx_5 = new MFloatPoint(-cubeSize, cubeSize, -cubeSize);
            MFloatPoint vtx_6 = new MFloatPoint(-cubeSize, cubeSize, cubeSize);
            MFloatPoint vtx_7 = new MFloatPoint(cubeSize, cubeSize, cubeSize);
            MFloatPoint vtx_8 = new MFloatPoint(cubeSize, cubeSize, -cubeSize);

            points.append(vtx_1);
            points.append(vtx_2);
            points.append(vtx_3);
            points.append(vtx_4);
            points.append(vtx_5);
            points.append(vtx_6);
            points.append(vtx_7);
            points.append(vtx_8);


            // Set up an array containing the number of vertices
            // for each of the 6 cube faces (4 verticies per face)
            //
            int[]     face_counts = { 4, 4, 4, 4, 4, 4 };
            MIntArray faceCounts  = new MIntArray(face_counts);

            // Set up and array to assign vertices from points to each face
            //
            int[]     face_connects = { 0, 1, 2, 3,
                                        4,     5, 6, 7,
                                        3,     2, 6, 5,
                                        0,     3, 5, 4,
                                        0,     4, 7, 1,
                                        1,     7, 6, 2 };
            MIntArray faceConnects = new MIntArray(face_connects);

            MObject newMesh = meshFS.create(numVertices, numFaces, points, faceCounts, faceConnects, outData);

            return(newMesh);
        }
 /// <summary>
 /// [x,y,z]
 /// </summary>
 /// <param name="mFloatPoint"></param>
 /// <returns></returns>
 public static float[] toArray(this MFloatPoint mFloatPoint)
 {
     float[] array = new float[3];
     for (uint index = 0; index < 3; index++)
     {
         array[index] = mFloatPoint[index];
     }
     return(array);
 }
Beispiel #3
0
        protected MObject createMesh(MTime time, ref MObject outData)
        {
            int numVertices, frame;
            float cubeSize;
            MFloatPointArray points = new MFloatPointArray();
            MFnMesh meshFS = new MFnMesh();

            // Scale the cube on the frame number, wrap every 10 frames.
            frame = (int)time.asUnits(MTime.Unit.kFilm);
            if (frame == 0)
                frame = 1;
            cubeSize = 0.5f * (float)(frame % 10);

            const int numFaces = 6;
            numVertices = 8;

            MFloatPoint vtx_1 = new MFloatPoint(-cubeSize, -cubeSize, -cubeSize);
            MFloatPoint vtx_2 = new MFloatPoint(cubeSize, -cubeSize, -cubeSize);
            MFloatPoint vtx_3 = new MFloatPoint(cubeSize, -cubeSize, cubeSize);
            MFloatPoint vtx_4 = new MFloatPoint(-cubeSize, -cubeSize, cubeSize);
            MFloatPoint vtx_5 = new MFloatPoint(-cubeSize, cubeSize, -cubeSize);
            MFloatPoint vtx_6 = new MFloatPoint(-cubeSize, cubeSize, cubeSize);
            MFloatPoint vtx_7 = new MFloatPoint(cubeSize, cubeSize, cubeSize);
            MFloatPoint vtx_8 = new MFloatPoint(cubeSize, cubeSize, -cubeSize);
            points.append(vtx_1);
            points.append(vtx_2);
            points.append(vtx_3);
            points.append(vtx_4);
            points.append(vtx_5);
            points.append(vtx_6);
            points.append(vtx_7);
            points.append(vtx_8);


            // Set up an array containing the number of vertices
            // for each of the 6 cube faces (4 verticies per face)
            //
            int[] face_counts = { 4, 4, 4, 4, 4, 4 };
            MIntArray faceCounts = new MIntArray(face_counts);

            // Set up and array to assign vertices from points to each face 
            //
            int[] face_connects = {	0, 1, 2, 3,
									4, 5, 6, 7,
									3, 2, 6, 5,
									0, 3, 5, 4,
									0, 4, 7, 1,
									1, 7, 6, 2	};
            MIntArray faceConnects = new MIntArray(face_connects);

            MObject newMesh = meshFS.create(numVertices, numFaces, points, faceCounts, faceConnects, outData);

            return newMesh;
        }
Beispiel #4
0
        private static void unpackDynMesh(Mesh dynMesh, out MIntArray faceCnx, out MFloatPointArray verticies, out MIntArray faceVtxCt)
        {
            MIntArray        m_faceCnx   = new MIntArray();
            MFloatPointArray m_verticies = new MFloatPointArray();
            MIntArray        m_faceVtxCt = new MIntArray();

            MFloatPoint vtxToAdd = new MFloatPoint();


            foreach (var vtx in dynMesh.VertexPositions)
            {
                if (MGlobal.isZAxisUp)
                {
                    vtxToAdd.x = (float)vtx.X;
                    vtxToAdd.y = (float)vtx.Y;
                    vtxToAdd.z = (float)vtx.Z;
                }
                else
                {
                    vtxToAdd.x = (float)vtx.X;
                    vtxToAdd.y = (float)vtx.Z;
                    vtxToAdd.z = -(float)vtx.Y;
                }

                m_verticies.Add(vtxToAdd);
            }

            foreach (var fidx in dynMesh.FaceIndices)
            {
                int vtxCt = (int)fidx.Count;
                m_faceVtxCt.Add(vtxCt);
                if (vtxCt == 3)
                {
                    m_faceCnx.Add((int)fidx.A);
                    m_faceCnx.Add((int)fidx.B);
                    m_faceCnx.Add((int)fidx.C);
                }
                else
                {
                    m_faceCnx.Add((int)fidx.A);
                    m_faceCnx.Add((int)fidx.B);
                    m_faceCnx.Add((int)fidx.C);
                    m_faceCnx.Add((int)fidx.D);
                }
            }

            verticies = m_verticies;
            faceCnx   = m_faceCnx;
            faceVtxCt = m_faceVtxCt;
        }
Beispiel #5
0
        private void create_dodecahedron()
        {
            // Generated from icosahedron points.
            create_icosa_points();
            MFloatPoint[] my_info = new MFloatPoint[12];
            int           idx;

            for (idx = 0; idx < 12; idx++)
            {
                my_info[idx]   = new MFloatPoint();
                my_info[idx].x = iarr[idx].x;
                my_info[idx].y = iarr[idx].y;
                my_info[idx].z = iarr[idx].z;
            }

            iarr.clear();

            // now generate the dodecahedron points:
            double x1, y1, z1, x2, y2, z2, x3, y3, z3;
            double xf, yf, zf;
            double len;

            for (idx = 0; idx < 20; idx++)
            {
                x1 = my_info[icosa_gons[3 * idx] - 1].x;
                y1 = my_info[icosa_gons[3 * idx] - 1].y;
                z1 = my_info[icosa_gons[3 * idx] - 1].z;

                x2 = my_info[icosa_gons[3 * idx + 1] - 1].x;
                y2 = my_info[icosa_gons[3 * idx + 1] - 1].y;
                z2 = my_info[icosa_gons[3 * idx + 1] - 1].z;

                x3 = my_info[icosa_gons[3 * idx + 2] - 1].x;
                y3 = my_info[icosa_gons[3 * idx + 2] - 1].y;
                z3 = my_info[icosa_gons[3 * idx + 2] - 1].z;

                // the docecahedron vertex is the average of these points.
                xf = (x1 + x2 + x3) / 3.0;
                yf = (y1 + y2 + y3) / 3.0;
                zf = (z1 + z2 + z3) / 3.0;

                // One more transformation: scale this point so it lies on the
                // unit sphere...
                len = Math.Sqrt(xf * xf + yf * yf + zf * zf);
                xf /= len; yf /= len; zf /= len;

                FILL(xf, yf, zf);
            }
        }
Beispiel #6
0
        public MObject ConvertMeshMaya(TriMesh triMesh)
        {
            MFnMesh meshMaya = new MFnMesh();

            int verticeNum          = triMesh.Vertices.Count;
            MFloatPointArray points = new MFloatPointArray();

            for (int i = 0; i < verticeNum; i++)
            {
                float x = (float)triMesh.Vertices[i].Traits.Position.x;
                float y = (float)triMesh.Vertices[i].Traits.Position.y;
                float z = (float)triMesh.Vertices[i].Traits.Position.z;

                MFloatPoint vertex = new MFloatPoint(x, y, z);
                points.append(vertex);
            }


            int faceNum = triMesh.Faces.Count;

            int[] face_Counts = new int[faceNum];
            for (int i = 0; i < faceNum; i++)
            {
                face_Counts[i] = 3;
            }
            MIntArray faceCounts = new MIntArray(face_Counts);


            int[] faceTopology = new int[faceNum * 3];

            for (int j = 0; j < faceNum; j++)
            {
                int index = j * 3;
                faceTopology[index] = triMesh.Faces[j].GetVertex(0).Index;

                faceTopology[index + 1] = triMesh.Faces[j].GetVertex(1).Index;

                faceTopology[index + 2] = triMesh.Faces[j].GetVertex(2).Index;
            }

            MIntArray faceConnects = new MIntArray(faceTopology);

            MObject mesh = meshMaya.create(verticeNum, faceNum, points, faceCounts, faceConnects);

            return(mesh);
        }
Beispiel #7
0
        private static void unpackTsMesh(TSplineSurface tsMesh, out MIntArray faceCnx, out MFloatPointArray verticies, out MIntArray faceVtxCt)
        {
            MIntArray        m_faceCnx   = new MIntArray();
            MFloatPointArray m_verticies = new MFloatPointArray();
            MIntArray        m_faceVtxCt = new MIntArray();

            MFloatPoint vtxToAdd = new MFloatPoint();

            var tsMeshCompress = tsMesh.CompressIndexes();

            foreach (var vtx in tsMeshCompress.Vertices)
            {
                if (MGlobal.isZAxisUp)
                {
                    vtxToAdd.x = (float)vtx.PointGeometry.X;
                    vtxToAdd.y = (float)vtx.PointGeometry.Y;
                    vtxToAdd.z = (float)vtx.PointGeometry.Z;
                }
                else
                {
                    vtxToAdd.x = (float)vtx.PointGeometry.X;
                    vtxToAdd.y = (float)vtx.PointGeometry.Z;
                    vtxToAdd.z = -(float)vtx.PointGeometry.Y;
                }

                m_verticies.Add(vtxToAdd);
            }

            foreach (var fidx in tsMeshCompress.Faces)
            {
                int vtxCt = fidx.Vertices.Length;
                m_faceVtxCt.Add(vtxCt);

                foreach (var fVert in fidx.Vertices)
                {
                    m_faceCnx.Add(fVert.Index);
                }
            }
            verticies = m_verticies;
            faceCnx   = m_faceCnx;
            faceVtxCt = m_faceVtxCt;
        }
Beispiel #8
0
        public static MayaM2Vertex Create(MFloatPoint pos, MFloatVector norm, List <Tuple <float, float> > uv,
                                          List <Tuple <MayaM2Bone, double> > weights, List <MayaM2Vertex> globalVertexList)
        {
            var compositeKey = new Tuple <MFloatPoint, MFloatVector, List <Tuple <float, float> > >(
                pos,
                norm,
                uv);

            if (_instances.ContainsKey(compositeKey))
            {
                return(_instances[compositeKey]);
            }
            var vert = new MayaM2Vertex
            {
                Position      = pos,
                Normal        = norm,
                UvCoordinates = uv,
                Weights       = weights
            };

            _instances[compositeKey] = vert;
            globalVertexList.Add(vert);
            return(vert);
        }
 public static Vector3 ToVector3(this MFloatPoint floatPoint)
 {
     return(new Vector3(floatPoint.x, floatPoint.y, floatPoint.z));
 }
Beispiel #10
0
        public void Load(string name)
        {
            List <StaticObjectVertex> vertices = GetVertices();
            List <uint> indices = GetIndices();

            MIntArray        polygonIndexCounts = new MIntArray((uint)indices.Count / 3);
            MIntArray        polygonIndices     = new MIntArray((uint)indices.Count);
            MFloatPointArray meshVertices       = new MFloatPointArray((uint)vertices.Count);
            MFloatArray      arrayU             = new MFloatArray((uint)vertices.Count);
            MFloatArray      arrayV             = new MFloatArray((uint)vertices.Count);
            MFnMesh          mesh        = new MFnMesh();
            MDagPath         meshDagPath = new MDagPath();
            MDGModifier      modifier    = new MDGModifier();
            MFnSet           set         = new MFnSet();

            for (int i = 0; i < indices.Count / 3; i++)
            {
                polygonIndexCounts[i] = 3;
            }

            for (int i = 0; i < indices.Count; i++)
            {
                polygonIndices[i] = (int)indices[i];
            }

            for (int i = 0; i < vertices.Count; i++)
            {
                StaticObjectVertex vertex = vertices[i];

                meshVertices[i] = new MFloatPoint(vertex.Position.X, vertex.Position.Y, vertex.Position.Z);
                arrayU[i]       = vertex.UV.X;
                arrayV[i]       = 1 - vertex.UV.Y;
            }

            //Assign mesh data
            mesh.create(vertices.Count, indices.Count / 3, meshVertices, polygonIndexCounts, polygonIndices, arrayU, arrayV, MObject.kNullObj);
            mesh.getPath(meshDagPath);
            mesh.assignUVs(polygonIndexCounts, polygonIndices);

            //Set names
            mesh.setName(name);
            MFnTransform transformNode = new MFnTransform(mesh.parent(0));

            transformNode.setName("transform_" + name);

            //Get render partition
            MFnPartition renderPartition = MayaHelper.FindRenderPartition();

            //Create Materials
            uint startIndex = 0;

            for (int i = 0; i < this.Submeshes.Count; i++)
            {
                MFnDependencyNode   dependencyNode = new MFnDependencyNode();
                MFnLambertShader    lambertShader  = new MFnLambertShader();
                StaticObjectSubmesh submesh        = this.Submeshes[i];

                lambertShader.create(true);
                lambertShader.setName(submesh.Name);
                lambertShader.color = MaterialProvider.GetMayaColor(i);

                MObject shadingEngine = dependencyNode.create("shadingEngine", submesh.Name + "_SG");
                MObject materialInfo  = dependencyNode.create("materialInfo", submesh.Name + "_MaterialInfo");
                MPlug   partitionPlug = new MFnDependencyNode(shadingEngine).findPlug("partition");
                MPlug   setsPlug      = MayaHelper.FindFirstNotConnectedElement(renderPartition.findPlug("sets"));
                modifier.connect(partitionPlug, setsPlug);

                MPlug outColorPlug      = lambertShader.findPlug("outColor");
                MPlug surfaceShaderPlug = new MFnDependencyNode(shadingEngine).findPlug("surfaceShader");
                modifier.connect(outColorPlug, surfaceShaderPlug);

                MPlug messagePlug      = new MFnDependencyNode(shadingEngine).findPlug("message");
                MPlug shadingGroupPlug = new MFnDependencyNode(materialInfo).findPlug("shadingGroup");
                modifier.connect(messagePlug, shadingGroupPlug);

                modifier.doIt();

                MFnSingleIndexedComponent component = new MFnSingleIndexedComponent();
                MObject   faceComponent             = component.create(MFn.Type.kMeshPolygonComponent);
                MIntArray groupPolygonIndices       = new MIntArray();
                uint      endIndex = (startIndex + (uint)submesh.Indices.Count) / 3;
                for (uint j = startIndex / 3; j < endIndex; j++)
                {
                    groupPolygonIndices.append((int)j);
                }
                component.addElements(groupPolygonIndices);

                set.setObject(shadingEngine);
                set.addMember(meshDagPath, faceComponent);

                startIndex += (uint)submesh.Indices.Count;
            }

            mesh.updateSurface();
        }
Beispiel #11
0
 public static C3Vector AxisInvert(MFloatPoint point) => AxisInvert(point.x, point.y, point.z);
Beispiel #12
0
		private void create_dodecahedron()
		{
			// Generated from icosahedron points.
			create_icosa_points();
			MFloatPoint[] my_info = new MFloatPoint[12];
			int idx;
			for( idx = 0; idx < 12; idx++ ) {
				my_info[idx] = new MFloatPoint();
				my_info[idx].x = iarr[idx].x;
				my_info[idx].y = iarr[idx].y;
				my_info[idx].z = iarr[idx].z;
			}

			iarr.clear();

			// now generate the dodecahedron points:
			double x1,y1,z1,x2,y2,z2,x3,y3,z3;
			double xf, yf, zf;
			double len;

			for( idx = 0; idx < 20; idx++ ) {
				x1 = my_info[ icosa_gons[3*idx]-1 ].x;
				y1 = my_info[ icosa_gons[3*idx]-1 ].y;
				z1 = my_info[ icosa_gons[3*idx]-1 ].z;

				x2 = my_info[ icosa_gons[3*idx + 1]-1 ].x;
				y2 = my_info[ icosa_gons[3*idx + 1]-1 ].y;
				z2 = my_info[ icosa_gons[3*idx + 1]-1 ].z;

				x3 = my_info[ icosa_gons[3*idx + 2]-1 ].x;
				y3 = my_info[ icosa_gons[3*idx + 2]-1 ].y;
				z3 = my_info[ icosa_gons[3*idx + 2]-1 ].z;

				// the docecahedron vertex is the average of these points.
				xf = (x1+x2+x3)/3.0;
				yf = (y1+y2+y3)/3.0;
				zf = (z1+z2+z3)/3.0;

				// One more transformation: scale this point so it lies on the
				// unit sphere...
				len = Math.Sqrt( xf*xf + yf*yf + zf*zf );
				xf /= len; yf /= len; zf /= len;

				FILL( xf, yf, zf );
			}
		}
Beispiel #13
0
		private void FILL( double x, double y, double z )
		{
			MFloatPoint pnt = new MFloatPoint( (float)x, (float)y, (float)z );
			iarr.append( pnt );
		}
Beispiel #14
0
        public static bool ToMayaOLD(Mesh MeshToSend, string name)
        {
            bool     nodeExists = false;
            MDagPath node       = null;
            Task     checkNode  = null;
            Task     mobjMesh   = null;

            try
            {
                checkNode = Task.Factory.StartNew(() => node = DMInterop.getDagNode(name));
                checkNode.Wait(500);

                nodeExists = true;
            }
            catch (Exception)
            {
                nodeExists = false;
            }
            MFnMesh mfnMesh;

            if (nodeExists)
            {
                mfnMesh = new MFnMesh(node);
            }
            else
            {
                mfnMesh = new MFnMesh();
            }

            //unpack geom
            int numVert = MeshToSend.VertexPositions.Length;
            int numPoly = MeshToSend.FaceIndices.Length;

            MIntArray        faceCnx   = new MIntArray();
            MFloatPointArray verticies = new MFloatPointArray();
            MIntArray        faceVtxCt = new MIntArray();

            MFloatPoint vtxToAdd = new MFloatPoint();

            foreach (var vtx in MeshToSend.VertexPositions)
            {
                if (MGlobal.isZAxisUp)
                {
                    vtxToAdd.x = (float)vtx.X;
                    vtxToAdd.y = (float)vtx.Y;
                    vtxToAdd.z = (float)vtx.Z;
                }
                else
                {
                    vtxToAdd.x = (float)vtx.X;
                    vtxToAdd.y = (float)vtx.Z;
                    vtxToAdd.z = -(float)vtx.Y;
                }

                verticies.Add(vtxToAdd);
            }

            foreach (var fidx in MeshToSend.FaceIndices)
            {
                int vtxCt = (int)fidx.Count;
                faceVtxCt.Add(vtxCt);
                if (vtxCt == 3)
                {
                    faceCnx.Add((int)fidx.A);
                    faceCnx.Add((int)fidx.B);
                    faceCnx.Add((int)fidx.C);
                }
                else
                {
                    faceCnx.Add((int)fidx.A);
                    faceCnx.Add((int)fidx.B);
                    faceCnx.Add((int)fidx.C);
                    faceCnx.Add((int)fidx.D);
                }
            }

            //create maya mesh
            if (nodeExists)
            {
                mfnMesh.createInPlace(numVert, numPoly, verticies, faceVtxCt, faceCnx);
                mfnMesh.Dispose();
            }
            else
            {
                using (var obj = mfnMesh.create(numVert, numPoly, verticies, faceVtxCt, faceCnx))
                {
                    MFnDependencyNode nodeFn = new MFnDagNode(obj);
                    nodeFn.setName(name);
                    MGlobal.executeCommand("sets -e -forceElement initialShadingGroup " + nodeFn.name);
                    mfnMesh.Dispose();
                }
            }


            return(true);
        }
Beispiel #15
0
        private void FILL(double x, double y, double z)
        {
            MFloatPoint pnt = new MFloatPoint((float)x, (float)y, (float)z);

            iarr.append(pnt);
        }
        public void Load(string name, SKLFile skl = null)
        {
            MIntArray        polygonIndexCounts = new MIntArray((uint)this.Indices.Count / 3);
            MIntArray        polygonIndices     = new MIntArray((uint)this.Indices.Count);
            MFloatPointArray vertices           = new MFloatPointArray((uint)this.Vertices.Count);
            MFloatArray      arrayU             = new MFloatArray((uint)this.Vertices.Count);
            MFloatArray      arrayV             = new MFloatArray((uint)this.Vertices.Count);
            MVectorArray     normals            = new MVectorArray((uint)this.Vertices.Count);
            MIntArray        normalIndices      = new MIntArray((uint)this.Vertices.Count);
            MFnMesh          mesh        = new MFnMesh();
            MDagPath         meshDagPath = new MDagPath();
            MDGModifier      modifier    = new MDGModifier();
            MFnSet           set         = new MFnSet();

            for (int i = 0; i < this.Indices.Count / 3; i++)
            {
                polygonIndexCounts[i] = 3;
            }

            for (int i = 0; i < this.Indices.Count; i++)
            {
                polygonIndices[i] = this.Indices[i];
            }

            for (int i = 0; i < this.Vertices.Count; i++)
            {
                SKNVertex vertex = this.Vertices[i];

                vertices[i]      = new MFloatPoint(vertex.Position.X, vertex.Position.Y, vertex.Position.Z);
                arrayU[i]        = vertex.UV.X;
                arrayV[i]        = 1 - vertex.UV.Y;
                normals[i]       = new MVector(vertex.Normal.X, vertex.Normal.Y, vertex.Normal.Z);
                normalIndices[i] = i;
            }

            //Assign mesh data
            mesh.create(this.Vertices.Count, this.Indices.Count / 3, vertices, polygonIndexCounts, polygonIndices, arrayU, arrayV, MObject.kNullObj);
            mesh.setVertexNormals(normals, normalIndices);
            mesh.getPath(meshDagPath);
            mesh.assignUVs(polygonIndexCounts, polygonIndices);

            //Set names
            mesh.setName(name);
            MFnTransform transformNode = new MFnTransform(mesh.parent(0));

            transformNode.setName("transform_" + name);

            //Get render partition
            MGlobal.displayInfo("SKNFile:Load - Searching for Render Partition");
            MItDependencyNodes itDependencyNodes = new MItDependencyNodes(MFn.Type.kPartition);
            MFnPartition       renderPartition   = new MFnPartition();
            bool foundRenderPartition            = false;

            for (; !itDependencyNodes.isDone; itDependencyNodes.next())
            {
                renderPartition.setObject(itDependencyNodes.thisNode);
                MGlobal.displayInfo("SKNFile:Load - Iterating through partition: " + renderPartition.name + " IsRenderPartition: " + renderPartition.isRenderPartition);
                if (renderPartition.name == "renderPartition" && renderPartition.isRenderPartition)
                {
                    MGlobal.displayInfo("SKNFile:Load - Found render partition");
                    foundRenderPartition = true;
                    break;
                }
            }


            //Create Materials
            for (int i = 0; i < this.Submeshes.Count; i++)
            {
                MFnDependencyNode dependencyNode = new MFnDependencyNode();
                MFnLambertShader  lambertShader  = new MFnLambertShader();
                SKNSubmesh        submesh        = this.Submeshes[i];
                MObject           shader         = lambertShader.create(true);

                lambertShader.setName(submesh.Name);
                lambertShader.color = MaterialProvider.GetMayaColor(i);

                MObject shadingEngine = dependencyNode.create("shadingEngine", submesh.Name + "_SG");
                MObject materialInfo  = dependencyNode.create("materialInfo", submesh.Name + "_MaterialInfo");
                if (foundRenderPartition)
                {
                    MPlug partitionPlug = new MFnDependencyNode(shadingEngine).findPlug("partition");
                    MPlug setsPlug      = MayaHelper.FindFirstNotConnectedElement(renderPartition.findPlug("sets"));
                    modifier.connect(partitionPlug, setsPlug);
                }
                else
                {
                    MGlobal.displayInfo("SKNFile:Load - Couldn't find Render Partition for mesh: " + name + "." + submesh.Name);
                }

                MPlug outColorPlug      = lambertShader.findPlug("outColor");
                MPlug surfaceShaderPlug = new MFnDependencyNode(shadingEngine).findPlug("surfaceShader");
                modifier.connect(outColorPlug, surfaceShaderPlug);

                MPlug messagePlug      = new MFnDependencyNode(shadingEngine).findPlug("message");
                MPlug shadingGroupPlug = new MFnDependencyNode(materialInfo).findPlug("shadingGroup");
                modifier.connect(messagePlug, shadingGroupPlug);

                modifier.doIt();

                MFnSingleIndexedComponent component = new MFnSingleIndexedComponent();
                MObject   faceComponent             = component.create(MFn.Type.kMeshPolygonComponent);
                MIntArray groupPolygonIndices       = new MIntArray();
                uint      endIndex = (submesh.StartIndex + submesh.IndexCount) / 3;
                for (uint j = submesh.StartIndex / 3; j < endIndex; j++)
                {
                    groupPolygonIndices.append((int)j);
                }
                component.addElements(groupPolygonIndices);

                set.setObject(shadingEngine);
                set.addMember(meshDagPath, faceComponent);
            }

            if (skl == null)
            {
                mesh.updateSurface();
            }
            else
            {
                MFnSkinCluster skinCluster             = new MFnSkinCluster();
                MSelectionList jointPathsSelectionList = new MSelectionList();

                jointPathsSelectionList.add(meshDagPath);
                for (int i = 0; i < skl.Influences.Count; i++)
                {
                    short    jointIndex = skl.Influences[i];
                    SKLJoint joint      = skl.Joints[jointIndex];
                    jointPathsSelectionList.add(skl.JointDagPaths[jointIndex]);

                    MGlobal.displayInfo(string.Format("SKNFile:Load:Bind - Added joint [{0}] {1} to binding selection", joint.ID, joint.Name));
                }

                MGlobal.selectCommand(jointPathsSelectionList);
                MGlobal.executeCommand("skinCluster -mi 4 -tsb -n skinCluster_" + name);

                MPlug      inMeshPlug        = mesh.findPlug("inMesh");
                MPlugArray inMeshConnections = new MPlugArray();
                inMeshPlug.connectedTo(inMeshConnections, true, false);

                if (inMeshConnections.length == 0)
                {
                    MGlobal.displayError("SKNFile:Load:Bind - Failed to find the created Skin Cluster");
                    throw new Exception("SKNFile:Load:Bind - Failed to find the created Skin Cluster");
                }

                MPlug         outputGeometryPlug = inMeshConnections[0];
                MDagPathArray influencesDagPaths = new MDagPathArray();

                skinCluster.setObject(outputGeometryPlug.node);
                skinCluster.influenceObjects(influencesDagPaths);

                MIntArray influenceIndices = new MIntArray((uint)skl.Influences.Count);
                for (int i = 0; i < skl.Influences.Count; i++)
                {
                    MDagPath influencePath = skl.JointDagPaths[skl.Influences[i]];

                    for (int j = 0; j < skl.Influences.Count; j++)
                    {
                        if (influencesDagPaths[j].partialPathName == influencePath.partialPathName)
                        {
                            influenceIndices[i] = j;
                            MGlobal.displayInfo("SKNReader:Load:Bind - Added Influence Joint: " + i + " -> " + j);
                            break;
                        }
                    }
                }

                MFnSingleIndexedComponent singleIndexedComponent = new MFnSingleIndexedComponent();
                MObject   vertexComponent    = singleIndexedComponent.create(MFn.Type.kMeshVertComponent);
                MIntArray groupVertexIndices = new MIntArray((uint)this.Vertices.Count);

                for (int i = 0; i < this.Vertices.Count; i++)
                {
                    groupVertexIndices[i] = i;
                }
                singleIndexedComponent.addElements(groupVertexIndices);

                MGlobal.executeCommand(string.Format("setAttr {0}.normalizeWeights 0", skinCluster.name));

                MDoubleArray weights = new MDoubleArray((uint)(this.Vertices.Count * skl.Influences.Count));
                for (int i = 0; i < this.Vertices.Count; i++)
                {
                    SKNVertex vertex = this.Vertices[i];

                    for (int j = 0; j < 4; j++)
                    {
                        double weight    = vertex.Weights[j];
                        int    influence = vertex.BoneIndices[j];

                        if (weight != 0)
                        {
                            weights[(i * skl.Influences.Count) + influence] = weight;
                        }
                    }
                }

                skinCluster.setWeights(meshDagPath, vertexComponent, influenceIndices, weights, false);
                MGlobal.executeCommand(string.Format("setAttr {0}.normalizeWeights 1", skinCluster.name));
                MGlobal.executeCommand(string.Format("skinPercent -normalize true {0} {1}", skinCluster.name, mesh.name));
                mesh.updateSurface();
            }
        }
Beispiel #17
0
       public MObject ConvertMeshMaya(TriMesh triMesh)
       {
          MFnMesh meshMaya = new MFnMesh();

          int verticeNum = triMesh.Vertices.Count;
          MFloatPointArray points = new MFloatPointArray();
         
          for(int i=0;i<verticeNum;i++)
          {
              float x=(float)triMesh.Vertices[i].Traits.Position.x;
              float y=(float)triMesh.Vertices[i].Traits.Position.y;
              float z=(float)triMesh.Vertices[i].Traits.Position.z;

              MFloatPoint vertex = new MFloatPoint(x,y,z);
              points.append(vertex);
          }

           
           int faceNum = triMesh.Faces.Count;

           int[] face_Counts=new int[faceNum];
           for(int i=0;i<faceNum;i++)
           {
              face_Counts[i]=3;
           }
           MIntArray faceCounts = new MIntArray(face_Counts);


           int[] faceTopology=new int[faceNum*3];
            
           for(int j=0;j<faceNum ;j++)
           {
              int index=j*3;
              faceTopology[index]=triMesh.Faces[j].GetVertex(0).Index ;
               
              faceTopology[index+1]=triMesh.Faces[j].GetVertex(1).Index ;
               
              faceTopology[index+2]=triMesh.Faces[j].GetVertex(2).Index ;
               
           }

           MIntArray faceConnects = new MIntArray(faceTopology);

           MObject mesh= meshMaya.create(verticeNum, faceNum, points, faceCounts, faceConnects);

           return mesh;
       }
 public static string toString(this MFloatPoint mFloatPoint)
 {
     return(mFloatPoint == null ? "" : mFloatPoint.toArray().toString());
 }