0

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)
        "}"
)
Iron Fist
  • 10,739
  • 2
  • 18
  • 34

2 Answers2

1

Better make it one formating 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 }}.

Iron Fist
  • 10,739
  • 2
  • 18
  • 34
  • I already tried this, but I am getting: hairSyst, nucleus) # KeyError: '\n {1}' –  Mar 23 '17 at 19:49
  • @user3809642, I've doubled the `{` since we are using `{}` operator to format the string, we have to escape `{` by `{{` – Iron Fist Mar 23 '17 at 19:51
0

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.

halp_me
  • 46
  • 7
  • thanks, now I am getting this error though: # "{0}.currentTime = 1; ".format(nucleus) + # ValueError: unmatched '{' in format # –  Mar 23 '17 at 19:45