Error: invalid syntax, why? Python script for bookmarking tool that saves poses of rigged character is NOT working.

Error: invalid syntax, why? Python script for bookmarking tool that saves poses of rigged character is NOT working.

Anonymous
Not applicable
1,170 Views
1 Reply
Message 1 of 2

Error: invalid syntax, why? Python script for bookmarking tool that saves poses of rigged character is NOT working.

Anonymous
Not applicable

I'm trying to use Python in Maya (2022) to create a bookmarking tool that saves the poses of my rigged character.  Everything works -- I select object(s) to bookmark and run code, and a prompt dialog appears inquiring a name for my pose/bookmark wherein I name/save -- except later in my workflow I try to change the character's pose to one that is bookmarked via my bookmarking tool (on my shelf), the tool doesn't work and I get an error message (# Error: invalid syntax). I don't know which line is problematic in regards to syntax, because Maya doesn't specify.

 

Here's my script:

 

from maya import cmds

#This variable will store the information that our Shelf Button will hold.  We make sure it's empty so our information doesn't continue to pile everytime we use this tool
storeCmds = ""

#This variable stores our selection
selPose = cmds.ls(sl=True)
  
#Condition statement that checks the size of our selection
if len(selPose) < 1:
    #If the condition above is met, then print this warning message
    cmds.warning("Must select at least one object!")
#If the condition above is not true, then run the following commands  
else:
    for allOfIt in selPose:
        #This variable holds all keyable, readable, writeable, connectable, and unlocked channels of the item(s) selected
        keyable = cmds.listAttr(allOfIt, k=True, r=True, w=True, c=True, u=True)
        print(keyable)
        for vals in keyable:
            #This variable stores the values of all stored keyable channels
            findVal = cmds.getAttr(allOfIt + "." + vals)
            print(findVal)
            #This varaible stores the end syntax of the code stores in our Shelf Button.  It, essentially, adds a ";" at the end of each line and uses "\n" to print our data vertically
            startCode = "setAttr "
            endCode = ";\n"
            #This variable stores the command(s) that allow us to revert back to the pose(s)/bookmark(s) we save
            saveToShelf = (startCode + (allOfIt + "." + vals) + " %f" + endCode) % findVal
            #This is the first variable we created above.  It's used to store the information of "saveToShelf" so we can add it to our Shelf Button's command
            storeCmds += saveToShelf
            #This line prints the data stored in our "storeCmds" variable
            print(storeCmds)

    #This variable stores our Prompt Dialog   
    pd_body = cmds.promptDialog(t="Body Pose", m="Name of Pose?", b="Save It!")
    #Condition statement that checks if our button gets clicked.  If this condition is met, then run the following commands
    if pd_body == "Save It!":
        #This variable stores the Name we add to our Prompt Dialog
        pd_body_name = cmds.promptDialog(q=True, text=True)
        
        #This line creates our Shelf Button that uses MEL as the source type for the commands stored in "storeCmds", and adds the Shelf Button to our custom shelf named "Rigging"
        cmds.shelfButton(l=pd_body_name, annotation=pd_body_name, imageOverlayLabel=pd_body_name, image1="Pose_Body_image.png", command=storeCmds, p="Rigging", sourceType="mel")

 

0 Likes
1,171 Views
1 Reply
Reply (1)
Message 2 of 2

Kahylan
Advisor
Advisor

Hi!

 

I'm a bit confused here, because your script seems to be working fine. I could create a pose, it made an automatic button in the Rigging shelf. When I moved the objects and clicked the button, it moved them back into position.

 

I think the problem lies in the way you are calling your MEL script. Because if you get the Error Message "#Error: invalid syntax", that means you tried to call your script using Python and not MEL ( "#" comment identifier not "//").

But that is not what happend for me and I don't see why it should since your code specifies, that it is a MEL Button.

So you either somehow changed the "Language" radiobutton in the "Command" Tab of your Shelf button from "MEL" to "Python", or you are trying to automatically incorporate the Script from your button into Python code which causes problems.

 

By the way, if you want to publish this script for other people to use.

You should make it create it's own specific shelf when it creates the first Button and then have the subsequent buttons stored on that shelf (simple check if the shelf exists). For these two reasons:

 

1) since your script creates a button for each pose, you will end up with a lot of buttons, which most people don't like on their default shelfes.

2) If the person that runs the script is in a new Session and hasn't opened the "Rigging" shelf before running the script (like I did), their shelf is not loaded yet, so all the other default/custom buttons on the shelf get lost when the new button is created.

 

I know this is just a prototype and you are probably working on a better way to recall poses. But I thought I'd mention it anyway.

 

I hope this helps!

0 Likes