Hi Marcus,

In Python 3 casting to int should work just fine.

Yes it is possible to end up in a situation where your object has been
garbage collected and thus an ID is now pointing to invalid memory.
So storing IDs for objects allocated on the function stack definitely isn't
safe in this case, and definitely not ok to call a nested function from
outside the parent function scope, regardless!

PyObj_FromPtr does increments the reference counter and therefore should
prevent the GC from trashing your object, however, care still needs to be
taken to make sure it gets executed before GC can have a chance to do so.

So in this example, the custom command wrapper that ends up calling the
_ctypes.PyObj_FromPtr needs to be called from within the createBox function
and it should probably pass either the mod class instance itself or another
custom class with undo/redo methods it can call.
As a side note, I found that calling PyObj_FromPtr on a class methods (like
mod.undoIt), to be unreliable, but the following workaround seems to work:

mod = om.MDagModifier()
undoIt = mod.undoIt
undoItRef =_ctypes.PyObj_FromPtr(id(undoIt))

Also, this is not production proven code so of course all the usual caveats
apply :)

I wrote a quick post about this:
https://medium.com/@k_serguei/maya-python-api-2-0-and-the-undo-stack-80b84de70551

Cheers!


On Sun, Mar 25, 2018 at 3:37 PM, Matteo Di Lena <[email protected]>
wrote:

> Hey guys,
>
> thanks a lot for keeping this thread alive, it's something that I've
> always heard about in forums or videos, but no one ever got into details of
> implementing it. It's really nice of you to share your discoveries :)
>
> Cheers!
>
> Il giorno domenica 25 marzo 2018 17:26:23 UTC+1, Marcus Ottosson ha
> scritto:
>>
>> 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 [email protected].
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/python_inside_maya/ef35ba41-c0f1-402f-aaa0-
> 6633bb85e15e%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/ef35ba41-c0f1-402f-aaa0-6633bb85e15e%40googlegroups.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/CAPCVZ5NDRtwdYz%3DHusA-BSd97bJm1cTw-Adb-xhAvnW5Gr7vJw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to