Passing variable from Python into PyMel expression

Passing variable from Python into PyMel expression

dukeweezo
Enthusiast Enthusiast
1,160 Views
2 Replies
Message 1 of 3

Passing variable from Python into PyMel expression

dukeweezo
Enthusiast
Enthusiast

Hi, I'd like to pass a variable from my .py script into an expression using the PyMel Expression node / function. I'm new to PyMel / Mel and the documentation (http://help.autodesk.com/cloudhelp/2016/ENU/Maya-Tech-Docs/PyMel/generated/classes/pymel.core.nodety...) is slightly hard to figure out as it doesn't seem to provide examples. I've been able to get this much to work in my .py script

 

y = 20 # unused
exp = Expression() exp.setExpression("thing1.rotateY = 20") exp.evaluate()

However, having difficulty finding the best way to get y to be used in place of the 20 in the assignment of rotateY  (I imagine there's a fairly simple / standard way to do this). Note I'm not planning on having y change in the .py script (or expecting the expression to be updated with values provided by that script. y is more or less a precalculated constant).

Thanks

 

 

0 Likes
Accepted solutions (1)
1,161 Views
2 Replies
Replies (2)
Message 2 of 3

lee.dunham
Collaborator
Collaborator
Accepted solution

You may want to look at string formatting.

 

You create an expression object and then provide a string as the expression to execute.

When you generate the string to give as the expression, format it with the parameters you want to use from any current variables.

https://realpython.com/python-string-formatting/

0 Likes
Message 3 of 3

dukeweezo
Enthusiast
Enthusiast

Hi, that works, thanks. I thought maybe there was another way (though now I can't think of why there would need to be as you could deal with anything more complicated like lists beforehand in PyMel).

0 Likes