Objects

MEL is most commonly used to create and edit objects. This section covers the variety of commands devoted to objects. As mentioned earlier, all Maya objects are actually Dependency Graph nodes, so the process of creating and manipulating objects really involves creating and manipulating nodes. Fortunately you don't have to know the exact details of each object or node in order to use it. There are a lot of MEL commands designed specifically to access objects without your knowing their inner workings. Some commands can also operate on a variety of different objects.

3.4.1 Basics

The following covers some of the basics of object creation and manipulation using MEL:

1. Open the Primitives.ma scene.

The scene contains three NURBS primitives: a cone, a sphere, and a cylinder. A very common operation is to get a complete list of objects in the scene.

  1. Open the Script Editor.
  2. Execute the following:

The complete list of objects in the scene is printed.

  • Result: timel renderPartition renderGlobalsListl defaultLight ... To get a list of just the surface shapes, execute the following. Is -type surfaceShape The result is as follows:
  • Result: nurbsConeShapel nurbsCyl inderShapel nurbsSphereShapel //

You often want a list of the currently selected objects.

  1. Select the nurbsConel object.
  2. Execute the following:

Is -selection; The result is:

To delete an object, use the del ete command.

6. Execute the following:

delete nurbsConel;

Alternatively, to delete the currently selected objects, simply use del ete without any arguments. To rename an object, the rename command is used.

7. Execute the following: rename nurbsSpherel ball;

The nurbsSpherel object has been renamed to ball. To determine if an object with a given name exists, use the objExists command.

8. Execute the following:

print( "ball does exist" );

Each object has a particular type. The objectType command is used to determine the type of a given object.

9. Execute the following: objectType bal1;

The ball object is of the transform type. This means that it is a transform node. LISTALL SCRIPT

This script demonstrates how to iterate over all the objects in the scene and display their names as well as their types.

print( "\nNodes..." ); string $nodes[] = ~ls';

string SnodeType - "objectType $node~;

print ("\nNode: " + $node + " (" + SnodeType + ")"); }

listAllO;

The procedure, 1 i stAl 1, is first defined. This procedure doesn't take any parameters or return any values.

Having printed out the text, Nodes..., on a new line, the next step is to get the complete list of nodes in the scene. The result of the 1 s command is stored in the Snodes array. Notice that you had to enclose the Is command with the single back quotes 0) in order to get its return value.

You then iterate over every item in the list.

For each node you then get its type using the objectType command. The resulting type is stored in the SnodeType variable.

string SnodeType - "objectType Snode";

Finally, the node's information is then printed out. The information is the result of concatenating a series of literal strings as well as the string variables you stored earlier. It is very important that you enclose the entire statement in parentheses before calling the print command.

print ("\nNode: " + Snode + " (" + SnodeType + ")");

When run, inside the Primitives.ma scene, the script generates the following output:

Node: timel (time)

Node: renderPartition (partition)

Node: renderGlobalsListl (renderGlobalsList)

Node: defaul tLi ghtLi stl (defaul tLi ghtLi st)

Node: defaultShaderListl (defaultShaderList)

Node: postProcessListl (postProcessList)

Node: defaul tRenderUti 1 i tyLi stl (defaul tRenderllti 1 i tyLi st) Node: 1i ghtLi stl (1i ghtLi st)

Node: defaultTextureListl (defaultTextureList) Node: lambertl (lambert) ... conti nues

3.4.2 Hierarchies

All surface shapes have a parent transform node. The surface shape node is therefore considered the child of the transform node. It is possible to have a transform node as the parent of another transform node. In fact you can create an arbitrary hierarchy of nodes with any number of children. Each of these children, in turn, could have their own children.

MEL provides several commands to create and navigate an object's hierarchy.

  1. Open the Primitives.ma scene.
  2. In the Script Editor, execute the following:

group -name topTransform nurbsSpherel;

The group command is used to create a new transform node. In this case it is named topTransform and has the nurbsSpherel added as a child. The parent command can be used to make a given node a child of another node.

parent nurbsConel topTransform;

The nurbsConel node is now a child of the topTransform node. The hierarchy now appears as shown in Figure 3.3.

If the topTransform node is moved, scaled, or rotated, all the children are affected. Execute the following:

move -relative 0 3 0 topTransform;

Both the cone and sphere are moved upwards. To not have the parent's transform affect a child, use the inheritTransform command to turn this off.

inheritTransform -off nurbsConel;

The cone returns to its original position. Moving the topTransform from now on has no effect on the cone, but it does affect the sphere. To see a list of the children of a node, use the following:

1istRelatives topTransform; The result is:

// Result: nurbsSpherel nurbsConel //

To get a complete list of all children, including grandchildren, use the -all Descendents flag. To get a list of all shapes under a node, use the -shapes flag, as follows:

1istRelatives -shapes nurbsSpherel; The result is:

// Result: nurbsSphereShapel //

Given a node, it is also possible to get its parents as follows: 1istRelatives -parent nurbsSphereShapel;

FIGURE 3.3 Node hierarchy

The result is:

If the object was instanced, getting all the possible parents can be done using the - al 1 Pa rents flag. To change the order of the siblings use the reorder command. Use the following to put the nurbsConel first:

reorder -front nurbsConel;

Reordering siblings doesn't have any effect on their parent-child relationship but simply changes their order in the list of children. To disconnect a child from its parent is the equivalent of making it a child of the world. The world is the topmost parent of all nodes. To disconnect the nurbsConel transform, use the following:

parent -world nurbsConel;

The ungroup command could also have been used. The equivalent would have been ungroup -world nurbsConel.

3.4.3 Transforms

Almost all the objects in a scene (cameras, lights, surfaces, and so on) are stored as DAG nodes. The majority of these DAG nodes consist of a shape node with a transform node as the parent. The transform node defines the position, orientation, and scale of their child nodes.

  1. Open the Primitives.ma scene.
  2. In the Script Editor, execute the following:

move -relative 3 0 0 nurbsSpherel;

While it may appear that the nurbsSpherel node has moved, it is in fact the nurbsSphereShapel that has. The nurbsSpherel node simply holds the information needed to move its children. This information is stored in a transformation matrix. This matrix is then used to transform the children to their new position. So in this case, the surface shape node (nurbsSphereShapel) is transformed by the matrix resulting in its new position along the x-axis.

3. Execute the following:

scale -absolute 1 0.5 1 nurbsSpherel;

The sphere is now scaled down the y-axis. Rotating objects can be done using the rotate command.

rotate -relative 45deg 0 0 nurbsSpherel;

Alternatively the translation, rotation, and scale can all be applied at once by using the xform command. Execute the following:

xform -relative -translation 0-2 3 -scale 0.5 1 1 -rotation 0 0 lOdeg nurbsSpherel;

The xform command can also be used to perform a shear.

xform -shear 10 0

0 0

Post a comment

  • Receive news updates via email from this site