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. On Wed, Jan 3, 2018 at 9:41 AM Paul Molodowitch <[email protected]> wrote: > 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. > > On Wed, Jan 3, 2018 at 1:43 AM Marcus Ottosson <[email protected]> > wrote: > >> 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 modfn.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() >> <http://help.autodesk.com/view/MAYAUL/2017/ENU/?guid=__py_ref_class_open_maya_1_1_m_object_html> >> at first, but that seems to always return False. >> >> Any ideas? >> >> Best, >> Marcus >> >> >> -- >> 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 [email protected]. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOBw42L51ULyNjOCmKGMOJTcnw44w-r5VjPMocLbpj%2BhfA%40mail.gmail.com >> <https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOBw42L51ULyNjOCmKGMOJTcnw44w-r5VjPMocLbpj%2BhfA%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAAssL7biQxBDUWBa3nGvGGBZ7ciWz73xg4fPSJkbnJ5oYnZP2w%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
