public static MFnDependencyNode CreateRemapValueNode(float inputMin, float inputMax, float outputMin, float outputMax) { MFnDependencyNode dependencyNode = new MFnDependencyNode(); dependencyNode.create("remapValue"); //Debug.Log("created node:" + (*dependencyNode).absoluteName()); dependencyNode.findPlug("inputMin").setFloat(inputMin); dependencyNode.findPlug("inputMax").setFloat(inputMax); dependencyNode.findPlug("outputMin").setFloat(outputMin); dependencyNode.findPlug("outputMax").setFloat(outputMax); return(dependencyNode); }
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(); }
override public void doIt(MArgList args) // Parses the given command line arguments and executes them. // { parseArgs(args); bool nothingSet = (!createdUsed && !appendCameraUsed && !appendCameraAndSetUsed && !deleteLayerUsed && !cameraUsed && !layerUsed && !helpUsed && !setUsed && !layerTypeUsed && !numLayersUsed); if (nothingSet) { throw new ArgumentException("A flag must be used. testCameraSet -help for available flags", "args"); } if (helpUsed) { MGlobal.displayInfo("testExCameraSet -help"); MGlobal.displayInfo("\ttestExCameraSet tests the functionality of the exCameraSet node."); MGlobal.displayInfo(""); MGlobal.displayInfo("\t-h -help : This message is printed"); MGlobal.displayInfo("\t-a -active [true/false]: Set/get whether a particular layer is active"); MGlobal.displayInfo("\t-ac -appendCamera <cameraName>: Append a new camera layer to the cameraSet using the specified camera"); MGlobal.displayInfo("\t-acs -appendCameraAndSet <cameraName> <setName>: Append a new camera layer to the cameraSet using the specified camera and set"); MGlobal.displayInfo("\t-cam -camera [<cameraName>]: Set/get the camera for a particular layer"); MGlobal.displayInfo("\t-c -create : Create a new cameraSet node"); MGlobal.displayInfo("\t-d -deleteLayer <layerIndex>: Delete the layer at the given index"); MGlobal.displayInfo("\t-nl -numLayers: Returns the number of layers defined in the specified cameraSet"); MGlobal.displayInfo("\t-l -layer <layerIndex>: Specifies the layer index to be used when accessing layer information"); MGlobal.displayInfo("\t-lt -layerType [<layerTypeName>]: Set/get the layer type for a particular layer. Possible values are Mono, Left, and Right."); MGlobal.displayInfo("\t-s -set [<setName>]: Set/get the set for a particular layer"); MGlobal.displayInfo("\t-e -edit : Perform an edit operation"); MGlobal.displayInfo("\t-q -query : Perform a query operation"); MGlobal.displayInfo(""); } uint nObjs = list.length; if (nObjs == 0) { if (createdUsed) { // Create a new cameraSet node. // MFnDependencyNode dirFn = new MFnDependencyNode(); string noName = ""; try { MObject dirObj = dirFn.create(exCameraSet.type_id, noName); MGlobal.select(dirObj, MGlobal.ListAdjustment.kReplaceList); } catch (System.Exception ex) { throw new ApplicationException("Could not create a cameraSet node", ex); } return; } if (appendCameraUsed || appendCameraAndSetUsed || deleteLayerUsed || editUsed || cameraUsed || setUsed || layerTypeUsed || activeUsed || numLayersUsed) { throw new ArgumentException("Must specify a cameraSet node", "args"); } } else { if (createdUsed) { throw new ArgumentException("-create cannot have any object specified", "args"); } if (appendCameraUsed) { if (nObjs != 1) { throw new ArgumentException("-appendCamera must have a single cameraSet node specified", "args"); } // Get the specified cameraSet node. // MObject dirNode = MObject.kNullObj; if (!getExCameraSetNode(dirNode)) { throw new ArgumentException("-appendCamera must have a valid exCameraSet node specified", "args"); } // Get a dag path to the specified camera. // MSelectionList camList = new MSelectionList(); camList.add(camName); MDagPath camPath = new MDagPath(); camList.getDagPath(0, camPath); if (!camPath.isValid) { throw new ArgumentException("-appendCamera must have a valid camera node specified", "args"); } // Call the MFnCameraSet method to append the layer. // MFnCameraSet dirFn = new MFnCameraSet(dirNode); dirFn.appendLayer(camPath, MObject.kNullObj); return; } if (appendCameraAndSetUsed) { if (nObjs != 1) { throw new ArgumentException("-appendCameraAndSet must have a single cameraSet node specified", "args"); } // Get the specified cameraSet node. // MObject dirNode = MObject.kNullObj; if (!getExCameraSetNode(dirNode)) { throw new ArgumentException("-appendCameraAndSet must have a valid exCameraSet node specified", "args"); } // Get a dag path to the specified camera. // MSelectionList camList = new MSelectionList(); camList.add(camName); MDagPath camPath = new MDagPath(); camList.getDagPath(0, camPath); if (!camPath.isValid) { throw new ArgumentException("-appendCameraAndSet must have a valid camera node specified", "args"); } // Get the specified set node. // MSelectionList setList = new MSelectionList(); setList.add(setName); MObject setObj = MObject.kNullObj; setList.getDependNode(0, setObj); if (setObj == MObject.kNullObj) { throw new ArgumentException("-appendCameraAndSet must have a valid set node specified", "args"); } // Call the MFnCameraSet method to append the layer. // MFnCameraSet dirFn = new MFnCameraSet(dirNode); dirFn.appendLayer(camPath, setObj); return; } if (deleteLayerUsed) { if (nObjs != 1) { throw new ArgumentException("-deleteLayer must have a single cameraSet node specified", "args"); } // Get the specified cameraSet node. // MObject dirNode = MObject.kNullObj; if (!getExCameraSetNode(dirNode)) { throw new ArgumentException("-deleteLayer must have a valid exCameraSet node specified", "args"); } // Call the MFnCameraSet method to delete the layer. // MFnCameraSet dirFn = new MFnCameraSet(dirNode); dirFn.deleteLayer((uint)cameraLayer); return; } if (numLayersUsed) { if (queryUsed) { // Get the specified cameraSet node. // MObject dirNode = MObject.kNullObj; if (!getExCameraSetNode(dirNode)) { throw new ArgumentException("-numLayers must have a valid exCameraSet node specified", "args"); } // Call the MFnCameraSet method to get the number of layers. // MFnCameraSet dirFn = new MFnCameraSet(dirNode); uint numLayers = dirFn.numLayers; setResult((int)numLayers); } else { throw new ArgumentException("-numLayers requires the query flag to be used", "args"); } return; } if (cameraUsed) { if ((nObjs != 1) || (!layerUsed)) { throw new ArgumentException("-camera must have a cameraSet node and layer specified", "args"); } // Get the specified cameraSet node. // MObject dirNode = MObject.kNullObj; if (!getExCameraSetNode(dirNode)) { throw new ArgumentException("-camera must have a valid exCameraSet node specified", "args"); } if (editUsed) { // Get a dag path to the specified camera. // MSelectionList camList = new MSelectionList(); camList.add(camName); MDagPath camPath = new MDagPath(); camList.getDagPath(0, camPath); if (!camPath.isValid) { throw new ArgumentException("-camera must have a valid camera node specified", "args"); } // Call the MFnCameraSet method to set the camera. // MFnCameraSet dirFn = new MFnCameraSet(dirNode); dirFn.setLayerCamera((uint)cameraLayer, camPath); } else if (queryUsed) { // Call the MFnCameraSet method to get the camera. // MDagPath camPath = new MDagPath(); MFnCameraSet dirFn = new MFnCameraSet(dirNode); dirFn.getLayerCamera((uint)cameraLayer, camPath); MObject camNode = camPath.node; MFnDependencyNode nodeFn = new MFnDependencyNode(camNode); setResult(nodeFn.name); } } if (setUsed) { if ((nObjs != 1) || (!layerUsed)) { throw new ArgumentException("-set must have a cameraSet node and layer specified", "args"); } // Get the specified cameraSet node. // MObject dirNode = MObject.kNullObj; if (!getExCameraSetNode(dirNode)) { throw new ArgumentException("-set must have a valid exCameraSet node specified", "args"); } if (editUsed) { // Get the specified set node. // MObject setObj = MObject.kNullObj; if (setName != "") { MSelectionList setList = new MSelectionList(); setList.add(setName); setList.getDependNode(0, setObj); if (setObj == MObject.kNullObj) { throw new ArgumentException("-set must have a valid set node specified", "args"); } } // Call the MFnCameraSet method to set the set node. // MFnCameraSet dirFn = new MFnCameraSet(dirNode); dirFn.setLayerSceneData((uint)cameraLayer, setObj); } else if (queryUsed) { // Call the MFnCameraSet method to get the set node. // MObject setObj = new MObject(); MFnCameraSet dirFn = new MFnCameraSet(dirNode); dirFn.getLayerSceneData((uint)cameraLayer, setObj); MFnDependencyNode nodeFn = new MFnDependencyNode(setObj); setResult(nodeFn.name); } } if (layerTypeUsed) { if ((nObjs != 1) || (!layerUsed)) { throw new ArgumentException("-layerType must have a cameraSet node and layer specified", "args"); } // Get the specified cameraSet node. // MObject dirNode = MObject.kNullObj; if (!getExCameraSetNode(dirNode)) { throw new ArgumentException("-layerType must have a valid exCameraSet node specified", "args"); } MFnDependencyNode nodeFn = new MFnDependencyNode(dirNode); exCameraSet exDir = nodeFn.userNode as exCameraSet; if (editUsed) { // Get the specified layer type. // int pt = -1; if (layerTypeVal == "Mono") { pt = 0; } else if (layerTypeVal == "Left") { pt = 1; } else if (layerTypeVal == "Right") { pt = 2; } else { throw new ArgumentException("-layerType must have a valid type specified", "args"); } // Call the exCameraSet method to set the layer type. // exDir.setLayerType((uint)cameraLayer, pt); } else if (queryUsed) { // Call the exCameraSet method to get the layer type. // try { int lt = exDir.getLayerType((uint)cameraLayer); if (lt == 0) { setResult("Mono"); } else if (lt == 1) { setResult("Left"); } else if (lt == 2) { setResult("Right"); } } catch (System.Exception ex) { throw new ApplicationException("exCameraSet node does not have a valid layer type", ex); } } } if (activeUsed) { if ((nObjs != 1) || (!layerUsed)) { throw new ArgumentException("-active must have a cameraSet node and layer specified", "args"); } // Get the specified cameraSet node. // MObject dirNode = MObject.kNullObj; if (!getExCameraSetNode(dirNode)) { throw new ArgumentException("-active must have a valid exCameraSet node specified", "args"); } if (editUsed) { // Call the MFnCameraSet method to set the set node. // MFnCameraSet dirFn = new MFnCameraSet(dirNode); dirFn.setLayerActive((uint)cameraLayer, activeVal); } else if (queryUsed) { // Call the MFnCameraSet method to get the active value. // MFnCameraSet dirFn = new MFnCameraSet(dirNode); activeVal = dirFn.isLayerActive((uint)cameraLayer); setResult(activeVal); } } } return; }
// Parses the given command line arguments and executes them. // public override void doIt(MArgList args) { parseArgs(args); bool nothingSet = (!createdUsed && !appendCameraUsed && !appendCameraAndSetUsed && !deleteLayerUsed && !cameraUsed && !layerUsed && !helpUsed && !setUsed && !layerTypeUsed && !numLayersUsed); if (nothingSet) { throw new ArgumentException("A flag must be used. testCameraSet -help for available flags", "args"); } if (helpUsed) { MGlobal.displayInfo("testExCameraSet -help"); MGlobal.displayInfo("\ttestExCameraSet tests the functionality of the exCameraSet node."); MGlobal.displayInfo(""); MGlobal.displayInfo("\t-h -help : This message is printed"); MGlobal.displayInfo("\t-a -active [true/false]: Set/get whether a particular layer is active"); MGlobal.displayInfo("\t-ac -appendCamera <cameraName>: Append a new camera layer to the cameraSet using the specified camera"); MGlobal.displayInfo("\t-acs -appendCameraAndSet <cameraName> <setName>: Append a new camera layer to the cameraSet using the specified camera and set"); MGlobal.displayInfo("\t-cam -camera [<cameraName>]: Set/get the camera for a particular layer"); MGlobal.displayInfo("\t-c -create : Create a new cameraSet node"); MGlobal.displayInfo("\t-d -deleteLayer <layerIndex>: Delete the layer at the given index"); MGlobal.displayInfo("\t-nl -numLayers: Returns the number of layers defined in the specified cameraSet"); MGlobal.displayInfo("\t-l -layer <layerIndex>: Specifies the layer index to be used when accessing layer information"); MGlobal.displayInfo("\t-lt -layerType [<layerTypeName>]: Set/get the layer type for a particular layer. Possible values are Mono, Left, and Right."); MGlobal.displayInfo("\t-s -set [<setName>]: Set/get the set for a particular layer"); MGlobal.displayInfo("\t-e -edit : Perform an edit operation"); MGlobal.displayInfo("\t-q -query : Perform a query operation"); MGlobal.displayInfo(""); } uint nObjs = list.length; if (nObjs == 0) { if (createdUsed) { // Create a new cameraSet node. // MFnDependencyNode dirFn = new MFnDependencyNode(); string noName = ""; try { MObject dirObj = dirFn.create(exCameraSet.type_id, noName); MGlobal.select(dirObj, MGlobal.ListAdjustment.kReplaceList); } catch (System.Exception ex) { throw new ApplicationException("Could not create a cameraSet node", ex); } return; } if (appendCameraUsed || appendCameraAndSetUsed || deleteLayerUsed || editUsed || cameraUsed || setUsed || layerTypeUsed || activeUsed || numLayersUsed) { throw new ArgumentException("Must specify a cameraSet node", "args"); } } else { if (createdUsed) { throw new ArgumentException("-create cannot have any object specified", "args"); } if (appendCameraUsed) { if (nObjs != 1) { throw new ArgumentException("-appendCamera must have a single cameraSet node specified", "args"); } // Get the specified cameraSet node. // MObject dirNode = MObject.kNullObj; if (!getExCameraSetNode(dirNode)) { throw new ArgumentException("-appendCamera must have a valid exCameraSet node specified", "args"); } // Get a dag path to the specified camera. // MSelectionList camList = new MSelectionList(); camList.add(camName); MDagPath camPath = new MDagPath(); camList.getDagPath(0, camPath); if (!camPath.isValid) { throw new ArgumentException("-appendCamera must have a valid camera node specified", "args"); } // Call the MFnCameraSet method to append the layer. // MFnCameraSet dirFn = new MFnCameraSet(dirNode); dirFn.appendLayer(camPath, MObject.kNullObj); return; } if (appendCameraAndSetUsed) { if (nObjs != 1) { throw new ArgumentException("-appendCameraAndSet must have a single cameraSet node specified", "args"); } // Get the specified cameraSet node. // MObject dirNode = MObject.kNullObj; if (!getExCameraSetNode(dirNode)) { throw new ArgumentException("-appendCameraAndSet must have a valid exCameraSet node specified", "args"); } // Get a dag path to the specified camera. // MSelectionList camList = new MSelectionList(); camList.add(camName); MDagPath camPath = new MDagPath(); camList.getDagPath(0, camPath); if (!camPath.isValid) { throw new ArgumentException("-appendCameraAndSet must have a valid camera node specified", "args"); } // Get the specified set node. // MSelectionList setList = new MSelectionList(); setList.add(setName); MObject setObj = MObject.kNullObj; setList.getDependNode(0, setObj); if (setObj == MObject.kNullObj) { throw new ArgumentException("-appendCameraAndSet must have a valid set node specified", "args"); } // Call the MFnCameraSet method to append the layer. // MFnCameraSet dirFn = new MFnCameraSet(dirNode); dirFn.appendLayer(camPath, setObj); return; } if (deleteLayerUsed) { if (nObjs != 1) { throw new ArgumentException("-deleteLayer must have a single cameraSet node specified", "args"); } // Get the specified cameraSet node. // MObject dirNode = MObject.kNullObj; if (!getExCameraSetNode(dirNode)) { throw new ArgumentException("-deleteLayer must have a valid exCameraSet node specified", "args"); } // Call the MFnCameraSet method to delete the layer. // MFnCameraSet dirFn = new MFnCameraSet(dirNode); dirFn.deleteLayer((uint)cameraLayer); return; } if (numLayersUsed) { if (queryUsed) { // Get the specified cameraSet node. // MObject dirNode = MObject.kNullObj; if (!getExCameraSetNode(dirNode)) { throw new ArgumentException("-numLayers must have a valid exCameraSet node specified", "args"); } // Call the MFnCameraSet method to get the number of layers. // MFnCameraSet dirFn = new MFnCameraSet(dirNode); uint numLayers = dirFn.numLayers; setResult((int)numLayers); } else { throw new ArgumentException("-numLayers requires the query flag to be used", "args"); } return; } if (cameraUsed) { if ((nObjs != 1) || (!layerUsed)) { throw new ArgumentException("-camera must have a cameraSet node and layer specified", "args"); } // Get the specified cameraSet node. // MObject dirNode = MObject.kNullObj; if (!getExCameraSetNode(dirNode)) { throw new ArgumentException("-camera must have a valid exCameraSet node specified", "args"); } if (editUsed) { // Get a dag path to the specified camera. // MSelectionList camList = new MSelectionList(); camList.add(camName); MDagPath camPath = new MDagPath(); camList.getDagPath(0, camPath); if (!camPath.isValid) { throw new ArgumentException("-camera must have a valid camera node specified", "args"); } // Call the MFnCameraSet method to set the camera. // MFnCameraSet dirFn = new MFnCameraSet(dirNode); dirFn.setLayerCamera((uint)cameraLayer, camPath); } else if (queryUsed) { // Call the MFnCameraSet method to get the camera. // MDagPath camPath = new MDagPath(); MFnCameraSet dirFn = new MFnCameraSet(dirNode); dirFn.getLayerCamera((uint)cameraLayer, camPath); MObject camNode = camPath.node; MFnDependencyNode nodeFn = new MFnDependencyNode(camNode); setResult(nodeFn.name); } } if (setUsed) { if ((nObjs != 1) || (!layerUsed)) { throw new ArgumentException("-set must have a cameraSet node and layer specified", "args"); } // Get the specified cameraSet node. // MObject dirNode = MObject.kNullObj; if (!getExCameraSetNode(dirNode)) { throw new ArgumentException("-set must have a valid exCameraSet node specified", "args"); } if (editUsed) { // Get the specified set node. // MObject setObj = MObject.kNullObj; if (setName != "") { MSelectionList setList = new MSelectionList(); setList.add(setName); setList.getDependNode(0, setObj); if (setObj == MObject.kNullObj) { throw new ArgumentException("-set must have a valid set node specified", "args"); } } // Call the MFnCameraSet method to set the set node. // MFnCameraSet dirFn = new MFnCameraSet(dirNode); dirFn.setLayerSceneData((uint)cameraLayer, setObj); } else if (queryUsed) { // Call the MFnCameraSet method to get the set node. // MObject setObj = new MObject(); MFnCameraSet dirFn = new MFnCameraSet(dirNode); dirFn.getLayerSceneData((uint)cameraLayer, setObj); MFnDependencyNode nodeFn = new MFnDependencyNode(setObj); setResult(nodeFn.name); } } if (layerTypeUsed) { if ((nObjs != 1) || (!layerUsed)) { throw new ArgumentException("-layerType must have a cameraSet node and layer specified", "args"); } // Get the specified cameraSet node. // MObject dirNode = MObject.kNullObj; if (!getExCameraSetNode(dirNode)) { throw new ArgumentException("-layerType must have a valid exCameraSet node specified", "args"); } MFnDependencyNode nodeFn = new MFnDependencyNode(dirNode); exCameraSet exDir = nodeFn.userNode as exCameraSet; if (editUsed) { // Get the specified layer type. // int pt = -1; if (layerTypeVal == "Mono") pt = 0; else if (layerTypeVal == "Left") pt = 1; else if (layerTypeVal == "Right") pt = 2; else { throw new ArgumentException("-layerType must have a valid type specified", "args"); } // Call the exCameraSet method to set the layer type. // exDir.setLayerType((uint)cameraLayer, pt); } else if (queryUsed) { // Call the exCameraSet method to get the layer type. // try { int lt = exDir.getLayerType((uint)cameraLayer); if (lt == 0) setResult("Mono"); else if (lt == 1) setResult("Left"); else if (lt == 2) setResult("Right"); } catch (System.Exception ex) { throw new ApplicationException("exCameraSet node does not have a valid layer type", ex); } } } if (activeUsed) { if ((nObjs != 1) || (!layerUsed)) { throw new ArgumentException("-active must have a cameraSet node and layer specified", "args"); } // Get the specified cameraSet node. // MObject dirNode = MObject.kNullObj; if (!getExCameraSetNode(dirNode)) { throw new ArgumentException("-active must have a valid exCameraSet node specified", "args"); } if (editUsed) { // Call the MFnCameraSet method to set the set node. // MFnCameraSet dirFn = new MFnCameraSet(dirNode); dirFn.setLayerActive((uint)cameraLayer, activeVal); } else if (queryUsed) { // Call the MFnCameraSet method to get the active value. // MFnCameraSet dirFn = new MFnCameraSet(dirNode); activeVal = dirFn.isLayerActive((uint)cameraLayer); setResult(activeVal); } } } return; }
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(); } }