om2-BeginnersGuide 01 - MObject/MObjectHandle
Disclaimer:
All code here is provided as is without support.
Use at your own risk! These posts assume you have some knowledge of import/running python script in maya. If Gifs/Images are not displaying in Chrome try a different browser.

If you were following from the first post you’d have been pointed to this;

API_MObject 2018

If you didn’t read it, here it is;

“Access to all Maya objects (curves, surfaces, DAG nodes, dependency graph nodes, lights, shaders, textures, etc.) is done through a handle object called an MObject. This handle provides a few simple methods to help determine the object type (see the MObject class documentation for a complete description). The MObject destructor does not delete the Maya object that it references—calling the destructor only deletes the handle object, thereby maintaining ownership.

IMPORTANT: You should never keep a pointer to an MObject between “runs” of the plug-in. Instead you may want to use an MObjectHandle since this object contains information on the validity of the MObject.”

The last part is pretty important, if you’re expecting to use the MObject right away you might get away with passing it along immediately, but if you’re expecting it to hang around for future reference;

myMObjH = om2.MObjectHandle(myMObj)

Finding out what kind of MObject you have

At some stage you’re going to want to check the type of MObject you have. Is it a transform? skinCluster?

To do this you can use;

# Returns the function set type for the object. Returns: MFn type constant
# myMObj.apiType()

# Tests whether object is compatible with the specified function set.
# myMObj.hasFn()

# eg
from maya.api import OpenMaya as om2
curSel = om2.MGlobal.getActiveSelectionList()
myMObj = curSel.getDependNode(0)

# looking for a transform MFn.kTransform
if myMObj.apiType() == om2.MFn.kTransform:
    print("Ya!")

# Or can it be used as part of the dagNode function set?
if myMObj.hasFn(om2.MFn.kDagNode):
    print("Ya!")