check if selected object is a poly mesh

6,639 views
Skip to first unread message

s...@weacceptyou.com

unread,
May 6, 2016, 4:54:53 PM5/6/16
to Python Programming for Autodesk Maya
Hi sorry, im guessing this is a simple one. But just wanted to know the simplest way of doing this.

i want to check that the object i have selected is a polygon mesh. I assumed i had to use the 'objectType' command. but it seems you have to provide the name of the shape node of the object first. If there is a pymel way of doing it that is also fine,

any help would be great,
thanks,
Sam

Fredrik Averpil

unread,
May 6, 2016, 5:08:32 PM5/6/16
to Python Programming for Autodesk Maya

Not at a computer now but... This gets you the selected objects:

import maya.cmds as cmds
selection = cmds.ls(sl=True)

// Fredrik


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/3a332d6c-0659-4d67-98e1-9b2caabeb760%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Justin Israel

unread,
May 6, 2016, 5:25:07 PM5/6/16
to python_in...@googlegroups.com
On Sat, May 7, 2016 at 8:54 AM <s...@weacceptyou.com> wrote:
Hi sorry, im guessing this is a simple one. But just wanted to know the simplest way of doing this.

i want to check that the object i have selected is a polygon mesh. I assumed i had to use the 'objectType' command. but it seems you have to provide the name of the shape node of the object first. If there is a pymel way of doing it that is also fine,

Here are two different commands that give you back the selected objects that are polygon mesh

cmds.filterExpand(sm=12, fp=True)

cmds.ls(sl=True, dag=True, type="mesh", long=True)

 

any help would be great,
thanks,
Sam

s...@weacceptyou.com

unread,
May 6, 2016, 5:33:17 PM5/6/16
to Python Programming for Autodesk Maya
thanks aloot guys;)

Sam

simonp91

unread,
May 6, 2016, 5:38:41 PM5/6/16
to python_in...@googlegroups.com
You can use a nodeType query, but remember, a transform node can contain more than one shape node. Also, you might want to query multiple selected objects. Not at the machine for syntax testing, but it would look something like this :

import pymel.core as pm
for o in pm.ls(selection=True):
for shp in pm.listRelatives(o, children=True, sh=True):
if pm.nodeType(shp, q=True)=='mesh':
print '%s is a poly mesh' % shp

...can't remember off the top of my head if you need an extra flag in the nodeType query to return its proper type, but if it doesn't return the mesh/es, just check the command in the maya python docs (is the same in maya cmds and pymel) and you'll see straight away.

Simon

Sent from my iPhone

Geordie Martinez

unread,
May 7, 2016, 12:37:17 AM5/7/16
to python_inside_maya

here are two more ways to add to the ways listed here.

import pymel.core as pm

# pm.selected returns a list of selected items
for x in pm.selected():

    # if you can get a shape node continue
    if x.getShape():

        # if the shapenode type is mesh then the test is good
        if x.getShape().nodeType() == 'mesh':
            print("Yep {0} is indeed a polygon mesh".format(x))

# returning only a list of meshes selected. 
# same as above but uses a fancy pants list comprehension
justMeshes = [y for y in [ x for x in pm.selected() if x.getShape()] if y.getShape().nodeType() == 'mesh']

Geordie Martinez

unread,
May 7, 2016, 12:41:43 AM5/7/16
to python_inside_maya

Wow. I don’t think I’ve ever seen/used filterExpand in any code. that’s actually quite a useful command. 

I wonder how I've missed that all these years. 

Thanks Justin! Always a fount of knowledge!

Fredrik Averpil

unread,
May 7, 2016, 3:37:48 AM5/7/16
to python_inside_maya
cmds.filterExpand had escaped me as well. Nice!

It bothers me a lot that that when you perform a query like e.g. the cmds.ls one, the return is of type list. But if there are no results, you get a return of type None. I don't know if it's just me, but I would have preferred getting an empty list back; [].

What is the general practice on this in Python?
Perhaps there's a PEP I should read?
I'm afraid I've learnt how to do this wrong and am still doing it wrong... since when I code my own stuff, I return empty lists rather than None.

// Fredrik


Justin Israel

unread,
May 7, 2016, 4:00:07 AM5/7/16
to python_in...@googlegroups.com
On Sat, May 7, 2016 at 7:37 PM Fredrik Averpil <fredrik...@gmail.com> wrote:
cmds.filterExpand had escaped me as well. Nice!

It bothers me a lot that that when you perform a query like e.g. the cmds.ls one, the return is of type list. But if there are no results, you get a return of type None. I don't know if it's just me, but I would have preferred getting an empty list back; [].

I feel like a function shouldn't return different types, unless properly documented that it would do so. It kinda bothers me too that you could get back either a list or None
 

Rémi Deletrain

unread,
May 9, 2016, 11:16:21 AM5/9/16
to Python Programming for Autodesk Maya, s...@weacceptyou.com
With PyMel you have this solution:

maya_node = pmc.selected()[0]
or
maya_node = pmc.PyNode("YourNode")

# Check if given node is pymel node
if isinstance(maya_node , pmc.PyNode):
    # Do somthing

or

# Check if given node is pymel mesh node
if isinstance(maya_node , pmc.nodetype.Mesh):
    # Do somthing

# Check if given node is Mesh or NurbsSurface or ...
if isinstance(maya_node , (pmc.nodetype.Mesh, pmc.nodetype.NurbsSurface , ...)):
    # Do somthing
Reply all
Reply to author
Forward
0 new messages