Invalid Syntax Error in Maya 2022

Invalid Syntax Error in Maya 2022

3d160emiliano
Observer Observer
758 Views
1 Reply
Message 1 of 2

Invalid Syntax Error in Maya 2022

3d160emiliano
Observer
Observer

Hello everyone! 

I am trying to use a script to automate the turn-on of the Visibility on Redshift for all selected objects. But every time I run it it says "Syntax Error".

 

import maya.cmds as cmds

def set_visibility_overrides():
# Get the selected geometries
selected_geometries = cmds.ls(selection=True, dag=True, shapes=True)

for geometry in selected_geometries:
# Check if the geometry has the attribute "rsEnableVisibilityOverrides"
if cmds.attributeQuery("rsEnableVisibilityOverrides", node=geometry, exists=True):
# Set the value of "rsEnableVisibilityOverrides" to 1
cmds.setAttr(geometry + ".rsEnableVisibilityOverrides", 1)
print("Set rsEnableVisibilityOverrides to 1 for:", geometry)
else:
print("Attribute 'rsEnableVisibilityOverrides' not found for", geometry)

# Call the function
set_visibility_overrides()

 

 

Can anyone spot the error? Or is it just a thing of my Maya preferences? 

 

Thank you in advance! 

759 Views
1 Reply
Reply (1)
Message 2 of 2

Kahylan
Advisor
Advisor

Hi!

 

Here is the code with the right indentation levels, if someone else wants to reproduce the issue:

import maya.cmds as cmds

def set_visibility_overrides():
    # Get the selected geometries
    selected_geometries = cmds.ls(selection=True, dag=True, shapes=True)
    
    for geometry in selected_geometries:
        # Check if the geometry has the attribute "rsEnableVisibilityOverrides"
        if cmds.attributeQuery("rsEnableVisibilityOverrides", node=geometry, exists=True):
            # Set the value of "rsEnableVisibilityOverrides" to 1
            cmds.setAttr(geometry + ".rsEnableVisibilityOverrides", 1)
            print("Set rsEnableVisibilityOverrides to 1 for:", geometry)
        else:
            print("Attribute 'rsEnableVisibilityOverrides' not found for", geometry)
        
# Call the function
set_visibility_overrides()

 

I can't say for sure, because you didn't post the full error message.

But my guess is, that you are not running it from a correct place in Maya. This is a Python script, so it needs to be run from a place that Maya looks at as being Python.
1) Python Tab in the Script editor
2) A shelfbutton that is set to be read as Python

3)A shortcut that is set to be read as Python
You probably accidentally ran it from a MEL Tab or from a shelfbutton/shortcut that is set to be read as MEL. Which would result in the Following error message:

// Error: def set_visibility_overrides(): // 
// Error: Line 3.30: Syntax error //

 

If you have a Python synthax problem, and I accidentally fixed it somehow, the error message would look like this:

# Error: invalid syntax # 

 

If you have the first error message, run it from one of the places I mentioned above and you should be fine

 

I hope it helps!

 

0 Likes