Is there a callback on node removed by undo?

Is there a callback on node removed by undo?

negow
Advocate Advocate
984 Views
2 Replies
Message 1 of 3

Is there a callback on node removed by undo?

negow
Advocate
Advocate

Hi all,

 

I'd like to track when a node is accessible to the user, but am having trouble finding the means of tracking when a node is *removed* from the scene. The holdout is when the node is removed from undo, such as creating and then immediately undoing the creation.

 

- MNodeMessage::addNodePreRemovalCallback This covers all removal I need, except removal via undo

- MDGMessage::addNodeAddedCallback This covers all addition I need, incl. via undo

 

 

 

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

cmds.loadPlugin("destroyMePlease")

mod = om.MDagModifier()
parent = mod.createNode("transform")
rigid = mod.createNode("destroyMePlease", parent=parent)
mod.doIt()

mod.undoIt()
# Callback not called
# ~destructor not called

cmds.file(new=True, force=True)
# still not called

cmds.unloadPlugin("destroyMePlease")
# RuntimeError: Plug-in, "destroyMePlease", cannot be unloaded because it is still in use # 

del mod
# ~destructor called
# But not callback

cmds.unloadPlugin("destroyMePlease")
# Success!

 

 

 

I've got a reproducible here for the finer details.

 

- https://gist.github.com/mottosso/939ecaa992a3c98df5988484bbfb5e02

 

The secondary issue is not being able to unload a plug-in until the destructor is called, which won't happen until the MDagModifier is destroyed from Python. Any workaround for that would also be great :S Python should *not* be allowed to keep nodes around, references should be weak.

 

Applies to Maya 2018-2020.2. Any ideas?

0 Likes
Accepted solutions (1)
985 Views
2 Replies
Replies (2)
Message 2 of 3

zewt
Collaborator
Collaborator
Accepted solution

I do this with MDGMessage::addNodeAddedCallback and MDGMessage::addNodeRemovedCallback, and it tracks through undo correctly.  For example:

 

https://github.com/zewt/zMayaTools/blob/master/scripts/zMayaTools/mouth_keying.py#L219

 

0 Likes
Message 3 of 3

negow
Advocate
Advocate

Oh my, how could I have missed that! I found the "added" equivalent which has been working reliably so far. Thanks, this looks to be it.

0 Likes