OpenMaya API 2.0: How to use OpenMaya.MGlobal.isSelected(mObject)?

OpenMaya API 2.0: How to use OpenMaya.MGlobal.isSelected(mObject)?

1554195645
Explorer Explorer
1,582 Views
3 Replies
Message 1 of 4

OpenMaya API 2.0: How to use OpenMaya.MGlobal.isSelected(mObject)?

1554195645
Explorer
Explorer

I'm confused about the usage of OpenMaya.MGlobal.isSelected(). I want to use this method to detect if an mObject is selected in the viewport, but this method always returns False.

 

I'm using the python code below in Maya's Script Editor to test the method:

 

import maya.api.OpenMaya as OpenMaya
selectionList = OpenMaya.MGlobal.getActiveSelectionList() # Use this to get a selection
                                                          # list containing all selected
                                                          # objects
mObject1 = selectionList.getDependNode(0) # Get the first OpenMaya.MObject instance in
                                          # the selection list, mObject1 is of type
                                          # OpenMaya.MObject
print(OpenMaya.MGlobal.isSelected(mObject1)) # I think it will return True,
                                             # because mObject1 is from the
                                             # selectionList, but it returns False

 

You can view the test scene in the attachment.

OpenMaya.MGlobal.isSelected() is documented on the page below: 

https://help.autodesk.com/view/MAYAUL/2022/CHS/?guid=Maya_SDK_py_ref_class_open_maya_1_1_m_global_ht...

0 Likes
Accepted solutions (1)
1,583 Views
3 Replies
Replies (3)
Message 2 of 4

brentmc
Autodesk
Autodesk
Accepted solution

Hi,

MGlobal.isSelected only checks for dependency node selections but when you select a 3D object it is the dag path that is selected so you really need to use MSelectionList.hasItem and pass a dag path as shown below:

import maya.api.OpenMaya as OpenMaya
selectionList = OpenMaya.MGlobal.getActiveSelectionList()
mDagPath1 = selectionList.getDagPath(0)
print(selectionList.hasItem(mDagPath1))

 

Brent McPherson
Principle Software Developer
Message 3 of 4

1554195645
Explorer
Explorer

Thank you! It's really helpful and detailed. I have realized that my understanding of dependency node and dag path is wrong😂

0 Likes
Message 4 of 4

1554195645
Explorer
Explorer
Thank you!! It's really helpful and detailed. I realize that my understanding of dependency node is wrong~
0 Likes