This page provides Python code examples for maya.cmds.ls.
Posted by2 years ago
Archived
I'm trying to write a small script that takes the attribute from a shader on the first selected object, and applies it to the shader of a second selected object.
So, this translates the z position from the first selected object to the second. However, when I select and object it selects the geo, not the shader. How do I grab the attribute of the shader of the selected object '.color' of a vray shader for instance? Not the shading group, which is only marginally helpful, but the actual shader itself.
I'm new to coding. Please advise. Thanks!!
23 comments
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:
(Edit > Select All by Type > Geometry)
Could anyone translate this to Python?
Ramon BlanquerRamon Blanquer13011 gold badge66 silver badges1919 bronze badges
3 Answers
What you're seeing is the procedure
SelectAllGeometry
, and its contents:That command is several parts. The part in the backquotes:
Is actually a MEL procedure. Run the command
help listTransforms
to see the path to the .mel file. Reading that, the command is actuallyThe output of that is the argument to:
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:
And as Python, that would be:
Expanded out to be just a tad more readable:
mhlestermhlester18.6k99 gold badges3838 silver badges6161 bronze badges
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
theodoxtheodoxlistRelatives -p
. So the total would be10.1k22 gold badges1616 silver badges3434 bronze badges
Alex M2,22077 gold badges2020 silver badges2929 bronze badges
Pramod NirwanPramod Nirwan