//====================================================================== // // Check the parsed arguments and do/undo/redo the command as appropriate // void checkArgs(ref MArgDatabase argsDb) { MSelectionList objects = new MSelectionList(); argsDb.getObjects(objects); for (uint i = 0; i < objects.length; ++i) { MDagPath dagPath = new MDagPath(); objects.getDagPath((uint)i, dagPath); MFnDagNode dagNode = new MFnDagNode(dagPath.node); MObject obj = dagNode.child(0); if (obj.apiTypeStr == "kMesh") { fMesh = new MFnMesh(obj); fObj = obj; fObjTransform = dagPath.node; } } if( fMesh == null || fObj == null || fObjTransform == null ) { string errMsg = MStringResource.getString(MetaDataRegisterMStringResources.kObjectNotFoundError); throw new ArgumentException(errMsg, "argsDb"); } }
//====================================================================== // // Check the parsed arguments and do/undo/redo the command as appropriate // void checkArgs(ref MArgDatabase argsDb) { MSelectionList objects = new MSelectionList(); argsDb.getObjects(objects); for (uint i = 0; i < objects.length; ++i) { MDagPath dagPath = new MDagPath(); objects.getDagPath((uint)i, dagPath); MFnDagNode dagNode = new MFnDagNode(dagPath.node); MObject obj = dagNode.child(0); if (obj.apiTypeStr == "kMesh") { fMesh = new MFnMesh(obj); fObj = obj; fObjTransform = dagPath.node; } } if (fMesh == null || fObj == null || fObjTransform == null) { string errMsg = MStringResource.getString(MetaDataRegisterMStringResources.kObjectNotFoundError); throw new ArgumentException(errMsg, "argsDb"); } }
//====================================================================== // // Look through the arg database and verify that the arguments are // valid. Only checks the common flags so derived classes should call // this parent method first before checking their own flags. // public virtual void checkArgs(MArgDatabase argsDb) { String formatType = "raw"; fSerialize = AssociationsSerializer.formatByName(formatType); if (fSerialize == null) { String fmt = MStringResource.getString(MetaDataRegisterMStringResources.kMetadataFormatNotFound); String msg = String.Format(fmt, formatType); displayError(msg); throw new System.ArgumentException(msg); } //---------------------------------------- // (selection list) // // Commands need at least one mesh object on which to operate so gather up // the list of meshes specified and/or selected. // // Empty out the list of meshes on which to operate so that it can be // populated from the selection or specified lists. fMeshes.clear(); MSelectionList objects = new MSelectionList(); argsDb.getObjects(objects); for (int i = 0; i < objects.length; ++i) { MDagPath dagPath = new MDagPath(); objects.getDagPath((uint)i, dagPath); MFnDagNode dagNode = new MFnDagNode(dagPath.node); MObject obj = dagNode.child(0); if (obj.apiTypeStr == "kMesh") { MFnMesh mesh = new MFnMesh(obj); if (mesh != null) { fMeshes.append(obj); } } else { String fmt = MStringResource.getString(MetaDataRegisterMStringResources.kObjectTypeError); String msg = String.Format(fmt, dagPath.fullPathName + "[" + obj.apiTypeStr + "]"); displayError(msg); throw new System.InvalidOperationException(msg); } } if (fMeshes.length == 0) { String msg = MStringResource.getString(MetaDataRegisterMStringResources.kObjectNotFoundError); displayError(msg); throw new System.InvalidOperationException(msg); } }
override public void redoIt() { _dagModifier.doIt(); var dagFn = new MFnDagNode(_transform); _shape = dagFn.child(0); _dagModifier.renameNode(_shape, "motionCameraShape"); _dagModifier.doIt(); }
//====================================================================== // // Look through the arg database and verify that the arguments are // valid. Only checks the common flags so derived classes should call // this parent method first before checking their own flags. // public virtual void checkArgs(MArgDatabase argsDb) { String formatType = "raw"; fSerialize = AssociationsSerializer.formatByName( formatType ); if( fSerialize == null) { String fmt = MStringResource.getString(MetaDataRegisterMStringResources.kMetadataFormatNotFound); String msg = String.Format(fmt, formatType); displayError(msg); throw new System.ArgumentException(msg); } //---------------------------------------- // (selection list) // // Commands need at least one mesh object on which to operate so gather up // the list of meshes specified and/or selected. // // Empty out the list of meshes on which to operate so that it can be // populated from the selection or specified lists. fMeshes.clear(); MSelectionList objects = new MSelectionList(); argsDb.getObjects(objects); for (int i = 0; i<objects.length; ++i) { MDagPath dagPath = new MDagPath(); objects.getDagPath((uint)i, dagPath); MFnDagNode dagNode = new MFnDagNode( dagPath.node ); MObject obj = dagNode.child(0); if (obj.apiTypeStr == "kMesh") { MFnMesh mesh = new MFnMesh(obj); if(mesh != null) fMeshes.append(obj); } else { String fmt = MStringResource.getString(MetaDataRegisterMStringResources.kObjectTypeError); String msg = String.Format(fmt, dagPath.fullPathName + "[" + obj.apiTypeStr + "]"); displayError(msg); throw new System.InvalidOperationException(msg); } } if( fMeshes.length == 0 ) { String msg = MStringResource.getString(MetaDataRegisterMStringResources.kObjectNotFoundError); displayError(msg); throw new System.InvalidOperationException(msg); } }
private void renameNodes(MObject transform, string baseName) { // Rename the transform to something we know no node will be using. dagMod.renameNode(transform, "polyPrimitiveCmdTemp"); // Rename the mesh to the same thing but with 'Shape' on the end. MFnDagNode dagFn = new MFnDagNode(transform); dagMod.renameNode(dagFn.child(0), "polyPrimitiveCmdTempShape"); // Now that they are in the 'something/somethingShape' format, any // changes we make to the name of the transform will automatically be // propagated to the shape as well. // // Maya will replace the '#' in the string below with a number which // ensures uniqueness. string transformName = baseName + "Shape"; dagMod.renameNode(transform, baseName); dagMod.renameNode(dagFn.child(0), transformName); }
//====================================================================== // // Check the parsed arguments and do/undo/redo the command as appropriate // void checkArgs(ref MArgDatabase argsDb) { //---------------------------------------- // (selection list) // // Commands need at least one node on which to operate so gather up // the list of nodes specified and/or selected. // // Empty out the list of nodes on which to operate so that it can be // populated from the selection or specified lists. fNodes.clear(); MSelectionList objects = new MSelectionList(); argsDb.getObjects(objects); for (uint i = 0; i < objects.length; ++i) { MDagPath dagPath = new MDagPath(); objects.getDagPath((uint)i, dagPath); MFnDagNode dagNode = new MFnDagNode(dagPath.node); MObject obj = dagNode.child(0); if (obj.apiTypeStr == "kMesh") { if (obj == MObject.kNullObj) { throw new ApplicationException("Error: objects.getDependNode() "); } fNodes.append(dagPath.node); } else { String fmt = MStringResource.getString(MetaDataRegisterMStringResources.kObjectTypeError); String msg = String.Format(fmt, dagPath.fullPathName + "[" + obj.apiTypeStr + "]"); displayError(msg); throw new System.InvalidOperationException(msg); } } if (fNodes.length == 0) { string errMsg = MStringResource.getString(MetaDataRegisterMStringResources.kObjectNotFoundError); throw new ArgumentException(errMsg, "argsDb"); } }
private void setMeshData(MObject transform, MObject dataWrapper) { // Get the mesh node. MFnDagNode dagFn = new MFnDagNode(transform); MObject mesh = dagFn.child(0); // The mesh node has two geometry inputs: 'inMesh' and 'cachedInMesh'. // 'inMesh' is only used when it has an incoming connection, otherwise // 'cachedInMesh' is used. Unfortunately, the docs say that 'cachedInMesh' // is for internal use only and that changing it may render Maya // unstable. // // To get around that, we do the little dance below... // Use a temporary MDagModifier to create a temporary mesh attribute on // the node. MFnTypedAttribute tAttr = new MFnTypedAttribute(); MObject tempAttr = tAttr.create("tempMesh", "tmpm", MFnData.Type.kMesh); MDagModifier tempMod = new MDagModifier(); tempMod.addAttribute(mesh, tempAttr); tempMod.doIt(); // Set the geometry data onto the temp attribute. dagFn.setObject(mesh); MPlug tempPlug = dagFn.findPlug(tempAttr); tempPlug.setValue(dataWrapper); // Use the temporary MDagModifier to connect the temp attribute to the // node's 'inMesh'. MPlug inMeshPlug = dagFn.findPlug("inMesh"); tempMod.connect(tempPlug, inMeshPlug); tempMod.doIt(); // Force the mesh to update by grabbing its output geometry. dagFn.findPlug("outMesh").asMObject(); // Undo the temporary modifier. tempMod.undoIt(); }
private void linkXformsMeshes() { foreach (MayaXform xform in allXforms) { // Convert to a DAG node to traverse children. MFnDagNode dagNode = new MFnDagNode(xform.mayaObjectPath); for (uint childIdx = 0; childIdx < dagNode.childCount; ++childIdx) { MObject childObj = dagNode.child(childIdx); // If the child has a mesh, link it. if (childObj.hasFn(MFn.Type.kMesh)) { xform.mesh = pathMeshMap[(new MFnMesh(childObj)).fullPathName]; break; } } } }
private void processXform(MayaXform xform) { // Add the current Xform to the list of all xforms. allXforms.Add(xform); // Set the Xform's id. xform.id = allXforms.Count - 1; // Add the current Xform to the map based on its path. pathXformMap[xform.name] = xform; // Build data for this xform. if (!xform.isRoot) { getXformData(xform); } MFnDagNode dagNode = new MFnDagNode(xform.mayaObjectPath); for (uint childIdx = 0; childIdx < dagNode.childCount; ++childIdx) { // If the child isn't a transform node, or it is a kManipulator3D (maya api bug), skip it. MObject childObj = dagNode.child(childIdx); if (!childObj.hasFn(MFn.Type.kTransform) || childObj.hasFn(MFn.Type.kManipulator3D)) { continue; } // Create a node for the child transform, parent it to this xform, and process the child's children. MFnDagNode childNode = new MFnDagNode(childObj); // Strip out nodes we don't care about. if (xformsToIgnore.Contains(childNode.fullPathName)) { continue; } MayaXform childXform = new MayaXform(); childXform.name = childNode.fullPathName; childNode.getPath(childXform.mayaObjectPath); childXform.parent = xform; xform.children.Add(childXform); processXform(childXform); } }
private void assignShadingGroup(MObject transform, string groupName) { // Get the name of the mesh node. // // We need to use an MFnDagNode rather than an MFnMesh because the mesh // is not fully realized at this point and would be rejected by MFnMesh. MFnDagNode dagFn = new MFnDagNode(transform); dagFn.setObject(dagFn.child(0)); string meshName = dagFn.name; // Use the DAG modifier to put the mesh into a shading group string cmd = "sets -e -fe "; cmd += groupName + " " + meshName; dagMod.commandToExecute(cmd); // Use the DAG modifier to select the new mesh. cmd = "select " + meshName; dagMod.commandToExecute(cmd); }
//====================================================================== // // Do the metadata creation. The metadata will be randomly initialized // based on the channel type and the structure specified. For recognized // components the number of metadata elements will correspond to the count // of components in the selected mesh, otherwise a random number of metadata // elements between 1 and 100 will be created (at consecutive indices). // // The previously existing metadata is preserved for later undo. // override public void doIt(MArgList args) { MArgDatabase argsDb = new MArgDatabase(syntax, args); checkArgs(ref argsDb); clearResult(); uint numNodes = fNodes.length; for ( int i = 0; i < numNodes; ++i) { // fNodes[i] is the transform not the shape itself MFnDagNode dagNode = new MFnDagNode(fNodes[i]); MObject obj = dagNode.child(0); // obj is the shape, which is where we can add the meta data MFnDependencyNode node = new MFnDependencyNode(obj); Console.WriteLine( "METADATA for node " + dagNode.fullPathName ); Console.WriteLine( "=====================================================================" ); foreach( Channel chn in node.metadata) { Console.WriteLine("Channel: type = {0}, nbStreams = {1}", chn.nameProperty, chn.dataStreamCount); foreach (Stream strm in chn) { Console.WriteLine("Stream: name = {0}, nbElements = {1}", strm.name, strm.elementCount() ); Structure strct = strm.structure; Console.WriteLine("Structure: name = {0}, nbMembers = {1}", strct.name, strct.memberCount ); string[] memberNames = new string[strct.memberCount]; int memberID = -1; foreach( Member member in strct ) { ++memberID; Console.WriteLine("Structure member: name = {0}, type = {1}, array size = {2}", member.nameProperty, member.typeProperty.ToString(), member.lengthProperty ); memberNames[memberID] = member.nameProperty; } int k = -1; foreach (Handle handle in strm) { ++k; for (uint n = 0; n < strct.memberCount; ++n) { handle.setPositionByMemberIndex(n); Array data = handle.asType; if( data.Length < 1 ) throw new ApplicationException( "Handle data seems corrupted" ); string line = string.Format( "Handle #{0}, member = {1}, data = {2}", k, memberNames[n], data.GetValue(0).ToString() ); if( data.Length > 1 ) { for( int d = 1; d < data.Length; ++d ) { line = line + "," + data.GetValue(d).ToString(); } } Console.WriteLine( line ); } } } } } }
//====================================================================== // // Check the parsed arguments and do/undo/redo the command as appropriate // void checkArgs(ref MArgDatabase argsDb) { //---------------------------------------- // -structure flag // fStructureFlag.parse(ref argsDb, flagStructure); if (fStructureFlag.isSet()) { if (!fStructureFlag.isArgValid()) { string errMsg = MStringResource.getString(MetaDataRegisterMStringResources.kInvalidString); throw new ArgumentException(errMsg, "argsDb"); } string structureName = fStructureFlag.arg(); try { fStructure = Structure.structureByName(structureName); } catch (System.Exception) { string msgFmt = MStringResource.getString(MetaDataRegisterMStringResources.kCreateMetadataStructureNotFound); throw new ArgumentException(String.Format(msgFmt, structureName), "argsDb"); } } else { string errMsg = MStringResource.getString(MetaDataRegisterMStringResources.kCreateMetadataNoStructureName); throw new ArgumentException(errMsg, "argsDb"); } //---------------------------------------- // -streamName flag // fStreamNameFlag.parse(ref argsDb, flagStreamName); if (fStreamNameFlag.isSet()) { if (!fStreamNameFlag.isArgValid()) { string errMsg = MStringResource.getString(MetaDataRegisterMStringResources.kInvalidString); throw new ArgumentException(errMsg, "argsDb"); } fStreamName = fStreamNameFlag.arg(); } else { string errMsg = MStringResource.getString(MetaDataRegisterMStringResources.kCreateMetadataNoStructureName); throw new ArgumentException(errMsg, "argsDb"); } //---------------------------------------- // -channelType flag // fChannelTypeFlag.parse(ref argsDb, flagChannelType); if (fChannelTypeFlag.isSet()) { if (!fChannelTypeFlag.isArgValid()) { string errMsg = MStringResource.getString(MetaDataRegisterMStringResources.kInvalidString); throw new ArgumentException(errMsg, "argsDb"); } fChannelType = fChannelTypeFlag.arg(); } else { string errMsg = MStringResource.getString(MetaDataRegisterMStringResources.kCreateMetadataNoStructureName); throw new ArgumentException(errMsg, "argsDb"); } //---------------------------------------- // (selection list) // // Commands need at least one node on which to operate so gather up // the list of nodes specified and/or selected. // // Empty out the list of nodes on which to operate so that it can be // populated from the selection or specified lists. fNodes.clear(); MSelectionList objects = new MSelectionList(); argsDb.getObjects(objects); for (uint i = 0; i < objects.length; ++i) { MDagPath dagPath = new MDagPath(); objects.getDagPath((uint)i, dagPath); MFnDagNode dagNode = new MFnDagNode(dagPath.node); MObject obj = dagNode.child(0); if (obj.apiTypeStr == "kMesh") { if (obj == MObject.kNullObj) { throw new ApplicationException("Error: objects.getDependNode() "); } fNodes.append(dagPath.node); } else { String fmt = MStringResource.getString(MetaDataRegisterMStringResources.kObjectTypeError); String msg = String.Format(fmt, dagPath.fullPathName + "[" + obj.apiTypeStr + "]"); displayError(msg); throw new System.InvalidOperationException(msg); } } if (fNodes.length == 0) { string errMsg = MStringResource.getString(MetaDataRegisterMStringResources.kObjectNotFoundError); throw new ArgumentException(errMsg, "argsDb"); } }
//====================================================================== // // Do the metadata creation. The metadata will be randomly initialized // based on the channel type and the structure specified. For recognized // components the number of metadata elements will correspond to the count // of components in the selected mesh, otherwise a random number of metadata // elements between 1 and 100 will be created (at consecutive indices). // // The previously existing metadata is preserved for later undo. // override public void doIt(MArgList args) { MArgDatabase argsDb = new MArgDatabase(syntax, args); checkArgs(ref argsDb); clearResult(); uint numNodes = fNodes.length; int i; for (i = 0; i < numNodes; ++i) { // fNodes[i] is the transform not the shape itself MFnDagNode dagNode = new MFnDagNode(fNodes[i]); MObject obj = dagNode.child(0); // obj is the shape, which is where we can add the meta data MFnDependencyNode node = new MFnDependencyNode(obj); // Get the current metadata (empty if none yet) Associations newMetadata = new Associations(node.metadata); Channel newChannel = newMetadata.channel(fChannelType); // Check to see if the requested stream name already exists Stream oldStream = newChannel.dataStream(fStreamName); if (oldStream != null) { string fmt = MStringResource.getString(MetaDataRegisterMStringResources.kCreateMetadataHasStream); string msg = String.Format(fmt, fStreamName); MGlobal.displayError(msg); continue; } Stream newStream = new Stream(fStructure, fStreamName); string strmName = newStream.name; int indexCount = 0; MFnMesh mesh = null; Random rndIndexCount = new Random(); // Treat the channel type initializations different for meshes if (obj.hasFn(MFn.Type.kMesh)) { mesh = new MFnMesh(obj); // Get mesh-specific channel type parameters if (fChannelType == "face") { indexCount = mesh.numPolygons; } else if (fChannelType == "edge") { indexCount = mesh.numEdges; } else if (fChannelType == "vertex") { indexCount = mesh.numVertices; } else if (fChannelType == "vertexFace") { indexCount = mesh.numFaceVertices; } else { // Set a random number between 1 to 100 indexCount = rndIndexCount.Next(1, 100); } } else { // Create generic channel type information indexCount = rndIndexCount.Next(1, 100); } // Fill specified stream ranges with random data int structureMemberCount = fStructure.memberCount; uint m, n, d; Random rnd = new Random(); for (m = 0; m < indexCount; ++m) { // Walk each structure member and fill with random data // tailored to the member data type. Handle handle = new Handle(fStructure); for (n = 0; n < structureMemberCount; ++n) { handle.setPositionByMemberIndex(n); switch (handle.dataType) { case Member.eDataType.kBoolean: { bool[] data = new bool[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { int randomInt = rnd.Next(0, 2); bool randomBool = randomInt == 1 ? true : false; data[d] = randomBool; } handle.asBooleanArray = data; break; } case Member.eDataType.kDouble: { double[] data = new double[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { // Set a random number between -2000000000.0.0 and 2000000000.0.0 data[d] = rnd.NextDouble() * 4000000000.0 - 2000000000.0; } handle.asDoubleArray = data; break; } case Member.eDataType.kDoubleMatrix4x4: { double[] data = new double[handle.dataLength * 16]; for (d = 0; d < handle.dataLength; ++d) { data[d * 16 + 0] = rnd.NextDouble() * 4000000000.0 - 2000000000.0; data[d * 16 + 1] = rnd.NextDouble() * 4000000000.0 - 2000000000.0; data[d * 16 + 2] = rnd.NextDouble() * 4000000000.0 - 2000000000.0; data[d * 16 + 3] = rnd.NextDouble() * 4000000000.0 - 2000000000.0; data[d * 16 + 4] = rnd.NextDouble() * 4000000000.0 - 2000000000.0; data[d * 16 + 5] = rnd.NextDouble() * 4000000000.0 - 2000000000.0; data[d * 16 + 6] = rnd.NextDouble() * 4000000000.0 - 2000000000.0; data[d * 16 + 7] = rnd.NextDouble() * 4000000000.0 - 2000000000.0; data[d * 16 + 8] = rnd.NextDouble() * 4000000000.0 - 2000000000.0; data[d * 16 + 9] = rnd.NextDouble() * 4000000000.0 - 2000000000.0; data[d * 16 + 10] = rnd.NextDouble() * 4000000000.0 - 2000000000.0; data[d * 16 + 11] = rnd.NextDouble() * 4000000000.0 - 2000000000.0; data[d * 16 + 12] = rnd.NextDouble() * 4000000000.0 - 2000000000.0; data[d * 16 + 13] = rnd.NextDouble() * 4000000000.0 - 2000000000.0; data[d * 16 + 14] = rnd.NextDouble() * 4000000000.0 - 2000000000.0; data[d * 16 + 15] = rnd.NextDouble() * 4000000000.0 - 2000000000.0; } handle.asDoubleMatrix4x4 = data; break; } case Member.eDataType.kFloat: { float[] data = new float[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { // Set a random number between -2000000.0 and 2000000.0 data[d] = (float)rnd.NextDouble() * 4000000.0f - 2000000.0f; } handle.asFloatArray = data; break; } case Member.eDataType.kFloatMatrix4x4: { float[] data = new float[handle.dataLength * 16]; for (d = 0; d < handle.dataLength; ++d) { // Set a random number between -2000000.0 and 2000000.0 data[d * 16 + 0] = (float)rnd.NextDouble() * 4000000.0f - 2000000.0f; data[d * 16 + 1] = (float)rnd.NextDouble() * 4000000.0f - 2000000.0f; data[d * 16 + 2] = (float)rnd.NextDouble() * 4000000.0f - 2000000.0f; data[d * 16 + 3] = (float)rnd.NextDouble() * 4000000.0f - 2000000.0f; data[d * 16 + 4] = (float)rnd.NextDouble() * 4000000.0f - 2000000.0f; data[d * 16 + 5] = (float)rnd.NextDouble() * 4000000.0f - 2000000.0f; data[d * 16 + 6] = (float)rnd.NextDouble() * 4000000.0f - 2000000.0f; data[d * 16 + 7] = (float)rnd.NextDouble() * 4000000.0f - 2000000.0f; data[d * 16 + 8] = (float)rnd.NextDouble() * 4000000.0f - 2000000.0f; data[d * 16 + 9] = (float)rnd.NextDouble() * 4000000.0f - 2000000.0f; data[d * 16 + 10] = (float)rnd.NextDouble() * 4000000.0f - 2000000.0f; data[d * 16 + 11] = (float)rnd.NextDouble() * 4000000.0f - 2000000.0f; data[d * 16 + 12] = (float)rnd.NextDouble() * 4000000.0f - 2000000.0f; data[d * 16 + 13] = (float)rnd.NextDouble() * 4000000.0f - 2000000.0f; data[d * 16 + 14] = (float)rnd.NextDouble() * 4000000.0f - 2000000.0f; data[d * 16 + 15] = (float)rnd.NextDouble() * 4000000.0f - 2000000.0f; } handle.asFloatMatrix4x4 = data; break; } case Member.eDataType.kInt8: { sbyte[] data = new sbyte[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { data[d] = (sbyte)rnd.Next(SByte.MinValue, SByte.MaxValue + 1); } handle.asInt8Array = data; break; } case Member.eDataType.kInt16: { short[] data = new short[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { data[d] = (short)rnd.Next(Int16.MinValue, Int16.MaxValue + 1); } handle.asInt16Array = data; break; } case Member.eDataType.kInt32: { int[] data = new int[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { // rnd.Next returns a number between [arg1,arg2[ // but unfortunately I can't pass Int32.MaxValue+1 here.... data[d] = rnd.Next(Int32.MinValue, Int32.MaxValue); } handle.asInt32Array = data; break; } case Member.eDataType.kInt64: { long[] data = new long[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { // rnd.Next() gives a number between [0,Int32 data[d] = (long)rnd.Next(Int32.MinValue, Int32.MaxValue); if (data[d] >= 0) { data[d] *= Int64.MaxValue / Int32.MaxValue; } else { data[d] *= Int64.MinValue / Int32.MinValue; } } handle.asInt64Array = data; break; } case Member.eDataType.kUInt8: { byte[] data = new byte[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { data[d] = (byte)rnd.Next(0, Byte.MaxValue + 1); } handle.asUInt8Array = data; break; } case Member.eDataType.kUInt16: { ushort[] data = new ushort[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { data[d] = (ushort)rnd.Next(0, UInt16.MaxValue + 1); } handle.asUInt16Array = data; break; } case Member.eDataType.kUInt32: { uint[] data = new uint[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { data[d] = (uint)rnd.Next(); } handle.asUInt32Array = data; break; } case Member.eDataType.kUInt64: { ulong[] data = new ulong[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { data[d] = ((ulong)rnd.Next()) * UInt64.MaxValue / UInt32.MaxValue; } handle.asUInt64Array = data; break; } case Member.eDataType.kString: { string[] randomStrings = new string[] { "banana", "tomatoe", "apple", "pineapple", "apricot", "pepper", "olive", "grapefruit" }; string[] data = new string[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { int index = rnd.Next(randomStrings.Length); data[d] = randomStrings[index]; } handle.asStringArray = data; break; } default: { Debug.Assert(false, "This should never happen"); break; } } } newStream.setElement(new Index(m), handle); } newChannel.setDataStream(newStream); newMetadata.setChannel(newChannel); // Note: the following will not work if "obj" is a shape constructed by a source object // You need to delete the history of the shape before calling this... fDGModifier.setMetadata(obj, newMetadata); fDGModifier.doIt(); // Set the result to the number of actual metadata values set as a // triple value: // (# nodes, # metadata elements, # members per element) // MIntArray theResult = new MIntArray(); theResult.append((int)fNodes.length); theResult.append((int)indexCount); theResult.append((int)structureMemberCount); setResult(theResult); } }
private void processXform( MayaXform xform ) { // Add the current Xform to the list of all xforms. allXforms.Add(xform); // Set the Xform's id. xform.id = allXforms.Count - 1; // Add the current Xform to the map based on its path. pathXformMap[xform.name] = xform; // Build data for this xform. if( !xform.isRoot ) { getXformData(xform); } MFnDagNode dagNode = new MFnDagNode(xform.mayaObjectPath); for( uint childIdx = 0; childIdx < dagNode.childCount; ++childIdx ) { // If the child isn't a transform node, or it is a kManipulator3D (maya api bug), skip it. MObject childObj = dagNode.child(childIdx); if( !childObj.hasFn(MFn.Type.kTransform) || childObj.hasFn(MFn.Type.kManipulator3D) ) { continue; } // Create a node for the child transform, parent it to this xform, and process the child's children. MFnDagNode childNode = new MFnDagNode(childObj); // Strip out nodes we don't care about. if( xformsToIgnore.Contains(childNode.fullPathName) ) { continue; } MayaXform childXform = new MayaXform(); childXform.name = childNode.fullPathName; childNode.getPath(childXform.mayaObjectPath); childXform.parent = xform; xform.children.Add(childXform); processXform(childXform); } }
//====================================================================== // // Do the metadata creation. The metadata will be randomly initialized // based on the channel type and the structure specified. For recognized // components the number of metadata elements will correspond to the count // of components in the selected mesh, otherwise a random number of metadata // elements between 1 and 100 will be created (at consecutive indices). // // The previously existing metadata is preserved for later undo. // override public void doIt(MArgList args) { MArgDatabase argsDb = new MArgDatabase(syntax, args); checkArgs(ref argsDb); clearResult(); uint numNodes = fNodes.length; for (int i = 0; i < numNodes; ++i) { // fNodes[i] is the transform not the shape itself MFnDagNode dagNode = new MFnDagNode(fNodes[i]); MObject obj = dagNode.child(0); // obj is the shape, which is where we can add the meta data MFnDependencyNode node = new MFnDependencyNode(obj); Console.WriteLine("METADATA for node " + dagNode.fullPathName); Console.WriteLine("====================================================================="); foreach (Channel chn in node.metadata) { Console.WriteLine("Channel: type = {0}, nbStreams = {1}", chn.nameProperty, chn.dataStreamCount); foreach (Stream strm in chn) { Console.WriteLine("Stream: name = {0}, nbElements = {1}", strm.name, strm.elementCount()); Structure strct = strm.structure; Console.WriteLine("Structure: name = {0}, nbMembers = {1}", strct.name, strct.memberCount); string[] memberNames = new string[strct.memberCount]; int memberID = -1; foreach (Member member in strct) { ++memberID; Console.WriteLine("Structure member: name = {0}, type = {1}, array size = {2}", member.nameProperty, member.typeProperty.ToString(), member.lengthProperty); memberNames[memberID] = member.nameProperty; } int k = -1; foreach (Handle handle in strm) { ++k; for (uint n = 0; n < strct.memberCount; ++n) { handle.setPositionByMemberIndex(n); Array data = handle.asType; if (data.Length < 1) { throw new ApplicationException("Handle data seems corrupted"); } string line = string.Format("Handle #{0}, member = {1}, data = {2}", k, memberNames[n], data.GetValue(0).ToString()); if (data.Length > 1) { for (int d = 1; d < data.Length; ++d) { line = line + "," + data.GetValue(d).ToString(); } } Console.WriteLine(line); } } } } } }
//====================================================================== // // Do the metadata creation. The metadata will be randomly initialized // based on the channel type and the structure specified. For recognized // components the number of metadata elements will correspond to the count // of components in the selected mesh, otherwise a random number of metadata // elements between 1 and 100 will be created (at consecutive indices). // // The previously existing metadata is preserved for later undo. // override public void doIt(MArgList args) { MArgDatabase argsDb = new MArgDatabase(syntax, args); checkArgs(ref argsDb); clearResult(); uint numNodes = fNodes.length; int i; for (i = 0; i < numNodes; ++i) { // fNodes[i] is the transform not the shape itself MFnDagNode dagNode = new MFnDagNode(fNodes[i]); MObject obj = dagNode.child(0); // obj is the shape, which is where we can add the meta data MFnDependencyNode node = new MFnDependencyNode(obj); // Get the current metadata (empty if none yet) Associations newMetadata = new Associations(node.metadata); Channel newChannel = newMetadata.channel(fChannelType); // Check to see if the requested stream name already exists Stream oldStream = newChannel.dataStream(fStreamName); if (oldStream != null) { string fmt = MStringResource.getString(MetaDataRegisterMStringResources.kCreateMetadataHasStream); string msg = String.Format(fmt, fStreamName); MGlobal.displayError( msg ); continue; } Stream newStream = new Stream(fStructure, fStreamName); string strmName = newStream.name; int indexCount = 0; MFnMesh mesh = null; Random rndIndexCount = new Random(); // Treat the channel type initializations different for meshes if (obj.hasFn(MFn.Type.kMesh)) { mesh = new MFnMesh(obj); // Get mesh-specific channel type parameters if (fChannelType == "face") { indexCount = mesh.numPolygons; } else if (fChannelType == "edge") { indexCount = mesh.numEdges; } else if (fChannelType == "vertex") { indexCount = mesh.numVertices; } else if (fChannelType == "vertexFace") { indexCount = mesh.numFaceVertices; } else { // Set a random number between 1 to 100 indexCount = rndIndexCount.Next(1, 100); } } else { // Create generic channel type information indexCount = rndIndexCount.Next(1, 100); } // Fill specified stream ranges with random data int structureMemberCount = fStructure.memberCount; uint m,n,d; Random rnd = new Random(); for (m = 0; m < indexCount; ++m) { // Walk each structure member and fill with random data // tailored to the member data type. Handle handle = new Handle(fStructure); for (n = 0; n < structureMemberCount; ++n) { handle.setPositionByMemberIndex(n); switch (handle.dataType) { case Member.eDataType.kBoolean: { bool[] data = new bool[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { int randomInt = rnd.Next(0, 2); bool randomBool = randomInt == 1 ? true : false; data[d] = randomBool; } handle.asBooleanArray = data; break; } case Member.eDataType.kDouble: { double[] data = new double[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { // Set a random number between -2000000000.0.0 and 2000000000.0.0 data[d] = rnd.NextDouble()*4000000000.0 - 2000000000.0 ; } handle.asDoubleArray = data; break; } case Member.eDataType.kDoubleMatrix4x4: { double[] data = new double[handle.dataLength * 16]; for (d = 0; d < handle.dataLength; ++d) { data[d*16+0] = rnd.NextDouble()*4000000000.0 - 2000000000.0 ; data[d*16+1] = rnd.NextDouble()*4000000000.0 - 2000000000.0 ; data[d*16+2] = rnd.NextDouble()*4000000000.0 - 2000000000.0 ; data[d*16+3] = rnd.NextDouble()*4000000000.0 - 2000000000.0 ; data[d*16+4] = rnd.NextDouble()*4000000000.0 - 2000000000.0 ; data[d*16+5] = rnd.NextDouble()*4000000000.0 - 2000000000.0 ; data[d*16+6] = rnd.NextDouble()*4000000000.0 - 2000000000.0 ; data[d*16+7] = rnd.NextDouble()*4000000000.0 - 2000000000.0 ; data[d*16+8] = rnd.NextDouble()*4000000000.0 - 2000000000.0 ; data[d*16+9] = rnd.NextDouble()*4000000000.0 - 2000000000.0 ; data[d*16+10] = rnd.NextDouble()*4000000000.0 - 2000000000.0 ; data[d*16+11] = rnd.NextDouble()*4000000000.0 - 2000000000.0 ; data[d*16+12] = rnd.NextDouble()*4000000000.0 - 2000000000.0 ; data[d*16+13] = rnd.NextDouble()*4000000000.0 - 2000000000.0 ; data[d*16+14] = rnd.NextDouble()*4000000000.0 - 2000000000.0 ; data[d*16+15] = rnd.NextDouble()*4000000000.0 - 2000000000.0 ; } handle.asDoubleMatrix4x4 = data; break; } case Member.eDataType.kFloat: { float[] data = new float[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { // Set a random number between -2000000.0 and 2000000.0 data[d] = (float)rnd.NextDouble()*4000000.0f - 2000000.0f ; } handle.asFloatArray = data; break; } case Member.eDataType.kFloatMatrix4x4: { float[] data = new float[handle.dataLength * 16]; for (d = 0; d < handle.dataLength; ++d) { // Set a random number between -2000000.0 and 2000000.0 data[d*16+0] = (float)rnd.NextDouble()*4000000.0f - 2000000.0f ; data[d*16+1] = (float)rnd.NextDouble()*4000000.0f - 2000000.0f ; data[d*16+2] = (float)rnd.NextDouble()*4000000.0f - 2000000.0f ; data[d*16+3] = (float)rnd.NextDouble()*4000000.0f - 2000000.0f ; data[d*16+4] = (float)rnd.NextDouble()*4000000.0f - 2000000.0f ; data[d*16+5] = (float)rnd.NextDouble()*4000000.0f - 2000000.0f ; data[d*16+6] = (float)rnd.NextDouble()*4000000.0f - 2000000.0f ; data[d*16+7] = (float)rnd.NextDouble()*4000000.0f - 2000000.0f ; data[d*16+8] = (float)rnd.NextDouble()*4000000.0f - 2000000.0f ; data[d*16+9] = (float)rnd.NextDouble()*4000000.0f - 2000000.0f ; data[d*16+10] = (float)rnd.NextDouble()*4000000.0f - 2000000.0f ; data[d*16+11] = (float)rnd.NextDouble()*4000000.0f - 2000000.0f ; data[d*16+12] = (float)rnd.NextDouble()*4000000.0f - 2000000.0f ; data[d*16+13] = (float)rnd.NextDouble()*4000000.0f - 2000000.0f ; data[d*16+14] = (float)rnd.NextDouble()*4000000.0f - 2000000.0f ; data[d*16+15] = (float)rnd.NextDouble()*4000000.0f - 2000000.0f ; } handle.asFloatMatrix4x4 = data; break; } case Member.eDataType.kInt8: { sbyte[] data = new sbyte[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { data[d] = (sbyte)rnd.Next(SByte.MinValue, SByte.MaxValue+1); } handle.asInt8Array = data; break; } case Member.eDataType.kInt16: { short[] data = new short[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { data[d] = (short)rnd.Next(Int16.MinValue, Int16.MaxValue+1); } handle.asInt16Array = data; break; } case Member.eDataType.kInt32: { int[] data = new int[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { // rnd.Next returns a number between [arg1,arg2[ // but unfortunately I can't pass Int32.MaxValue+1 here.... data[d] = rnd.Next(Int32.MinValue, Int32.MaxValue); } handle.asInt32Array = data; break; } case Member.eDataType.kInt64: { long[] data = new long[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { // rnd.Next() gives a number between [0,Int32 data[d] = (long)rnd.Next(Int32.MinValue, Int32.MaxValue); if( data[d] >= 0 ) data[d] *= Int64.MaxValue / Int32.MaxValue; else data[d] *= Int64.MinValue / Int32.MinValue; } handle.asInt64Array = data; break; } case Member.eDataType.kUInt8: { byte[] data = new byte[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { data[d] = (byte)rnd.Next(0, Byte.MaxValue + 1); } handle.asUInt8Array = data; break; } case Member.eDataType.kUInt16: { ushort[] data = new ushort[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { data[d] = (ushort)rnd.Next(0, UInt16.MaxValue + 1); } handle.asUInt16Array = data; break; } case Member.eDataType.kUInt32: { uint[] data = new uint[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { data[d] = (uint)rnd.Next(); } handle.asUInt32Array = data; break; } case Member.eDataType.kUInt64: { ulong[] data = new ulong[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { data[d] = ((ulong)rnd.Next()) * UInt64.MaxValue / UInt32.MaxValue; } handle.asUInt64Array = data; break; } case Member.eDataType.kString: { string[] randomStrings = new string[] { "banana", "tomatoe", "apple", "pineapple", "apricot", "pepper", "olive", "grapefruit" }; string[] data = new string[handle.dataLength]; for (d = 0; d < handle.dataLength; ++d) { int index = rnd.Next( randomStrings.Length ); data[d] = randomStrings[index]; } handle.asStringArray = data; break; } default: { Debug.Assert(false, "This should never happen"); break; } } } newStream.setElement(new Index(m), handle); } newChannel.setDataStream(newStream); newMetadata.setChannel(newChannel); // Note: the following will not work if "obj" is a shape constructed by a source object // You need to delete the history of the shape before calling this... fDGModifier.setMetadata(obj, newMetadata); fDGModifier.doIt(); // Set the result to the number of actual metadata values set as a // triple value: // (# nodes, # metadata elements, # members per element) // MIntArray theResult = new MIntArray(); theResult.append( (int) fNodes.length ); theResult.append( (int) indexCount ); theResult.append( (int) structureMemberCount ); setResult( theResult ); } }
private void renameNodes(MObject transform, string baseName) { // Rename the transform to something we know no node will be using. dagMod.renameNode(transform, "polyPrimitiveCmdTemp"); // Rename the mesh to the same thing but with 'Shape' on the end. MFnDagNode dagFn = new MFnDagNode(transform); dagMod.renameNode(dagFn.child(0), "polyPrimitiveCmdTempShape"); // Now that they are in the 'something/somethingShape' format, any // changes we make to the name of the transform will automatically be // propagated to the shape as well. // // Maya will replace the '#' in the string below with a number which // ensures uniqueness. string transformName = baseName + "#"; dagMod.renameNode(transform, transformName); }
private void linkXformsMeshes() { foreach( MayaXform xform in allXforms ) { // Convert to a DAG node to traverse children. MFnDagNode dagNode = new MFnDagNode(xform.mayaObjectPath); for( uint childIdx = 0; childIdx < dagNode.childCount; ++childIdx ) { MObject childObj = dagNode.child(childIdx); // If the child has a mesh, link it. if( childObj.hasFn(MFn.Type.kMesh) ) { xform.mesh = pathMeshMap[(new MFnMesh(childObj)).fullPathName]; break; } } } }