Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

Loop over selected objects and print their polygon (triangle) count

Loop over selected objects and print their polygon (triangle) count

Anonymous
Not applicable
5,769 Views
2 Replies
Message 1 of 3

Loop over selected objects and print their polygon (triangle) count

Anonymous
Not applicable

I'm writing a script that with a selected number of objects, writes out the name of each selected object along with it's individual poly count. So far so good. Only I can't figure out how to do that on an object by object basis. The poly count is added in total and not done individually.

 

Here's what I have

 

import maya.cmds as cmds
import sys

# List all selected objects
selectAll = cmds.ls( selection = True )

if len(selectAll) < 1:
    print "No selection"
else:
 
    for obj in selectAll:
    
    print obj
    
    # query the number of faces
    faces = cmds.polyEvaluate( f = True )

    # query the number of triangles
    triangles = cmds.polyEvaluate( t = True )

    print "Selected triangles: " + str(triangles)

    results = "Selected triangles: " + str(total_faces)

    sys.stdout.write(results)
 

0 Likes
5,770 Views
2 Replies
Replies (2)
Message 2 of 3

haggi_master
Advocate
Advocate

Your code is a bit confusing, maybe it is only a copy/paste problem but after the "for obj in..." should be a indentation:

 

import maya.cmds as cmds
import sys

# List all selected objects
selectAll = cmds.ls( selection = True )

if len(selectAll) < 1:
    print "No selection"
else:
 
    for obj in selectAll:
    
        print obj
    
        # query the number of faces
        faces = cmds.polyEvaluate( f = True )

        # query the number of triangles
        triangles = cmds.polyEvaluate( t = True )

        print "Selected triangles: " + str(triangles)

        results = "Selected triangles: " + str(total_faces)

        sys.stdout.write(results)
Message 3 of 3

RFlannery1
Collaborator
Collaborator

For many Maya commands, if you don't specify any object(s), then the command will run on whatever is selected.  This is what is happening in your case.  (In general it is better to include the object(s) in the call to the Maya command.  Sometimes something else in the script can change the selection, and the command won't give the results you expect.  But if the objects are explicitly named in the command, then it will return the correct result no matter what is selected.)

 

So, for this specific case, the problem can be fixed by doing something like this:

for obj in selectAll:
    faces = cmds.polyEvaluate(obj, f=True)