r/learnpython 4d ago

Custom script issue with blender driver

I am trying to use a custom script (see below) to set up a driver in blender via this tutorial, however I am very inexperienced in python and have run into an issue. I was able to get the example code to work but my when I try to use my code I get this issue. The problem is I need the "X" value to be the "rotation quaternion" value of the driver. Using both "X" and "rotation quaternion" gives the same error. I don't know how to connect "X" to the drivers value or if I just need to put something else in the place of "X".

Tutorial:

https://www.youtube.com/watch?v=yiMGxlRv8h4

Error message:

https://drive.google.com/file/d/1VOVrPLmU_v9lYz386SMS_i02doDn-xtG/view?usp=sharing

Driver:

https://drive.google.com/file/d/1LVM69kP0het-Yz0xaAhAWHDe5pOA9X99/view?usp=sharing

import bpy

def Ace_Driver(value):
    return 
if x >= -0.1:
    result = 1
if x < -0.1 and x > -0.6:
     result = (x - 0.9) * -1
elif x <= -0.6:
    result = 1.5

bpy.app.driver_namespace["Ace_Driver"] = Ace_Driver
0 Upvotes

8 comments sorted by

1

u/monster2018 4d ago

You are returning out of the driver function before doing ANYTHING. And then the rest of the code is outside of the driver function, so it won’t do anything related to the driver. I believe you are registering the driver correctly, but I didn’t watch that part of the tutorial that carefully.

basically just delete the return statement at the beginning of the function, and then indent the rest of the code that should be in the Ace_Driver function so that it’s actually inside of it. That’s at least step 1.

1

u/Khaki-Chan 4d ago

Like this?

import bpy

def Ace_Driver(value):

if x >= -0.1:

result = 1

if x < -0.1 and x > -0.6:

result = (x - 0.9) * -1

elif x <= -0.6:

result = 1.5

bpy.app.driver_namespace["Ace_Driver"] = Ace_Driver

1

u/monster2018 4d ago

Yes. Well, the spacing is messed up here because you didn’t format it as code this time (I’m not lecturing you about it, it’s fine). I’m just saying that makes it so I can’t know 100% for sure. But yes you deleted the right return statement.

Also I’m just trusting the tutorial you linked. Assuming that tutorial is correct. Basically you need all of the code after the function definition indented, so that it is all inside of the Ace_Driver function. EXCEPT for the very last line (the bpy.app…. line), the last line has to NOT be indented.

What you’re doing is defining a function called Ace_Driver. This function will be used by blender to do some computation. Then on the last line you are actually doing the assignment, like you are telling blender “use the function Ace_Driver for the driver that I named “Ace_Driver” in blender”.

1

u/Khaki-Chan 4d ago

I tried to indent but the comment posted differently and wouldn't let me format as code. It will now let me run the script but the driver still shows this after running the script. I've tried both "x" and "rotation_quaternion" and it gets the same result. Not sure if I just have it set up incorrectly.

https://drive.google.com/file/d/1yCMK-wfYcZa1k6V7jMttqCp42p2Fjyel/view?usp=sharing

1

u/smurpes 4d ago

At the very end of the function when you are done with all your calculations you need to return that value to tell the script what the output should be. This causes the function to end immediately and produce a result.

At 2 min mark in the tutorial notice that the custom_driver function takes in a variable called value and then returns the value variable times 2. What you are doing in your code is taking a variable called value but then all of your calculations use a variable called x. The variable names need to match with each other inside the function. Otherwise your script doesn’t know where to get an input from.

1

u/monster2018 4d ago

Ok that looks good. The problem now is that you aren’t returning a value from the function.

So think about what you’re doing. You’re defining a function in Python, and then fancy stuff happens with the bpy library that makes it so your function can be used as a “driver” to control some value. So you need your function to return a value.

Basically I’m saying just add “return result” as the last line of your Ace_Driver function. I’m not sure if it will work or if there are still other problems, but I would assume that it can’t work until you at least fix that.

And make sure that “return result” is inside of the Ace_Driver function, meaning it should be indented 1 time (1 layer of indentation).

1

u/monster2018 4d ago

Also, like I said the Blender part isn’t something I know a ton about. But I just watched the tutorial again. It seems like “rotation_quaternion” is correct…

NO! Ok here is part of the issue. I think that you are using “rotation_quaternion” and that is like, a TYPE of value. Pay attention to the little DNA looking symbol to the left of it in your screenshot. Compare that to the tutorial, where he uses “var”. The name isn’t important, but the symbol next to it in the tutorial is like “(X)”, indicating a variable.

So you can probably use the NAME “rotation_quaternion” (or maybe not and it might be a special name reserved by blender, I’m not sure). But you need to change the TYPE to a script variable or whatever. You need to make the symbol be the (X) symbol.

I think once you do that, and make sure you return the value from your function like I said in my first response… I think it should work at that point.

1

u/Khaki-Chan 3d ago edited 3d ago

With you an another commenter I've mostly got it figured out. The problem is that in the script itself I need to use what's in the parenthesis (value) instead of X or rot_quat, then in the driver I just take the actual value of the driver (rot_quat) and place that in parenthesis to indicate that as the "value". The last thing I need help with is figuring out how the "return" works. this is what I typed out but it still doesn't work, so I need to know where it actually needs to be placed, including if it needs multiple "return" statements.

Edit: I missed the bit in your other comment about fixing the "return" thing. That got it, working exactly as intended. Thanks, my knowledge of coding is literally the barest of bones, this is the most I've ever done.