OpenMaya2: deleting object in scene?

I’m attempting to get some really basic OpenMaya2 code going. Right now I’m just trying to delete. I haven’t found many examples online for such a simple thing.

I’m creating a bunch of locators. After I create each one, I’m just attempting to delete it. (If anyone knows of a way to delete all of them via OM that’d be great info to have too!).

I’m getting a Maya hard crash on the below code when calling mod.doIt(). In deleteNode I’ve tried the mobject, the dagpath, and dag node. Nothing seems to take. The mobject throws an error of # RuntimeError: (kInvalidParameter): Object is incompatible with this method #. I thought mobject would be the way to go?

Any help here is appreciated!

import maya.cmds as cmds
import maya.api.OpenMaya as om

for i in range(0, 99):
    
    loc = cmds.spaceLocator()
    cmds.select(loc, add=True)
    
    loc_sl = om.MGlobal.getActiveSelectionList()
    ldag, lmobj = loc_sl.getComponent(0)
    om.MDagPath
    mod = om.MDGModifier()
    
    mod.deleteNode(ldag.node())
    mod.doIt() # <----crash!

First off, you’d likely want to delete them in batch afterwards - so that the MDGModifier triggers once. You can just stack deletes and trigger doIt afterwards. Likely that also saves you from the crash since the crash could be related to garbage collection happening early, but I’m not sure.

import maya.cmds as cmds
import maya.api.OpenMaya as om

mod = om.MDGModifier()
for i in range(0, 99):
    
    loc = cmds.spaceLocator()
    cmds.select(loc, add=True)
    
    loc_sl = om.MGlobal.getActiveSelectionList()
    ldag, lmobj = loc_sl.getComponent(0)
    
    mod.deleteNode(ldag.node())
    
mod.doIt()

This doesn’t crash for me.

Slightly simplified:


import maya.cmds as cmds
import maya.api.OpenMaya as om

mod = om.MDGModifier()
for i in range(0, 99):
    
    loc = cmds.spaceLocator()
    cmds.select(loc, add=True)
    
    sel = om.MGlobal.getActiveSelectionList()
    node = sel.getDependNode(0)
    
    mod.deleteNode(node)
    
mod.doIt()

Note that without MDgModifer there is also MGlobal.deleteNode() which works fine, doesn’t crash in this particular case plus actually updates the viewport so that it doesn’t display the deleted nodes, hehe.


import maya.cmds as cmds
import maya.api.OpenMaya as om


for i in range(0, 99):
    
    loc = cmds.spaceLocator()
    cmds.select(loc, add=True)
    
    sel = om.MGlobal.getActiveSelectionList()
    node = sel.getDependNode(0)
    
    om.MGlobal.deleteNode(node)

Sorry for not pointing to where the crash is originating from, but hope this at least allows you to continue.

1 Like

Thanks @BigRoyNL I really do appreciate it, and thank you for the tips. These new versions do work.

I was hoping you should shed light on another version. I’m trying to create the locator using OpenMaya2 as well. But I’m getting an Unexpected Internal Failure error for some reason.

import maya.api.OpenMaya as om

mod = om.MDGModifier()

for i in range(10):

    mDagMod = om.MDagModifier()
    loc_mobj = mDagMod.createNode("locator")
            
    mod.deleteNode(loc_mobj )
    
mod.doIt()

Pretty sure you need to call doIt right after your create the node in this scenario.

Nothing is actually created in the scene until you call doIt() - if you consider how the python script will run through vs the actual state of the scene, you’re stacking commands to alternately:

  • create a new node and return a reference to what it will be (which I guess here is just an invalid MObject)
  • delete that object, which is invalid, and which also doesn’t exist yet.

sorry to necro this, but then what’s the “proper” option then? Because using MGlobal doesn’t allow for undoIt() but updates the viewport, while MDGModifier() allows you to undoIt() but doesn’t update the viewport, or the outliner/ui.

Any thoughts on how MDGModifier() and update the UI/Viewport?

That’s because the script does nothing, at least how it was described, within an undo environment (especially if you want to see them in the viewport before being deleted!).

with undo

  • create node
  • delete node

As pointed out earlier, if you need to do something with the node(s) before deleting them, you need to run in three blocks; one where you create them all, use them, then delete them all. At each block, you will need to run a doIt().

It sounds like you have an intention, but are not explaining what it is. Try outlining what your high level goal is.