Check object type?

Check object type?

Anonymous
Not applicable
5,102 Views
5 Replies
Message 1 of 6

Check object type?

Anonymous
Not applicable
Hi,

my problem is really simple.. i need to know witch type is my selected object.
Actually i do something like:

selList = OM.MSelectionList()
OM.MGlobal.getActiveSelectionList( selList )
selListIter = OM.MItSelectionList( selList )

while not selListIter.isDone():
dagpath = OM.MDagPath()
component = OM.MObject()
dagnode = OM.MFnDagNode()
selListIter.getDagPath( dagpath, component )
dagnode.setObject( dagpath )

# check type of object selected
#
# if POLYGON: do something
#
# if LOCATOR: do something
#
#

selListIter.next()


how can I check if what I select is a Locator or a Polygon?

(to simplify the problem i can check just one of them and with an IF-ELSE statement i can continue by myself)
0 Likes
5,103 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable
can't anyone help me?

I really need some suggestions..
0 Likes
Message 3 of 6

Anonymous
Not applicable
Hello, I've been codding python in maya for a while, and its quiet messy.
The thing is... I don't know what library you are using, but my best guess is how to use it with the basic maya.cmds.
By the way thanx for posting it, I've learned more of maya python in the process, so be my guest and post things like this, as many as you can.
You helped my develop a new very usefull tool!

Ok my interpetation of what I said is this, tell me if it helped:


impot maya.cmds as mc

#get the list of selected objects, will only return the transform nodes
sel=mc.ls(sl=True)

#iterate list of selectd objects
for i in range(0,len(sel),1):

#this is for clarity you could have gone with sel insted of curr objects
currObj=sel
#selecte each item's hierarchy
mc.select(currObj,hi=True)
#from this selction, keep only the first shape node
currShape = mc.ls( sl=True, shapes=True)

print currShape
#print the shape and its type, for clarity as well
print mc.ls(currShape,showType=True)

#compare the type of shape that it is
if(mc.objectType(currShape, isType="locator")):
print currShape+" is a locator "
elif(mc.objectType(currShape, isType="mesh")):
print currShape+" is a Mesh"
#if it is neither a locator nor a mesh, i don't know what it is
else:
print "No idea of the type of the damned shape"

print"\n-------------------------"

#return to inicial conditions of selection
mc.select(sel)



I hope my explanationts in the code are helpfull for you, else tell me and I'll be happy to explain my self further
0 Likes
Message 4 of 6

haggi
Enthusiast
Enthusiast
If you have an MObject, you should be able to test for a type with:


import maya.OpenMaya as om

mobject....

if mobject.apiType() == om.MFn.kMesh:
print "its a mesh"
if mobject.apiType() == om.MFn.kLocator:
print "its a locator"
0 Likes
Message 5 of 6

haggi
Enthusiast
Enthusiast
Maybe you allow me to show some examples how to use pymel and make code more efficient.
Your original was (I removed the comments):

impot maya.cmds as mc

sel=mc.ls(sl=True)

for i in range(0,len(sel),1):

currObj=sel
mc.select(currObj,hi=True)
currShape = mc.ls( sl=True, shapes=True)

print currShape
print mc.ls(currShape,showType=True)

if(mc.objectType(currShape, isType="locator")):
print currShape+" is a locator "
elif(mc.objectType(currShape, isType="mesh")):
print currShape+" is a Mesh"
else:
print "No idea of the type of the damned shape"

print"\n-------------------------"
mc.select(sel)


I think with pymel you can do this:

import pymel.core as pm

for object in pm.ls(sl=True):
shape = object.getShape()
if shape.type() == "mesh":
print "Its a mesh..."

0 Likes
Message 6 of 6

Anonymous
Not applicable
Wow, that's cool i'll try that, thanx.

Where can I get the api of pymel?
0 Likes