I’ve ran into two issues with the ctypes approach so far.

   1. long is deprecated in Python 3; but can be worked around
   2. Garbage collection

I’ve put together a reproducible of the issue; TLDR; it (sometimes) crashes
Maya.

import _ctypesfrom maya.api import OpenMaya as om

ids = dict()
def createBox():
    mod = om.MDagModifier()
    mod.createNode("transform")
    mod.doIt()

    def undo():
        mod.undoIt()

    ids["undo"] = hex(id(undo))

createBox()
# At this point, `undo` has been garbage collected
undo = _ctypes.PyObj_FromPtr(long(ids["undo"], 0))
# WARNING: This *may* crash Maya
undo()

What I suspect happens is that _ctypes happily returns *something* that you
might expect to be the local undo(), and lets you call it. Presumably
because it is forcibly returned as a callable, regardless of what is
actually at that memory location. In reality it probably couldn’t return
that function, because it no longer exists due to garbage collection.

Any ideas?
​

On 25 March 2018 at 17:26, Marcus Ottosson <konstrukt...@gmail.com> wrote:

> if invoked by itself
>
> Good point. It’d need to be prevented from being called explicitly
> somehow, and only be used via this “commit” function.
>
> Will experiment with these approaches. Thanks!
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAFRtmODqXA2QAKdnf58ndFwbZ1op61U%3DFRBPXC24ZMuD5d6R3w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to