Select all by type: Geometry. Equivalent Python script?

18,377

Solution 1

What you're seeing is the procedure SelectAllGeometry, and its contents:

select -r `listTransforms -geometry`;

That command is several parts. The part in the backquotes:

listTransforms -geometry

Is actually a MEL procedure. Run the command help listTransforms to see the path to the .mel file. Reading that, the command is actually

listRelatives("-p", "-path", eval("ls", $flags));

The output of that is the argument to:

select -r the_list_of_geometry_transforms

So check out Maya's MEL and Python command reference for select, listRelatives, and ls, to research how one command translates to the other:


Combining that all together, the equivalent MEL actually being called is:

select -r `listRelatives("-p", "-path", eval("ls", $flags))`

And as Python, that would be:

from maya import cmds
cmds.select(cmds.listRelatives(cmds.ls(geometry=True), p=True, path=True), r=True)

Expanded out to be just a tad more readable:

from maya import cmds
geometry = cmds.ls(geometry=True)
transforms = cmds.listRelatives(geometry, p=True, path=True)
cmds.select(transforms, r=True)

Solution 2

ls -type (or cmds.ls) use the maya node hierarchy (as laid out in the docs. So you can get all geometry shapes with ls -type geometryShape, since geometryShape is the node from which all other kinds of geometry derive. (Check the list in the link for ways to refine this by picking different types and subtypes)

To get the transforms, add a listRelatives -p. So the total would be

string $sh[] = `ls -type geometryShape`;
string $t[] = `listRelatives -p $sh`;
select -r $t;

Solution 3

It's easy:

import maya.cmds as cmds

cmds.SelectAllGeometry()
Share:
18,377
Ramon Blanquer
Author by

Ramon Blanquer

Updated on June 05, 2022

Comments

  • Ramon Blanquer
    Ramon Blanquer almost 2 years

    I was trying to find the right code to make maya select all the geometry objects in my scene. I tried to echo command while doing the operation and I get this:

    SelectAllGeometry;
    select -r `listTransforms -geometry`;
    

    (Edit > Select All by Type > Geometry)

    Could anyone translate this to Python?

  • Ramon Blanquer
    Ramon Blanquer about 10 years
    Definitely such a clear way to explain it, thank you very much it helped me a lot to understand all what is going on.