Hi guys,
Thanks for your responses, they both really helped. I actually went
out and got a Python book after reading your replies. I definitely
understand a little more of how the string formatting is working. But
this problem was so basic, I feel like I have a mountain to climb :P
I wasn't planing on setting an attribute straight to the node, I was
going to hold it in the script until the expression is written.
def findRadius():
#get wheel name
wheelname=
pm.ls( selection=True )
#rename wheel to something logical
pm.rename(wheelname,"fronttire")
#get radius through BB information
bbox = pm.exactWorldBoundingBox("fronttire")
oradius=bbox[4]/2
return oradius
radius=findRadius()
pm.expression( o='fronttire', s= 'fronttire.translateX =
sin(fronttire.translateX) * %s' % radius )
Do you think it would be better to add the attribute to the node?
This script is a part of a vehicle rigging scripts that I'm throwing
together. Thanks again and I hope to be on these forums long enough to
help others once I graduate to your level. Also, I know the code
snippet is quite small but any critiques or recommendations on how to
make it look better, more pro or efficient, I'm all ears.
On Apr 8, 2:03 am, Leonid Onokhov <
sop...@gmail.com> wrote:
> pm.expression is just a wrapper for maya `expression` command.
>
> Here is maya doc:
>
> This command describes an expression that belongs to the current scene. The
> expression is a block of code of unlimited length with a C-like syntax that
> can perform conversions, mathematical operations, and logical decision
> making on any numeric attribute(s) in the scene. One expression can read and
> alter any number of numeric attributes. Theoretically, every expression in a
> scene can be combined into one long expression, but it is recommended that
> they are separated for ease of use and editing, as well as efficiency.
>
> So, that -s flag passes 'string' expression which is not python and somewhat
> not mel.
>
> You need to make whole valid expression string and pass it to command.
> Like that (if radius is float)
>
> >>>radius = 9 # this one is python interpreter variable, it has nothing to
> do with expression
> >>> myexpr = 'fronttire.translateX=sin(fronttire.translateX)* %f' % radius
> >>> print myexpr
>
> fronttire.translateX=sin(fronttire.translateX)* 9.0000;
>
> >>>pm.expression(o='fronttire', s=myexpr)
>
> String formatting operator '%' will replace %f token in string with radius
> value. Python printf-like string formatting is described herehttp://
docs.python.org/release/2.6.6/library/stdtypes.html#string-for....
>
> String myexpr containing expression is passed to pm.expression command which
> will then create expression node - just like maya expression command.
> After that changing radius variable will not affect your expression.
>
> Now, if you want to be able to dynamically change radius, you should add
> attribute controling the radius to any node you like, and make expression
> like 'fronttire.translateX=sin(fronttire.translateX)*
> myControlNode.tireRadius'.
>
> Using python and pymel is same as using mel, but with proper language
> instead of ugly shell scripting.
> I recommend you reading python tutorialhttp://
docs.python.org/release/2.6.6/tutorial/index.html, it really helps.