Add metadata to your mesh using MEL scripting

To add metadata to your mesh, follow the guidelines below:

  1. Create a data structure and define its name, its members and their corresponding data type using the dataStructure command. The data structure can have multiple members.

    In this example, you define a data structure named ColorStruct with the member Color, which is of type float[3], and the member ID, which is of type int32.

    dataStructure -format "raw" -asString "name=ColorStruct:float[3]=Color:int32=ID";

    Once created, data structures cannot be modified.

  2. Use the addMetadata command to create an empty stream to add to an object (-streamName flag). Specify the channel type (-channelName flag) to which the metadata should be added, such as the predefined mesh channels: vertex, edge, face, or vertexFace; or, a name of your choosing for general metadata. Specify the data structure that defines the metadata to be added to the object (-structure flag).

    You can add multiple streams to the same channel and to the same object.

    The same stream can be shared among objects.

    Each stream can include multiple members - the members of which are defined in the data structure. Only one data structure is allowed per stream.

    In this example, you create two streams, the data of which is defined by the ColorStruct data structure. You add these streams to the vertex channel of both meshes.

    addMetadata -streamName "StreamOne" -channelName "vertex" -structure "ColorStruct" smcPlaneShape smcPlane1Shape; 
    addMetadata -streamName "StreamTwo" -channelName "vertex" -structure "ColorStruct" smcPlaneShape smcPlane1Shape; 
  3. Set or remove metadata values on your object using the editMetadata command. Specify the stream that you want to populate (for example, the stream you created in step 2 above) using the -streamName flag and specify the member of the stream using the -memberName flag.

    Use the -value or -stringValue flags to specify your metadata. You can assign metadata to components using their index values, or by selecting them. Streams on the special mesh channels (that is, channels that are implicitly associated with mesh components: vertex, face, edge, and faceVertex) can be edited by selecting the components to which the metadata is to be added, rather than using the index flag. Other Streams must be edited using the index flag as they are not associated with any particular geometry element.

    In this example, you add metadata to both the Color and ID members of StreamOne and then StreamTwo. Metadata is added to the vertices by referring to their index values.

    editMetadata -streamName "StreamOne" -memberName "Color" -value 2 -value 0.5 -value 1 -index 1 smcPlaneShape;
    editMetadata -streamName "StreamOne" -memberName "ID" -value 17 -index 1 smcPlaneShape;
    editMetadata -streamName "StreamTwo" -memberName "Color" -value -2 -value -0.5 -value 2.5 -index 2 smcPlaneShape; 
    editMetadata -streamName "StreamTwo" -memberName "ID" -value 3 -index 3 smcPlaneShape; 

    In the example above, both streams (and thus the vertex channel) obtained their values from the same data structure. However, you can also add different data structures to the same channel by assigning a different data structure to each stream.

    For example,

    //Create two data structures: ColorStruct and NameStruct
    dataStructure -format "raw" -asString "name=ColorStruct:float[3]=Color:int32=ID";
    dataStructure -format "raw" -asString "name=NameStruct:string=Name:int32=ID";
    
    //Assign the ColorStruct data structure to StreamOne
    addMetadata -streamName "StreamOne" -channelName "vertex" -structure "ColorStruct" smcPlaneShape smcPlane1Shape; 
    //Assign the NameStruct data structure to StreamTwo
    addMetadata -streamName "StreamTwo" -channelName "vertex" -structure "NameStruct" smcPlaneShape smcPlane1Shape; 
     
    //Assign metadata to the Color and ID members of StreamOne/ColorStruct on smcPlaneShape
    editMetadata -streamName "StreamOne" -memberName "Color" -value 2 -value 0.5 -value 1 -index 1 smcPlaneShape;
    editMetadata -streamName "StreamOne" -memberName "ID" -value 17 -index 1 smcPlaneShape;
    //Assign metadata to the Name and ID members of StreamTwo/NameStruct on smcPlaneShape
    editMetadata -streamName "StreamTwo" -memberName "Name" -stringValue "cube1" -index 2 smcPlaneShape; 
    editMetadata -streamName "StreamTwo" -memberName "ID" -value 3 -index 3 smcPlaneShape;
    
    //Assign metadata to the Color and ID members of StreamOne/ColorStruct on smcPlane1Shape
    editMetadata -streamName "StreamOne" -memberName "Color" -value 127 -value 255 -value 212 -index 1 smcPlane1Shape;
    editMetadata -streamName "StreamOne" -memberName "ID" -value 8 -index 1 smcPlane1Shape;
    //Assign metadata to the Name and ID members of StreamTwo/NameStruct on smcPlane1Shape
    editMetadata -streamName "StreamTwo" -memberName "Name" -stringValue "cube2" -index 2 smcPlane1Shape; 
    editMetadata -streamName "StreamTwo" -memberName "ID" -value 36 -index 3 smcPlane1Shape;
Note: If a polyShape node has an incoming connection on its inMesh attribute, then the metadata for that shape will be provided from that same incoming connection. In general, this is due to construction history or deformation. However the metadata is not modified as it passes through the DG, so any intervening node, such as a delete vertex or hard edge modification that changes the topology of the mesh, will not modify the metadata to mesh component associations, and the metadata remains associated with the same components to which it was originally applied, leading to unexpected results (for example, the metadata is attached to a vertex that doesn’t exist, or metadata that started on one vertex and ended up on a different one due to changing vertex indices). Use the editMetadata command to insert an editMetadata node into history to manually fix any erroneous metadata associations.