Inside Sabertooth
Learn how Sabertooth uses 3ds Max to create 3D interactive projects, including HBO Go’s Game of Thrones interactive experience
  • 1/3
You are here: Forum Home / Autodesk® Maya® / Python / Check object type?
  RSS 2.0 ATOM  

Check object type?
Rate this thread
 
57697
 
Permlink of this thread  
avatar
  • arnet
  • Posted: 06 July 2011 03:09 AM
  • Total Posts: 2
  • Joined: 12 March 2011 09:14 AM

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.getActiveSelectionListselList )
    
selListIter OM.MItSelectionListselList )
    
    while 
not selListIter.isDone():
     
dagpath OM.MDagPath()
     
component OM.MObject()
     
dagnode OM.MFnDagNode()
     
selListIter.getDagPathdagpathcomponent )
     
dagnode.setObjectdagpath )
     
     
# 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)



Replies: 0
avatar
  • arnet
  • Posted: 06 July 2011 10:42 PM

can’t anyone help me?

I really need some suggestions..



Replies: 0
avatar
  • adrifre7
  • Posted: 23 July 2011 10:45 PM

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[i] insted of curr objects
    
currObj=sel[i]
    
#selecte each item's hierarchy
    
mc.select(currObj,hi=True)
    
#from this selction, keep only the first shape node
    
currShape mc.lssl=Trueshapes=True)[0]
    
    
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(currShapeisType="locator")):
        print 
currShape+" is a locator "
    
elif(mc.objectType(currShapeisType="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



Replies: 0
avatar
  • haggi
  • Posted: 25 July 2011 11:20 PM

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"


Replies: 0
avatar
  • haggi
  • Posted: 25 July 2011 11:36 PM

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[i]
    mc
.select(currObj,hi=True)
    
currShape mc.lssl=Trueshapes=True)[0]
    
    
print currShape
    
print mc.ls(currShape,showType=True)
    
    if(
mc.objectType(currShapeisType="locator")):
        print 
currShape+" is a locator "
    
elif(mc.objectType(currShapeisType="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..."


Replies: 0
avatar
  • adrifre7
  • Posted: 27 July 2011 05:15 AM

Wow, that’s cool i’ll try that, thanx.

Where can I get the api of pymel?



Replies: 0