"Why do my eyes hurt?" "Because you've never used them before"
I'm feeling a little THAT right now. Thank you a 1000 times over. On Sun, Feb 15, 2009 at 6:19 PM, Dean Edmonds <[email protected]>wrote: > > On Sun, Feb 15, 2009 at 14:16, jasonosipa <[email protected]> wrote: > > > > What do const, &, and * each mean? > > > > When you look at documentation for an object, each argument can have a > > lot of different markup, and I'm not confident with guesses at what > > those mean, so I'll just ask: > > > > What do const, &, and * each mean? What does "blank" mean, where > > there is no const at the head of the line, and no * or & ? How about > > the stuff after the = in the argument? Is that just a "recommended > > default"? Does const only show up for things that are MObjects? > > As Tim explained, '&' and '*' are slightly different ways of passing > the value itself. If neither of those is present than what gets passed > is a copy of the value. > > In terms of translating all that markup into Pythonic terms, you first > have to differentiate between simple types, like integers and floats, > and object types. > > In Python, if I pass an object to a function the function can directly > manipulate it and the caller will be able to see the results of that > manipulation. For example: > > class myObj: > someValue = 12 > > def changeObj(theObj): > theObj.someValue = 15 > > testObj = myObj() > > print testObj.someValue > 12 > > changeObj(testObj) > print testObj.someValue > 15 > > The same is *not* true when passing a simple value to a function: > > def changeValue(theValue): > theValue = 15 > > someValue = 12 > print someValue > 12 > > changeValue(someValue) > print someValue > 12 > > In C++ there is no such differentiation between objects and simple > values. If a parameter is passed by pointer ('*') or reference ('&') > then the function can modify it such that the caller will see the > changes. If neither of those markers is present then the function is > passed a copy of the value and any changes that the function makes to > that copy will not be visible to the caller. > > In addition, if a parameter is tagged as 'const' that means that the > method is promising not modify it, even it has been passed by pointer > or reference. > > So, if you are calling an API method from Python and one of its > parameters takes an object type, such as MDagPath, the following > markups mean that the caller's copy of the object will not be changed > by the method: > > MDagPath > const MDagPath* > const MDagPath& > > while the following mean that the caller's copy of the object can be > changed by the method: > > MDagPath* > MDagPath& > > In terms of the syntax you use in Python, you always just pass the > object without doing anything special. E.g: > > somePath = om.MDagPath() > om.MDagPath.getAPathTo(someNode, somePath) > > If the markup indicates that the method can change the object that you > pass to it and you don't want it to change your copy of the object, > then you would need to pass the method a separate copy of the object:. > In the example below 'tempPath' will be modified by the call to > getAPathTo() but 'pathToKeep' will remain unchanged: > > tempPath = om.MDagPath(pathToKeep) > om.MDagPath.getAPathTo(someNode, tempPath) > > > For simple types we run into a bit of a problem because Python does > not provide any way for a function to modify a simple type which is > passed to it such that the caller sees the change. For example, > MFnMesh has a getUV() method which is declared as follows: > > MStatus getUV (int uvId, float &u, float &v, const MString > *uvSet=NULL) const; > > The intent is that you pass the id of the UV you want in the 'uvId' > parameter and get back its U and V coordinates in the 'u' and 'v' > parameters. Unfortunately there is no direct way to do that in Python, > so the Maya API provides the MScriptUtil class to get around that. > E.g: > > uptr = om.MScriptUtil().asFloatPtr() > vptr = om.MScriptUtil().asFloatPtr() > > meshFn.getUV(13, uptr, vptr) > > u = om.MScriptUtil.getFloat(uptr) > v = om.MScriptUtil.getFloat(vptr) > > > Finally, if 'const' appears at the end of the method declaration that > means that the method promises not to change any of the object's > member variables. For example, MFnMesh's numVertices() method is > declared as follows: > > int numVertices (MStatus *ReturnStatus=NULL) const > > This means that if you call numVertices() you can be assured that the > MFnMesh object will not be changed by call. > > -- > -deane > > > > -- Jason Osipa, Manager Osipa Entertainment, LLC www.osipaentertainment.com 415.374.9651 --~--~---------~--~----~------------~-------~--~----~ Yours, Maya-Python Club Team. -~----------~----~----~----~------~----~------~--~---
