To exclude some errors, which may occur in my script i need to make sure that selection, that user makes does not include anything except meshes or grouped meshes.
Using few examples i was able to figure out how to query type of selected object.
Attaching my code, which returns me type of each selected object (well, not type but rather more important categories for me - mesh, not mesh, group and empty group)
My initial idea was that i would use something like any to check if there is any no_meshes in my selection and would warn user that he needs to exclude those objects from the selection.
What i have now works fine with single objects, but i can’t wrap my head around the way i should deal with group hierarchy, since there might be meshes inside each group or combination of meshes and not meshes inside the groups.
Also it returns group name for each object in the group… which is obviously not the correct way to do it…
so, my first idea was to use listRelatives(allDescendents=True), but i am not sure how…
Would appreciate any piece of advice, i am kinda lost here.
Probably there are better ideas that will allow to accomplish my initial goal of making sure that user doesn’t have anything extra in the selection except meshes and groups.
import pymel.core as pm
current_selection = pm.selected()
# Check if anything selected
if not current_selection:
print 'Nothing is selected'
elif current_selection:
for each_object in current_selection:
# First check if there is a Shape
if each_object.getShape():
if each_object.getShape().nodeType() == 'mesh':
# object is a mesh
print('{0} is a polygon mesh'.format(each_object))
else:
# object is not a mesh
print('{0} is not a polygon mesh'.format(each_object))
# Check if object is a group
elif not each_object.getShape():
children = each_object.getChildren()
if children:
for each_child in children:
if type(each_child) is not pm.nodetypes.Transform:
# object is something else
print('{0} is something else'.format(each_object))
else:
# object is a group
print('{0} is a group'.format(each_object))
else:
# object is an empty group
print('{0} is a an empty group'.format(each_object))