When writing mel scripts, often it is important to either select
objects in a specific order for a specific tool or command,
or you may simply need to know then names of all meshes for
instance.
Example 1 : Listing all objects within
the scene [API]
The mel command ls allows you to
list objects within the maya scene. We can then simply loop
through an array and use the node name to perform actions upon
that node. (in this case simply printing the name)
{
// declare an array called $nodes. Call ls to list
// the nodes in the scene
$nodes = `ls`;
// loop through the array and print each nodefor( $node in $nodes ) {
// print the node nameprint( $node + "\n" );
}
}
The mel command ls allows you to
list objects within the maya scene. We can then simply loop
through an array and use the node name to perform actions upon
that node. (in this case simply printing the name)
{
// declare an array called $nodes. Call ls to list
// the nodes in the scene
$nodes = `ls`;
// loop through the array and print each nodefor( $node in $nodes ) {
// use the nodeType function to determine the
// type of the node
$type = `nodeType $node`;
// print the node name and it's typeprint( $node +" "+ $type +"\n" );
}
}
ls also allows us to list nodes of
a specific given type, to do this we simply use the -type flag.
This is really useful for processing all node of type XXXX.
{
// declare an array called $nodes. Call ls to list
// the nodes in the scene
$meshes = `ls -type "mesh"`;
// loop through each meshfor( $mesh in $meshes ) {
// print the mesh nameprint( $node +"\n" );
}
}
By using the -sl flag with the ls
function, we are able to list all selected objects.
{
// declare an array called $nodes. Call ls to list
// the nodes in the scene
$selected = `ls -sl`;
// loop through each meshfor( $node in $selected ) {
// print the selected object's nameprint( $node +"\n" );
}
}