If statement for snapping?

Hi All

Im very much a python noob, but im looking to learn.
Im trying to create some simple scripts to shorten some tasks in blender.
in this case I am trying to create a toggle for “Align Rotation to Target” in the snapping menu.
From my google-ing this seems correct but it returns an invalid syntax error so im clearly missing something…
can anyone help?

import bpy

if bpy.context.scene.tool_settings.use_snap_align_rotation = True:
    bpy.context.scene.tool_settings.use_snap_align_rotation = False
    
else:
    bpy.context.scene.tool_settings.use_snap_align_rotation = True

Hmmm. Can you post the error. What kind of invalid syntax error

Hey here is the screenshot, it says check the system console but nothing outputs there.

You need a add-on that allows errors to show in the console. Try this console_run_script.py (2.7 KB)

Hi,
== after " if ":
if bpy.context.scene.tool_settings.use_snap_align_rotation == True:

2 Likes

@killbourne If you are using windows then go to top left corner windows>toggle system console. This will open up terminal / console in which you can see error message. And answer by @Valentin_Min is correct. You need == for comparison, single = is assignment operator.

2 Likes

Fantastic! thank you all for the help :slight_smile:
got the console as well as the script to work beautifully.
now to carry on the journey of learning python in blender…

3 Likes

You may want to simplify the syntax a bit with something like:

tools = bpy.context.scene.tool_settings
if tools.use_snap_align_rotation == True:
    tools.use_snap_align_rotation = False
else:
    tools.use_snap_align_rotation = True

before the if instruction. The variable (in this case a reference) is created automatically, you don’t need to declare it like in other languages. Be careful that some stuff are objects while others are direct values (immutable). Look for them in the Blender’s documentation.

@Blutag thanx! will definitely use this.

if I may recommend an alternative:

tools.use_snap_align_rotation = not tools.use_snap_align_rotation

ditch the if altogether.

2 Likes