View Full Version : Python nuances that confuse me
ArrantSquid 08-23-2010, 04:53 PM So I'm just getting into python coming from mel (and a bevvy of other web languages), but certain things about the way python functions within Maya confuses me.
For instance:
p, li { white-space: pre-wrap; } cmds.createNode('nurbsCurve', name="areallysweetname")
doesn't actually create the curve with the name. It creates a shapeNode underneath the curve that has that name. Why doesn't it assign it to the actual literal name?
I also had an issue with locators. Here's what I mean:
cmds.spaceLocator(name="this", p=(1, 2, 3))
instead of it creating it with the position at 1, 2, 3 it sets the worldspace coordinates instead of the local coordinates, which leaves the local coordinates at 0, 0, 0
Am I just doing something wrong or is this just the way that things work? Thanks for the help.
|
|
kojala
08-23-2010, 07:17 PM
well,
whit this command its easier to create a curve:
cmds.curve(p=[(0, 0, 0), (0, 1, 0)], n='myCurve', k=[0, 1, 2, 3])
And the locator thing is prolly just maya feature. Same thing happens with mel.
you just have to set the translation after the locator is created.
ArrantSquid
08-23-2010, 09:52 PM
AH! I see! Thanks for filling in those gaps for me kojala. I appreciate it. :)
ThE_JacO
08-24-2010, 02:51 AM
On a remotely related note, get in the habit early of preventing automated typecasting internally, or even worse, hard to track, type related bugs.
When something is a float, use a float. 0 should be 0.0, 1 should be 1.0 and so on.
Using int notation, in a dynamically typed language, means you rely on the good will of whoever wrote the API or the bindings to cast or convert for you.
What is worse is that something like 1/3 might slip through when you want to quickly punch in 0.333, and you don't notice that working on ints and before the type conversion that will get you a 0.
ArrantSquid
08-25-2010, 06:23 AM
Wow, I hadn't even thought about the fact that I may be passing an int and a float at the same time. :/ Thank you for pointing that out though because now I'm going to go through my current script and make sure that I'm passing everything the same values. :) I'm still trying to get the hang of Python, so it's been a bit rough thus far, but I'm learning a lot and am cutting down on my code by about 60% (vs Mel)! :) Thanks again!
ThE_JacO
08-25-2010, 08:47 AM
When you come from a statically typed language it's pretty easy to forget that dynamic typing is pretty convenient, but can also dig you rather deep debugging hole :)
In general the only things you'll have to watch your step around are numbers or custom objects, particularly if they are subclassing some of the base objects.
Dealing with a vanilla maya without using any particular modules (so no sciPy, numPy, lxml or anything like that) it will most likely just boil down to distinguishing between ints and floats, and paying attention to the difference in manipulating lists vs other non mutable in place arrays.
ArrantSquid
08-27-2010, 04:27 AM
Thanks for the extra information! Right now I am doing just basic vanilla stuff, but am going to start exploring pyQT in a few weeks after I feel more comfortable in Maya, so this is all definitely great stuff to know. Plus since everything you do in scripting/programming is based around numbers, knowing to cast your types as they should be is definitely useful, because I may inadvertently pass a float to a int just because I'm not having to define my object as "int a" or whatever it might be. This whole process has been fun, but overwhelming and I've decided to write my own autorigger as my first python project because the setup I have now is good for what I do, so I figured I may as well simplify the process. :) Thanks again for the help! I probably wouldn't have thought of any of this stuff until it came down to something going wrong, so it's nice to be able to head it off. :)
CGTalk Moderation
08-27-2010, 04:27 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.