Reputation:
I have this code in python:
"{0}.currentTime += 1;".format(hairSyst)
where hairSyst
is a string defined earlier. I don't understand why I am getting a syntax error. My aim is to set an expression inside maya, and the expression is a bit long, I paste the whole thing below, maybe you can suggest a better way to do it.
expr = ("if ({0}.autoOverlap == 1){".format(firstControl.getName())
"{0}.currentTime += 1;".format(hairSyst)
"{0}.currentTime += 1;".format(nucleus)
"float $refresh_tx = {0}.translateX;".format(cube)
"float $refresh_ty = {0}.translateY;".format(cube)
"float $refresh_tz = {0}.translateZ;".format(cube)
"float $refresh_rx = {0}.rotateX;".format(cube)
"float $refresh_ry = {0}.rotateY;".format(cube)
"float $refresh_rz = {0}.rotateZ;".format(cube)
"}else if({0}.autoOverlap == 0){".format(firstControl.getName())
"{0}.currentTime = 1;".format(hairSyst)
"{0}.currentTime = 1;".format(nucleus)
"}"
)
Upvotes: 0
Views: 976
Reputation: 10951
Better make it one format
ing string:
expr = """if ({0}.autoOverlap == 1){{
{1}.currentTime += 1;
{2}.currentTime += 1;
float $refresh_tx = {3}.translateX;
float $refresh_ty = {3}.translateY;
float $refresh_tz = {3}.translateZ;
float $refresh_rx = {3}.rotateX;
float $refresh_ry = {3}.rotateY;
float $refresh_rz = {3}.rotateZ;
}}else if({0}.autoOverlap == 0){{"
{1}.currentTime = 1;
{2}.currentTime = 1;
}}""".format(firstControl.getName(), hairSyst, nucleus, cube)
Notice the use of """
triple quoting instead of "
single quoting to format a multi-line string.
EDIT:
In case the original string contains { }
, we have to escape them by {{ }}
as per documentation:
6.1.3. Format String Syntax
The str.format() method and the Formatter class share the same syntax for format strings (although in the case of Formatter, subclasses can define their own format string syntax).
Format strings contain “replacement fields” surrounded by curly braces {}. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }}.
Upvotes: 1
Reputation: 46
You have to concatenate those strings. Right now, eval()
sees that you've passed in 13 strings, but eval()
only takes a single string. So do something like this:
expr = ("if ({0}.autoOverlap == 1){ ".format(firstControl.getName()) +
"{0}.currentTime += 1; ".format(hairSyst) +
"{0}.currentTime += 1; ".format(nucleus) +
"float $refresh_tx = {0}.translateX; ".format(cube) +
"float $refresh_ty = {0}.translateY; ".format(cube) +
"float $refresh_tz = {0}.translateZ; ".format(cube) +
"float $refresh_rx = {0}.rotateX; ".format(cube) +
"float $refresh_ry = {0}.rotateY; ".format(cube) +
"float $refresh_rz = {0}.rotateZ; ".format(cube) +
"}else if({0}.autoOverlap == 0){ ".format(firstControl.getName()) +
"{0}.currentTime = 1; ".format(hairSyst) +
"{0}.currentTime = 1; ".format(nucleus) +
"}"
)
I additionally added spaces after each string to make sure they don't run into each other.
Upvotes: 0