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

Not applicable
12-13-2021
06:01 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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")