[Maya-Python] Crash when using MFnDependencyNode on a deleted MObject

791 views
Skip to first unread message

Marcus Ottosson

unread,
Jan 3, 2018, 4:43:26 AM1/3/18
to python_in...@googlegroups.com

Hi all,

I’m trying to figure out convert a crash into an exception.

The crash is coming from use of a function set when the MObject it’s managing is pointing to a node I’ve just deleted. Here’s the shortest I could make of it (Maya 2018).

from maya.api import OpenMaya as om
fn = om.MFnDagNode()
mobj = fn.create("transform")
fn.name()
# u'transform3'
mod = om.MDGModifier()
mod.deleteNode(mobj);
mod.doIt();
del mod
fn.name()

Which throws this..

Stack trace:
  Foundation.dll!TmtRefCounter::add
  Foundation.dll!Tstring::operator=
  OpenMaya.dll!Autodesk::Maya::OpenMaya20180000::MString::operator=
  OpenMaya.dll!Autodesk::Maya::OpenMaya20180000::MFnDependencyNode::name
  OpenMaya.dll!MPyMFnDependencyNode_Type::addToModule
  python27.dll!PyEval_GetFuncDesc
  python27.dll!PyEval_EvalFrameEx
  python27.dll!PyEval_EvalCodeEx
  python27.dll!PyRun_FileExFlags
  python27.dll!PyRun_InteractiveOneFlags
  python27.dll!PyRun_InteractiveLoopFlags
  python27.dll!PyRun_AnyFileExFlags
  python27.dll!Py_Main
  KERNEL32.DLL!BaseThreadInitThunk
  ntdll.dll!RtlUserThreadStart

Result: untitled
Fatal Error. Attempting to save in C:/Users/marcus/AppData/Local/Temp/marcus.20180103.0935.ma

The fact that it crashes isn’t a problem, it makes sense. But what I’d like to try and do is instead of having it crash raise an exception.

I went for MObject.isNull() at first, but that seems to always return False.

Any ideas?

Best,
Marcus

Paul Molodowitch

unread,
Jan 3, 2018, 12:41:24 PM1/3/18
to python_in...@googlegroups.com
Try using MObjectHandle.  Unfortunately, I believe (though haven't confirmed) that this means you will have to get an MObjectHandle for your MObject at some point before it is deleted... but this is probably a good idea, anyway, as MObjectHandles are intended to be used for any situation in which you need to hold onto objects "longer term" and / or they might be deleted.

Once you have an MObjectHandle, you can try using it's isValid() or isAlive() methods. Hopefully those will give you what you need. 

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOBw42L51ULyNjOCmKGMOJTcnw44w-r5VjPMocLbpj%2BhfA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Marcus Ottosson

unread,
Jan 3, 2018, 12:50:55 PM1/3/18
to python_in...@googlegroups.com
Thanks a bunch Paul, trying this next.

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAAssL7bFoewtSBQo%2B9kzNwbSo9YOKJ_ftZp5L_qzHHi0YBSX1Q%40mail.gmail.com.

Paul Molodowitch

unread,
Jan 3, 2018, 12:57:23 PM1/3/18
to python_in...@googlegroups.com
For clarification - I think you actually want "isValid()", not "isAlive()" in this scenario. The distinction, as far as I understand, is:

isValid(): is the mobject part of the maya scene
isAlive(): is the mobject c++ entity still alive / not deleted (in a c++ sense)

The example given in the documentation to help distinguish them is that if an object is deleted, but undo is on, then the mobject will still be kept around, so the object may be re-added to the scene quickly if you redo... however, it is unlinked from the scene graph.

As for the distinction between MObjectHandle.isAlive() and MObject.isNull() - my guess is that either:

a) the underlying c++-object an MObject refers to can be deleted out from under it, but the MObject does not actually get notified if this happens - that is, the MObject wraps a "dumb" c++-pointer.

b) an MObject will PREVENT the underlying c++ mobject from ever being truly deleted - ie, it's wrapping a c++-reference-counted pointer.  (Note - if this is the case, then what I said before about not being able to cast to an MObjectHandle after the MObject is deleted is wrong. And now that I think about it, I'm guessing this is more likely the way it's implemented.)

In either case, MObject.isNull() will only return true if the MObject is ever explicitly set to null - ie, you use the default constructor, or assign it equal to function that returns a null MObject to indicate failure, etc.

The MObjectHandle, on the other hand, is essentially just a weakref.

Rémi Deletrain

unread,
Jan 4, 2018, 4:51:49 AM1/4/18
to Python Programming for Autodesk Maya
Hi all,

I trying too to delete a node. But I have a crash on doIt of MDGModifier
From what I read here, the reason would come from the management of pointers...

But I don't know how to fix that or convert to exception.
My process create many nodes. When I want to delete root of one of them maya have brutal crash

Here is my code:
#   Delete node
mdg_mod = OpenMaya.MDGModifier()
for moh_node in self.a_roots:
    if moh_node.isValid() is False:
        continue
    mdg_mod.deleteNode(moh_node.object())
mdg_mod.doIt()
del mdg_mod

I Use MObjectHandle and my node is valid and alive. I have a same problem on removeAttribute of MFnDependencyNode.
I try to set undo to false. When undo is false delete node works as long as outliner is not opened. When I open outliner maya crash...

You have any solution ?

Paul Molodowitch

unread,
Jan 4, 2018, 1:25:16 PM1/4/18
to python_in...@googlegroups.com

Looks like you’ve just uncovered a bug in maya. I say this because if I comment out the “del mdg_mod” line:

import pymel.core as pm
import maya.OpenMaya as OpenMaya
handles_to_delete = []
for i in xrange(5):
    new_nodes = pm.polyCube()
    for new_node in new_nodes:
        handles_to_delete.append(new_node.__apihandle__())

old_undo_state = pm.undoInfo(q=1, state=1)
pm.undoInfo(state=0)
try:
    mdg_mod = OpenMaya.MDGModifier()
    for moh_node in handles_to_delete:

        
if moh_node.isValid() is False:
            continue
        mdg_mod.deleteNode(moh_node.object())

    mdg_mod.doIt()

#    del mdg_mod
finally:
    pm.undoInfo(state=old_undo_state)

…then it no longer crashes… however, the viewport still displays the now-deleted cube(s), even though they don’t exist in the outliner / scene / etc. Clearly a bug - the viewport is still keeping around references to deleted objects somehow. (Tested with 2018.1, don’t have a copy of 2018.2 yet.)

Not much to do other than submit a bug report to autodesk about it.

Also - if possible, please try to include independently-executable code samples when you’re looking for help troubleshooting. It makes it much more likely you’ll get help… frequently, if I see something like “self.a_roots” in a code snippet, I won’t even bother trying to troubleshoot.

  • Paul
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

vtou...@d2.com

unread,
Jan 4, 2018, 1:33:58 PM1/4/18
to Python Programming for Autodesk Maya
Not sure about what maya does internally, but as a workaround (and maybe as the only solution), you can make sure to delete all the children before deleting the parent ?

from maya.api import OpenMaya as om
# create
fn = om.MFnDagNode()
hroots = []
for i in xrange(2):
oroot = fn.create("transform")
hroot = om.MObjectHandle(oroot)
hroots.append(hroot)
fnChild = om.MFnDagNode()
for j in xrange(2):
ochild = fnChild.create("transform")
fn.addChild(ochild)

# delete
mdg_mod = om.MDGModifier()
for moh_node in hroots:
if not moh_node.isValid():
continue
fnRoot = om.MFnDagNode(moh_node.object())
# make sure the children are removed prior to deletion
for j in xrange(fnRoot.childCount()):
fnRoot.removeChildAt(0)
mdg_mod.deleteNode(moh_node.object())
mdg_mod.doIt()
del mdg_mod


It looks like as soon as maya looses the target of a pointer, it crashes, like to avoid the memory leak (yay ^^) ? But by deleting the children first, you make sure nothing is left behind \o/

Marcus Ottosson

unread,
Jan 4, 2018, 2:09:24 PM1/4/18
to python_in...@googlegroups.com
I don't think it's a bug. I originally deleted the modifier because it would otherwise maintain reference to the mobject, presumably for undo. Normally the modifier is created and destroyed in the same scope, which implicitly deletes it once exiting that scope.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/dc967374-6159-4702-ba6e-77b9a12275d6%40googlegroups.com.

Rémi Deletrain

unread,
Jan 5, 2018, 3:06:14 AM1/5/18
to Python Programming for Autodesk Maya
Thanks for all reply !!!

Elrond79:
It's not possible to show my code because I experiment a new automatic rig. It's not I don't want to show my code but I have lot of file and is not possible to post all here...
I'll try to do some code to show when maya crash but I do not promise that I will get there with all my code...

For make short:
I have a Template class. This class have a possibility to read json file for create hierarchy of node with attribute, position and other....
I have an other class for create node from template. Skeleton, other herarchy. This class can be use for transform object, cut object, etc. (Builder / Cutter)
and after I have Rig classes for create all rig possibilities.

All api argument pasted between all class is MObjectHandle. No MObject, MFnDagNode, MFnDependencyNode and other maya api classes.

I work on maya 2014. I try on maya 2018 for next prod just make shure code works too but my priority is maya 2014 for this moment.
I d'ont try with API 2.0 because all code is make with API 1.0 in my studio. May be if I have a time I transform all code in API 2.0... But is not a priority...
On maya 2014 and 2018 I have a same problem and "del mdg_mod" don't crash maya on my computer.

VTou:
I try this morning. Thanks !

Any one have a same problem on deleteAttribute of MFnDependencyNode ?

Marcus Ottosson

unread,
Jan 5, 2018, 4:08:38 AM1/5/18
to python_in...@googlegroups.com

I managed to use MObjectHandle to avoid a crash successfully.

from maya.api import OpenMaya as
 om
fn = om.MFnDagNode()
mobj = om.MObjectHandle(fn.create("transform"))
mobj.isAlive()
# True
fn.name()
# u'transform3'
mod = om.MDGModifier()
mod.deleteNode(mobj.object())
mod.doIt()
mobj.isAlive()
del mod
fn.name()
# ''
mobj.isAlive()
# False

Testing this in production next.

It’s not possible to show my code because I experiment a new automatic rig. It’s not I don’t want to show my code but I have lot of file and is not possible to post all here…
I’ll try to do some code to show when maya crash but I do not promise that I will get there with all my code…

There’s no need to post all of your code, the trick is providing a short reproduction of the problem, such that we can run it ourselves. The fact that you’re reading a JSON and are making a rig etc. is likely not related to the problem and can be left out. The code snippet I provided (for example) isn’t actually part of anything I use in production, but rather is a condensed version highlighting the problem. Solving this condensed problem solves the problem in my production code.


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Rémi Deletrain

unread,
Jan 5, 2018, 4:41:00 AM1/5/18
to Python Programming for Autodesk Maya
Thanks for your reply Marcus,

Theres no need to post all of your code, the trick is providing a short reproduction of the problem, such that we can run it ourselves. The fact that youre reading a JSON and are making a rig etc. is likely not related to the problem and can be left out. The code snippet I provided (for example) isnt actually part of anything I use in production, but rather is a condensed version highlighting the problem. Solving this condensed problem solves the problem in my production code.....

For create my template object I use many recursive function un many files. It's very dfficult to isolate code for reproduce a little exemple. I try to make this today.

I think a found a solution.
I replace MDGModifier by MDagModifier and now delete don't crash.
MDagModifier could use a MDagPath for retrieve MObject ?

Marcus Ottosson

unread,
Jan 5, 2018, 4:48:05 AM1/5/18
to python_in...@googlegroups.com

Missed to include a key highlight, the isAlive() returns True before del mod, suggesting that it isn’t deleted internally until the modifier has been garbage collected.


Rémi Deletrain

unread,
Jan 5, 2018, 5:36:26 AM1/5/18
to Python Programming for Autodesk Maya
I tryed your solution

from maya.api import OpenMaya as
om
fn
= om.MFnDagNode()
mobj
= om.MObjectHandle(fn.create(
"transform"))
mobj
.isAlive()
# True
fn.name()
# u'transform3'
mod
= om.MDGModifier()
mod
.deleteNode(mobj.object())
mod
.doIt()
mobj
.isAlive()
del mod
fn.name()
# ''
mobj
.isAlive()
# False

and I have a crash.
But MDagModifier works very well

Rémi Deletrain

unread,
Jan 8, 2018, 3:36:06 AM1/8/18
to Python Programming for Autodesk Maya
Hi all,

I work on my problem.
I try to get all MDagPath of my MObject and use deleteNode of MDGModifier and it doesn't work.
I try to use isValid or isAlive of MObjectHandle and it doesn't work too.
I try to reproduce the code so that can to try but it works out of my system.

The only solution I found is to use MDagModifier.

It's possible I have variable doesn't correctly deleted who keep maya object. I don't know why MDagModifier works I try to understand for fix my code.
But for now it's possible to step forward.

Marcus Ottosson

unread,
Jan 8, 2018, 3:46:55 AM1/8/18
to python_in...@googlegroups.com
Does my example posted about also cause a crash for you? Which platform are you on? Which version of Maya? On Windows with Maya 2018 it works as expected over here.​

Rémi Deletrain

unread,
Jan 8, 2018, 3:53:59 AM1/8/18
to Python Programming for Autodesk Maya
Hi Marcus,

Does my example posted about also cause a crash for you ?

Yes, your exemple cause a crash.
I work with Maya 2014 on Windows 7 and API 1.0
It's not possible to work with API 2.0 for now because all code of my studio works on API 1.0. May be for the next prod it's possible to past to API 2.0

Marcus Ottosson

unread,
Jan 8, 2018, 4:41:10 AM1/8/18
to python_in...@googlegroups.com
Ah, API 1.0. Yes that would be a different story. Wouldn't expect it to crash though.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Rémi Deletrain

unread,
Jan 8, 2018, 5:12:21 AM1/8/18
to Python Programming for Autodesk Maya
API 2.0 is realy more better ?

Marcus Ottosson

unread,
Jan 8, 2018, 5:57:57 AM1/8/18
to python_in...@googlegroups.com
Didn't mean that, I meant it does things differently on the C++ side, so would expect it to crash differently.

Rémi Deletrain

unread,
Jan 8, 2018, 6:11:37 AM1/8/18
to Python Programming for Autodesk Maya
Yes but if Maya have API 2.0 it's finally for override API 1.0, So API 2.0 must be better. 
I saw on the web people doen't want to use API 2.0 and other people want to use only 2.0.

On your exemple you used 2.0.
What do you prefere with this API ?

Marcus Ottosson

unread,
Jan 8, 2018, 6:13:44 AM1/8/18
to python_in...@googlegroups.com

What do you prefere with this API ?

Well, for one, I like that it doesn’t crash when I run the snippet above. :)

Rémi Deletrain

unread,
Jan 8, 2018, 6:20:26 AM1/8/18
to Python Programming for Autodesk Maya
That is a good reason :)
Reply all
Reply to author
Forward
0 new messages