You know, I take back (or at least amend) what I said before about the
MScriptUtil doc - the new 2011 documentation is actually pretty good - it
explains how MScriptUtil actually has TWO values associated with it - what
it calls the "initial" and "working" values:

http://download.autodesk.com/us/maya/2011help/API/class_m_script_util.html

Thanks to whoever rewrote those!

Anyway, the fact that there are these two values helps explains why the
examples Brandon gave don't work:


> > > > > grab some components on a nurb surface and run this.
>> >
>> > > > > import maya.OpenMaya as openMaya
>> > > > > sel=openMaya.MSelectionList()
>> > > > > openMaya.MGlobal().getActiveSelectionList(sel)
>> > > > > path=openMaya.MDagPath()
>> > > > > obj=openMaya.MObject()
>> > > > > sel.getDagPath(0, path, obj)
>> > > > > compItr = openMaya.MItSurfaceCV(path, obj)
>> > > > > while not compItr.isDone():
>> > > > >    u = openMaya.MScriptUtil()
>> > > > >     v = openMaya.MScriptUtil()
>> > > > >     compItr.getIndex(u.asIntPtr(), v.asIntPtr())
>> > > > >     print openMaya.MScriptUtil().getInt(u.asIntPtr())
>> > > > >    print openMaya.MScriptUtil().getInt(v.asIntPtr())
>> > > > >    compItr.next()
>> >
>> > > > > Now the issue I have is that it doesn't print the correct values.
>> >
>>
>
This doesn't work because u/v aren't initialized - as the 2011 docs point
out,


> If no initial values are provided in the 
> MScriptUtil<class_m_script_util.html> constructor
> and none are subsequently supplied by calls to the create*() methods, then
> the pointers returned by the as*Ptr() methods won't have room to store even
> a single value, which will likely lead to errors.



As for the second example:

> > > > import maya.OpenMaya as openMaya
>> > > > > sel=openMaya.MSelectionList()
>> > > > > openMaya.MGlobal().getActiveSelectionList(sel)
>> > > > > path=openMaya.MDagPath()
>> > > > > obj=openMaya.MObject()
>> > > > > sel.getDagPath(0, path, obj)
>> > > > > compItr = openMaya.MItSurfaceCV(path, obj)
>> > > > > while not compItr.isDone():
>> > > > >    u = openMaya.MScriptUtil()
>> > > > >     v = openMaya.MScriptUtil()
>> > > > >    u.createFromInt(50)
>> > > > >    v.createFromInt(50)
>> > > > >     compItr.getIndex(u.asIntPtr(), v.asIntPtr())
>> > > > >     print openMaya.MScriptUtil().getInt(u.asIntPtr())
>> > > > >    print openMaya.MScriptUtil().getInt(v.asIntPtr())
>> > > > >    compItr.next()
>> >
>> > > > > This will just print 50. So how am I plugging this into
>> > > > > compItr.getIndex wrong?
>>
>
 This fails because every call to asIntPtr overwrites the working value with
the initial value - so when you do

> > > >     print openMaya.MScriptUtil().getInt(u.asIntPtr())

... the second call to asIntPtr() overwrites the working value that was set
by  getIndex() with the old, initial value - 50.  This is why you always
need to store the value returned by asIntPtr, instead of regenerating it
every time.

And yes, MScriptUtil has always worked this way - before, though, we just
had to figure out the way it was really working through trial and error...

Oh, and one last note - the getInt, getFloat, etc methods are all what C++ /
the api docs call "static" member functions - which in python terms, means
class methods.  So while something like:

openMaya.MScriptUtil().getInt(myIntPtr)

will work, it's needlessly creating an instance, and can be somewhat
misleading - the 'proper' way to do this would be:

openMaya.MScriptUtil.getInt(myIntPtr)

- Paul

-- 
http://groups.google.com/group/python_inside_maya

Reply via email to