Skip to content

.objectType() – Get the type of a Maya object using Python

This is used to find the type of an object.  Let’s take a look as some example code:

If you intend to run this code, be sure to add a few objects to your maya scene, for this example we have added 3 different mesh objects (cone, 2x cube, cylinder).

from maya import cmds
selection = cmds.ls(selection=True)

if len(selection) == 0:
  selection = cmds.ls(dag=True, long=True)

selection.sort(key=len, reverse=True)

for obj in selection:
  shortName = obj.split("|")[-1]

Let’s add a command to print the object type for each of the objects in the for loop above:

from maya import cmds
selection = cmds.ls(selection=True)

if len(selection) == 0:
  selection = cmds.ls(dag=True, long=True)

selection.sort(key=len, reverse=True)

for obj in selection:
  shortName = obj.split("|")[-1]

  print cmds.objectType(obj)

Notice the indentation?, the print command is within the for loop, so in each iteration, the command will print the objectType for each object.  When we run this script you’ll notice that the output is ‘transform’, why is that? 

This is just how Maya represents it objects, however is this really useful to us?  See .listRelatives()

To get the actual object type (mesh, joint, nurb etc..), we need to add a few more bits to our code (here is the full commented code):

# Load the maya commands library
from maya import cmds

# Create a new variable 'selection'
# Store in it the currently selected objects
selection = cmds.ls(selection=True)

# If the selection contains 0 lists
if len(selection) == 0:
    
    # Then get all objects in the outliner and assign to 'selection'
    # long=True : Gives us the full path to the object, so we can determine any parents.
    # dag=True : We will only get objects which are listed in the outliner, and none of the hidden objects inside of Maya.
    selection = cmds.ls(dag=True, long=True)

# Sort the list now stored in 'selection'
# Sort by length
# In reverse, giving us the longest path first
selection.sort(key=len, reverse=True)

# For each object now in the 'selection' variable
for obj in selection:
    
    # Split the path by "|" (pipe)
    # Store in a new variable 'shortName'
    shortName = obj.split("|")[-1]

    # Lets create a new variable to store the relatives
    # We want to list the relatives, we only want the children, and we want the full path
    # If there are no relative, return an empty list (or []), so that we always get a consistent object type output
    children = cmds.listRelatives(obj, children=True, fullPath=True) or []
    
    # If the length of the children is exactly 1 (remember this is in a for loop)
    if len(children) == 1:
        
        # Then put the object at position 0 in the chidren list into a new variable 'child'
        child = children[0]
        
        # Place the objecttype of the child variable in the variable 'objtype'
        objtype = cmds.objectType(child)
    else:
        
        # Otherwise if the length of children is NOT 1 (so basically 0)
        # Put the object type of the object in the objtype variable
        objType = cmds.objectType(obj)
        
    # print the objtype variable
  print objType

If we run the above code the output will be something like this:

  • mesh
    mesh
    nurbsCurve
    camera
Published inMayaObjectsPyCharmPythonscripting