Ramon Blanquer
Ramon Blanquer

Reputation: 454

Select all by type: Geometry. Equivalent Python script?

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?

Upvotes: 2

Views: 21839

Answers (3)

Pramod Nirwan
Pramod Nirwan

Reputation: 31

It's easy:

import maya.cmds as cmds

cmds.SelectAllGeometry()

Upvotes: 3

theodox
theodox

Reputation: 12218

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;

Upvotes: 2

mhlester
mhlester

Reputation: 23251

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)

Upvotes: 9

Related Questions