Example #1
0
        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;
        }
Example #3
0
		//
		// 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);
				}
			}
		}
Example #4
0
		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);
				}
			}
		}
Example #5
0
		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);
				}
			}
		}
Example #6
0
		//
		// 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;
        }
Example #8
0
 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();
     }
 }
Example #9
0
        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();
            }
        }