public override void doIt(MArgList args) { // Draw a circle and get its dagPath // using an iterator MGlobal.executeCommand("circle"); MFnNurbsCurve circle = new MFnNurbsCurve(); MDagPath dagPath = new MDagPath(); MItDependencyNodes iter = new MItDependencyNodes(MFn.Type.kNurbsCurve); for (iter.reset(); !iter.isDone; iter.next()) { MObject item = iter.item; if (item.hasFn(MFn.Type.kNurbsCurve)) { circle.setObject(item); circle.getPath(dagPath); MGlobal.displayInfo("DAG_PATH is " + dagPath.fullPathName); if (dagPath.isValid) { // register callback for instance add AND remove // dagPath.InstanceAddedDagPath += userDAGChildAddedCB; dagPath.InstanceRemovedDagPath += userDAGChildRemovedCB; // C# SDK will cleanup events, when this plugin is unloaded // callbacks.append(node); MGlobal.displayInfo("CALLBACK ADDED FOR INSTANCE ADD/REMOVE"); } } } }
public override void doIt(MArgList args) { // Draw a circle and get its dagPath // using an iterator MGlobal.executeCommand("circle"); MFnNurbsCurve circle = new MFnNurbsCurve(); MDagPath dagPath = new MDagPath(); MItDependencyNodes iter = new MItDependencyNodes( MFn.Type.kNurbsCurve); for (iter.reset(); !iter.isDone; iter.next()) { MObject item = iter.item; if (item.hasFn(MFn.Type.kNurbsCurve)) { circle.setObject(item); circle.getPath(dagPath); MGlobal.displayInfo("DAG_PATH is " + dagPath.fullPathName); if (dagPath.isValid) { // register callback for instance add AND remove // dagPath.InstanceAddedDagPath += userDAGChildAddedCB; dagPath.InstanceRemovedDagPath += userDAGChildRemovedCB; // C# SDK will cleanup events, when this plugin is unloaded // callbacks.append(node); MGlobal.displayInfo("CALLBACK ADDED FOR INSTANCE ADD/REMOVE"); } } } }
public override void writer(MFileObject file, string optionsString, MPxFileTranslator.FileAccessMode mode) { string fullName = file.fullName; StreamWriter sWriter = new StreamWriter(fullName); sWriter.Write("# Simple text file of custom node information" + Environment.NewLine); MItDependencyNodes iterator = new MItDependencyNodes(); while (!iterator.isDone) { MObject obj = iterator.thisNode; try { MFnDependencyNode dnFn = new MFnDependencyNode(obj); MPxNode userNode = dnFn.userNode; if (userNode != null) sWriter.Write("# custom node: " + dnFn.name + Environment.NewLine); } catch (System.Exception) { } iterator.next(); } sWriter.Close(); return; }
/// <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 static MFnPartition FindRenderPartition() { MItDependencyNodes itDependencyNodes = new MItDependencyNodes(MFn.Type.kPartition); MFnPartition renderPartition = new MFnPartition(); for (; !itDependencyNodes.isDone; itDependencyNodes.next()) { renderPartition.setObject(itDependencyNodes.thisNode); MGlobal.displayInfo("MayaHelper:FindRenderPartition - Iterating through partition: " + renderPartition.name + " IsRenderPartition: " + renderPartition.isRenderPartition); if (renderPartition.name == "renderPartition" && renderPartition.isRenderPartition) { MGlobal.displayInfo("MayaHelper:FindRenderPartition - Found render partition"); break; } } return(renderPartition); }
// // Write out all of the connections in the scene. // protected void writeConnections(FileStream f) { // // If the scene has broken any connections which were made in referenced // files, handle those first so that the attributes are free for any new // connections which may come along. // writeBrokenRefConnections(f); // // We're about to write out the scene's connections in three parts: DAG // nodes, non-DAG non-default nodes, then default nodes. // // It's really not necessary that we group them like this and would in // fact be more efficient to do them all in one MItDependencyNodes // traversal. However, this is the order in which the normal MayaAscii // translator does them, so this makes it easier to compare the output // of this translator to Maya's output. // // // Write out connections for the DAG nodes first. // MItDag dagIter = new MItDag(); dagIter.traverseUnderWorld(true); for (dagIter.next(); !dagIter.isDone; dagIter.next()) { MObject node = dagIter.item(); MFnDagNode dagNodeFn = new MFnDagNode(node); if (!dagNodeFn.isFlagSet(fConnectionFlag) && dagNodeFn.canBeWritten && !dagNodeFn.isDefaultNode) { writeNodeConnections(f, dagIter.item()); dagNodeFn.setFlag(fConnectionFlag, true); } } // // Now do the non-DAG, non-default nodes. // MItDependencyNodes nodeIter = new MItDependencyNodes(); for (; !nodeIter.isDone; nodeIter.next()) { MFnDependencyNode nodeFn = new MFnDependencyNode(nodeIter.item); if (!nodeFn.isFlagSet(fConnectionFlag) && nodeFn.canBeWritten && !nodeFn.isDefaultNode) { writeNodeConnections(f, nodeIter.item); nodeFn.setFlag(fConnectionFlag, true); } } // // And finish up with the default nodes. // uint numNodes = fDefaultNodes.length; int i; for (i = 0; i < numNodes; i++) { MFnDependencyNode nodeFn = new MFnDependencyNode(fDefaultNodes[i]); if (!nodeFn.isFlagSet(fConnectionFlag) && nodeFn.canBeWritten && nodeFn.isDefaultNode) { writeNodeConnections(f, fDefaultNodes[i]); nodeFn.setFlag(fConnectionFlag, true); } } }
protected void writeReferenceNodes(FileStream f) { // // We don't write out createNode commands for reference nodes, but // we do write out parenting between them and non-reference nodes, // as well as attributes added and attribute values changed after the // referenced file was loaded // writeRefNodeParenting(f); // // Output the commands for DAG nodes first. // MItDag dagIter = new MItDag(); for (dagIter.next(); !dagIter.isDone; dagIter.next()) { MObject node = dagIter.item(); MFnDependencyNode nodeFn = new MFnDependencyNode(node); if (nodeFn.isFromReferencedFile && !nodeFn.isFlagSet(fAttrFlag)) { writeNodeAttrs(f, node, false); // // Make note of any connections to this node which have been // broken by the main scene. // MFileIO.getReferenceConnectionsBroken( node, fBrokenConnSrcs, fBrokenConnDests, true, true ); nodeFn.setFlag(fAttrFlag, true); } } // // Now do the remaining, non-DAG nodes. // MItDependencyNodes nodeIter = new MItDependencyNodes(); for (; !nodeIter.isDone; nodeIter.next()) { MObject node = nodeIter.item; MFnDependencyNode nodeFn = new MFnDependencyNode(node); if (nodeFn.isFromReferencedFile && !nodeFn.isFlagSet(fAttrFlag)) { writeNodeAttrs(f, node, false); // // Make note of any connections to this node which have been // broken by the main scene. // MFileIO.getReferenceConnectionsBroken( node, fBrokenConnSrcs, fBrokenConnDests, true, true ); nodeFn.setFlag(fAttrFlag, true); } } }
protected void writeNonDagNodes(FileStream f) { MItDependencyNodes nodeIter = new MItDependencyNodes(); for (; !nodeIter.isDone; nodeIter.next()) { MObject node = nodeIter.item; MFnDependencyNode nodeFn = new MFnDependencyNode(node); // // Save default nodes for later processing. // if (nodeFn.isDefaultNode) { fDefaultNodes.append(node); } else if (!nodeFn.isFromReferencedFile && !nodeFn.isFlagSet(fCreateFlag)) { // // If this node is either writable or shared, then write it out. // Otherwise don't, but still mark it as having been written so // that we don't end up processing it again at some later time. // if (nodeFn.canBeWritten || nodeFn.isShared) { writeCreateNode(f, node); writeNodeAttrs(f, node, true); writeLockNode(f, node); } nodeFn.setFlag(fCreateFlag, true); nodeFn.setFlag(fAttrFlag, true); } } }
// // Maya calls this method to have the translator write out a file. // public override void writer(MFileObject file, string optionsString, MPxFileTranslator.FileAccessMode mode) { // // For simplicity, we only do full saves/exports. // if ((mode != MPxFileTranslator.FileAccessMode.kSaveAccessMode) && (mode != MPxFileTranslator.FileAccessMode.kExportAccessMode)) throw new System.NotImplementedException( "We only support support SaveAccessMode and ExportAccessMode"); // // Let's see if we can open the output file. // FileStream output = null; try { output = new FileStream(file.fullName, FileMode.Create, FileAccess.Write); } catch (ArgumentException ex) { MGlobal.displayInfo("File access invalid!"); if(output != null) output.Close(); throw ex; } // // Get some node flags to keep track of those nodes for which we // have already done various stages of processing. // fCreateFlag = MFnDependencyNode.allocateFlag(fPluginName); fAttrFlag = MFnDependencyNode.allocateFlag(fPluginName); fConnectionFlag = MFnDependencyNode.allocateFlag(fPluginName); // // Run through all of the nodes in the scene and clear their flags. // MItDependencyNodes nodesIter = new MItDependencyNodes(); for (; !nodesIter.isDone; nodesIter.next()) { MObject node = nodesIter.item; MFnDependencyNode nodeFn = new MFnDependencyNode(node); nodeFn.setFlag(fCreateFlag, false); nodeFn.setFlag(fAttrFlag, false); nodeFn.setFlag(fConnectionFlag, false); } // // Write out the various sections of the file. // writeHeader(output, file.name); writeFileInfo(output); writeReferences(output); writeRequirements(output); writeUnits(output); writeDagNodes(output); writeNonDagNodes(output); writeDefaultNodes(output); writeReferenceNodes(output); writeConnections(output); writeFooter(output, file.name); output.Close(); MFnDependencyNode.deallocateFlag(fPluginName, fCreateFlag); 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; }
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(); } }
private void buildMeshNodes() { MItDependencyNodes it = new MItDependencyNodes(MFn.Type.kMesh); while( !it.isDone ) { // Get the dag node of the mesh. MFnDagNode dagNode = new MFnDagNode(it.thisNode); // Create a new mesh. MayaMesh meshNode = new MayaMesh(); // Add the mesh to the all meshes list. allMeshes.Add(meshNode); // Configure the mesh node meshNode.name = dagNode.fullPathName; dagNode.getPath(meshNode.mayaObjectPath); meshNode.id = allMeshes.Count - 1; // The first parent of a mesh is the source transform, even with instances. meshNode.sourceXForm = pathXformMap[(new MFnDagNode(dagNode.parent(0))).fullPathName]; // Set the source Xform to know it's a source for an instance (its mesh). meshNode.sourceXForm.isSourceXform = true; // If the mesh has more than one parent, it has been instanced. We need to know this for computing local positions. meshNode.isInstanced = dagNode.isInstanced(); // Add the map to the dictionary to search by name. pathMeshMap[meshNode.name] = meshNode; it.next(); } }
private void buildLightNodes() { MItDependencyNodes it = new MItDependencyNodes(MFn.Type.kLight); while( !it.isDone ) { MObject mayaObj = it.thisNode; if( shouldPruneLight(mayaObj) ) { it.next(); continue; } // Create a new light and add it to the all lights list. MayaLight light = new MayaLight(); allLights.Add(light); light.id = allLights.Count - 1; // Get the dag node of the light for its name and parent. MFnDagNode dagNode = new MFnDagNode(mayaObj); dagNode.getPath(light.mayaObjectPath); light.name = dagNode.fullPathName; // First parent is the source transform node. light.sourceXform = pathXformMap[(new MFnDagNode(dagNode.parent(0))).fullPathName]; // Add the light to the dictionary to search by name. pathLightMap[light.name] = light; // // NOTE: Parent transforms of light sources in Maya can NOT be frozen, // so accessing them is guaranteed to not be frozen. // if( mayaObj.hasFn(MFn.Type.kAmbientLight) ) { MFnAmbientLight mayaLight = new MFnAmbientLight(mayaObj); light.type = MayaLight.Type.kAmbient; light.color = new Color(mayaLight.color.r, mayaLight.color.g, mayaLight.color.b, mayaLight.color.a); light.intensity = mayaLight.intensity; } else if( mayaObj.hasFn(MFn.Type.kDirectionalLight) ) { MFnDirectionalLight mayaLight = new MFnDirectionalLight(mayaObj); light.type = MayaLight.Type.kDirectional; light.color = new Color(mayaLight.color.r, mayaLight.color.g, mayaLight.color.b, mayaLight.color.a); light.intensity = mayaLight.intensity; } else if( mayaObj.hasFn(MFn.Type.kPointLight) ) { MFnPointLight mayaLight = new MFnPointLight(mayaObj); light.type = MayaLight.Type.kPoint; light.color = new Color(mayaLight.color.r, mayaLight.color.g, mayaLight.color.b, mayaLight.color.a); light.intensity = mayaLight.intensity; light.range = (float)mayaLight.centerOfIllumination; } else if( mayaObj.hasFn(MFn.Type.kSpotLight) ) { MFnSpotLight mayaLight = new MFnSpotLight(mayaObj); light.type = MayaLight.Type.kSpot; light.color = new Color(mayaLight.color.r, mayaLight.color.g, mayaLight.color.b, mayaLight.color.a); light.intensity = mayaLight.intensity; light.range = (float)mayaLight.centerOfIllumination; float mayaConeAngle = (float)(mayaLight.coneAngle * 0.5); light.coneInnerAngle = 57.29578f * mayaConeAngle; light.coneOuterAngle = 57.29578f * (mayaConeAngle + Math.Abs((float)mayaLight.penumbraAngle)); } it.next(); } }
private void buildLightNodes() { MItDependencyNodes it = new MItDependencyNodes(MFn.Type.kLight); while (!it.isDone) { MObject mayaObj = it.thisNode; if (shouldPruneLight(mayaObj)) { it.next(); continue; } // Create a new light and add it to the all lights list. MayaLight light = new MayaLight(); allLights.Add(light); light.id = allLights.Count - 1; // Get the dag node of the light for its name and parent. MFnDagNode dagNode = new MFnDagNode(mayaObj); dagNode.getPath(light.mayaObjectPath); light.name = dagNode.fullPathName; // First parent is the source transform node. light.sourceXform = pathXformMap[(new MFnDagNode(dagNode.parent(0))).fullPathName]; // Add the light to the dictionary to search by name. pathLightMap[light.name] = light; // // NOTE: Parent transforms of light sources in Maya can NOT be frozen, // so accessing them is guaranteed to not be frozen. // if (mayaObj.hasFn(MFn.Type.kAmbientLight)) { MFnAmbientLight mayaLight = new MFnAmbientLight(mayaObj); light.type = MayaLight.Type.kAmbient; light.color = new Color(mayaLight.color.r, mayaLight.color.g, mayaLight.color.b, mayaLight.color.a); light.intensity = mayaLight.intensity; } else if (mayaObj.hasFn(MFn.Type.kDirectionalLight)) { MFnDirectionalLight mayaLight = new MFnDirectionalLight(mayaObj); light.type = MayaLight.Type.kDirectional; light.color = new Color(mayaLight.color.r, mayaLight.color.g, mayaLight.color.b, mayaLight.color.a); light.intensity = mayaLight.intensity; } else if (mayaObj.hasFn(MFn.Type.kPointLight)) { MFnPointLight mayaLight = new MFnPointLight(mayaObj); light.type = MayaLight.Type.kPoint; light.color = new Color(mayaLight.color.r, mayaLight.color.g, mayaLight.color.b, mayaLight.color.a); light.intensity = mayaLight.intensity; light.range = (float)mayaLight.centerOfIllumination; } else if (mayaObj.hasFn(MFn.Type.kSpotLight)) { MFnSpotLight mayaLight = new MFnSpotLight(mayaObj); light.type = MayaLight.Type.kSpot; light.color = new Color(mayaLight.color.r, mayaLight.color.g, mayaLight.color.b, mayaLight.color.a); light.intensity = mayaLight.intensity; light.range = (float)mayaLight.centerOfIllumination; float mayaConeAngle = (float)(mayaLight.coneAngle * 0.5); light.coneInnerAngle = 57.29578f * mayaConeAngle; light.coneOuterAngle = 57.29578f * (mayaConeAngle + Math.Abs((float)mayaLight.penumbraAngle)); } it.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; }