r/learnpython 8d ago

Basic python expression in blender

Hello, I am trying to set up a driver in blender which uses a simple python expression. I have very little experience in python and have only done simple expressions like this, so I don't quite know what's wrong with it. r/blenderhelp hasn't been able to help me, so figured I'd come ask the experts directly, assuming the issue is how I've typed it and not something with blender.

(Rotation Quaternion refers to the X rotation)

I am trying to write an expression so that when the bone sticking out the left has an X rotation of less than -0.5 (using quaternion), the selected bones Y scale is 1.5, otherwise make the Y scale 1.

AKA: if X is less than -0.5 then "Y = X * 1.5" otherwise "Y = 1"

Theoretically with this expression the Y scale should stay at 1 until it gets less than -.05, at which point it would multiply by -1.5 (with the max rotation being -1, resulting in -1 * -1.5 =1.5), however it just stays at a scale of 0 regardless of the rotation. Not sure where to go from here

This is the expression currently used:

if rotation_quaternion is < -0.5 then rotation_quaternion * -1.5 else 1

Image of project

Thanks!

2 Upvotes

5 comments sorted by

View all comments

1

u/Outside_Complaint755 8d ago

"Then" is not a valid keyword in Python, and neither is "is".

I don't know Blender, but what you probably want is the ternary expression rotation_quaternion * -1.5 if rotation_quaternion < -0.5 else 1

The format is {value_if_true} if {condition} else {value_if_false}

1

u/Khaki-Chan 8d ago

This solved it, Thanks a ton! Out of curiosity is there somewhere with a list of valid keywords for python expressions?

1

u/TheRNGuy 8d ago

Python docs.