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); }
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); }
private void createNodes() { // Generate the raw data for the requested primitive. generatePrimitiveData(); // 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. MFnMesh meshFn = new MFnMesh(); MObject dataObj = meshFn.create( num_verts, num_faces, pa, faceCounts, faceConnects, dataWrapper ); // Use the DAG modifier to create an empty mesh node and its parent // transform. MObject transform = dagMod.createNode("mesh", MObject.kNullObj); // Commit the creation so that the transform and its child will be // valid below. dagMod.doIt(); // At the moment we have a transform named something like 'transform1' // and a mesh named something like 'polySurfaceShape1'. Let's tidy that // up by renaming them as 'pPrimitive#' and 'pPrimitiveShape#', where // '#' is a number to ensure uniqueness. renameNodes(transform, "pPrimitive"); // Commit the rename so that assignShadingGroup() can get the new name. dagMod.doIt(); // Assign the mesh to a shading group. assignShadingGroup(transform, "initialShadingGroup"); // Commit the changes. dagMod.doIt(); // Set the mesh node to use the geometry we created for it. setMeshData(transform, dataWrapper); }
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(); }
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(); }
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(); } }
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; }
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; }