Re: [Maya-Python] [Python API 1.0] - Proper flow in code

86 views
Skip to first unread message

Justin Israel

unread,
Feb 24, 2017, 5:02:33 AM2/24/17
to Python Programming for Autodesk Maya


On Thu, Feb 23, 2017, 9:04 PM justin hidair <justin...@gmail.com> wrote:
Quick questions here :

How do you properly destroy your objects and MScriptutils.. ?

I assume this is a C++ question, since Python is garbage collected?

"When an MScriptUtil object is destroyed any pointers to its data immediately become invalid."

So any data being managed by MScriptUtil should get cleaned up when the dtor is called. Is that what you are after?

Any optimization tricks ?

What specifically are you after?


Finally how many of you guys use Python API 2.O ?

--
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/08bf227c-8566-4016-8520-625a04a0f46e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paul Molodowitch

unread,
Feb 24, 2017, 6:13:37 PM2/24/17
to Python Programming for Autodesk Maya
He's asking about python - I don't know if C++ even has MScriptUtil, since it's a class created purely for python wrapping issues.

He's asking about destroying objects because, unfortunately, MScriptUtil is evil, and creates "ptr" objects which refer to storage contained within the MScriptUtil itself... but NOT in any sort of way that python is aware of.  The net result is that it's possible for python to destroy the MScriptUtil, and it's associated C++ objects... but still have a ptr object created by that MScriptUtil, which tries to use that now-freed space.  

As an example, code like this can cause a crash:

def getPtr():
    return MScriptUtil().asDoublePtr()

ptr = getPtr()
maya.OpenMaya.someFuncThatNeedsADoublePtr(ptr)

The problem is that the MScriptUtil object created within the function will get deleted as soon as the function exits... but the returned pointer object still points at the storage associated with that (now freed) MScriptUtil.  This is probably one of the many reasons why it was removed completely in the python 2.0 api (yay!)

However, because we still need to use the 1.0 api for many things... pymel contains a small wrapper class for dealing with MScriptUtil pointers.  Basically, it just holds a (python) ref to the MScriptUtil object along with the pointer, to ensure that it isn't destroyed prematurely.  Full details are in the docs / comments in pymel.api.allapi.SafeApiPtr, but here's some example usage:

ptr = pymel.api.allapi.SafeApiPtr('double')
maya.OpenMaya.someFuncThatNeedsADoublePtr(ptr())

Note that you need to manually get the pointer object by "calling" it - the general idea is that you should hold onto the SafeApiPtr object in python, and get the "raw" MScriptUtil pointer only immediately before passing to the OpenMaya call.  However, the SafeApiPtr still can't guarantee that you won't get in trouble - for instance, you can't do this:

maya.OpenMaya.someFuncThatNeedsADoublePtr(SafeApiPtr('double')())

...as the SafeApiPtr, and thus the MScriptUtil, may be destroyed by the time the OpenMaya function is called. Essentially, you need to make sure the SafeApiPtr object is kept around until the OpenMaya C++ functions are "done with it".

- Paul

On Fri, Feb 24, 2017 at 2:02 AM Justin Israel <justin...@gmail.com> wrote:


On Thu, Feb 23, 2017, 9:04 PM justin hidair <justin...@gmail.com> wrote:
Quick questions here :

How do you properly destroy your objects and MScriptutils.. ?

I assume this is a C++ question, since Python is garbage collected?

"When an MScriptUtil object is destroyed any pointers to its data immediately become invalid."

So any data being managed by MScriptUtil should get cleaned up when the dtor is called. Is that what you are after?

Any optimization tricks ?

What specifically are you after?


Finally how many of you guys use Python API 2.O ?

--
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.

--
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/CAPGFgA2p3%2ByouTDi-d83XMubKfce_UY_B5-8V4rMtYQbFxAvdw%40mail.gmail.com.

Justin Israel

unread,
Feb 24, 2017, 6:19:37 PM2/24/17
to python_in...@googlegroups.com
On Sat, Feb 25, 2017 at 12:13 PM Paul Molodowitch <elro...@gmail.com> wrote:
 The net result is that it's possible for python to destroy the MScriptUtil, and it's associated C++ objects... but still have a ptr object created by that MScriptUtil, which tries to use that now-freed space.  

In all fairness though, the documentation does say the pointers are only valid for the lifetime of the MScriptUtil instance :-)
 
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@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_m...@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_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAAssL7aGz%2B8RE5Cu9DpkaticQOvq1OR2s1yB5kmQ_%3DeLCAK%2B0g%40mail.gmail.com.

Paul Molodowitch

unread,
Feb 24, 2017, 6:40:28 PM2/24/17
to python_in...@googlegroups.com
On Fri, Feb 24, 2017 at 3:19 PM Justin Israel <justin...@gmail.com> wrote:
On Sat, Feb 25, 2017 at 12:13 PM Paul Molodowitch <elro...@gmail.com> wrote:
 The net result is that it's possible for python to destroy the MScriptUtil, and it's associated C++ objects... but still have a ptr object created by that MScriptUtil, which tries to use that now-freed space.  

In all fairness though, the documentation does say the pointers are only valid for the lifetime of the MScriptUtil instance :-)

They do now, true. They didn't always... at one point they did an overhaul of the MScriptUtil docs that made things MUCH clearer.  (Though the underlying design is still awkward.  Don't even get me started on the whole "initial values" vs "working values" thing - had to figure out that bit of oddball logic before there was any documentation on it's behavior!) The better change was when it was removed entirely in the python 2.0 api.  Kudos to Autodesk for realizing it was a problem, and trying to fix it, both with the doc update, and the 2.0 overhaul.

Speaking of 2.0 - does anyone know if it's feature complete? I thought it was dead, but then they added a bunch more classes an update or two ago, so maybe there's still hope of it getting there.  Would be nice to never have to use the python 1.0 api...

- Paul

justin hidair

unread,
Feb 25, 2017, 2:42:33 AM2/25/17
to Python Programming for Autodesk Maya
Thank you all, deleted it in the first place, because I though it was not that "interesting" here :D

justin hidair

unread,
Feb 25, 2017, 4:52:04 AM2/25/17
to Python Programming for Autodesk Maya


They do now, true. They didn't always... at one point they did an overhaul of the MScriptUtil docs that made things MUCH clearer.  (Though the underlying design is still awkward.  Don't even get me started on the whole "initial values" vs "working values" thing - had to figure out that bit of oddball logic before there was any documentation on it's behavior!) The better change was when it was removed entirely in the python 2.0 api.  Kudos to Autodesk for realizing it was a problem, and trying to fix it, both with the doc update, and the 2.0 overhaul.

Speaking of 2.0 - does anyone know if it's feature complete? I thought it was dead, but then they added a bunch more classes an update or two ago, so maybe there's still hope of it getting there.  Would be nice to never have to use the python 1.0 api...

- Paul

It's still not complete to my knowledge, talking about modules, but if you work on meshes , modeling things, it has it certainly
Reply all
Reply to author
Forward
0 new messages