Help using a function in a driver

Hi, I’m very new to python and try to use a coded function in the driver editor.
Here is a pic of my code and driver:


As you mght see my goal is to have the driver value set 1 when X > 0, and 0.5 when X < 0. And though I don’t get any error messages my driver value is for some reason stuck on zero, no matter what I change the input X to.

Any help?

What are you driving?

A “copy rotation” bone constraint. Why?

I’d try such expression:

(X&gt;0.0)*1.0 + (1-(X&gt;0.0))*0.5

There’s no way of executing functions in drivers’ controls, but the line above does exactly what you want.

(X>0.0) equals 1.0 when X is greater than 0.0. This is the equivalent of saying “if X>0.0”.
(1-(X>0.0)) equals 1.0 when X is NOT greater than 0.0, so it’s the equivalent of saying “else”.

so:
(X>0.0)*1.0 means if X > 0.0 then a=1.0
then we add the value we want if X is not greater than 0.0, so the final formula is:
(X>0.0)*1.0 + (1-(X>0.0))*0.5

Hope it makes sense. Even if it doesn’t - it at least works :slight_smile:

Thanks, it makes sence and works! :smiley:

Somethings don’t drive well, nodes for example.

You can use methods / props / classes whatever defined in the driver namespace in expressions. Attached a simple example showing defined method double in action.

Not that it matters now but were there any messages in the console? Putting a print statement in the driver method will show if it is being called correctly.

Also another, and possibly more elegant, way of writing your expression would be


1 if X &gt; 0.0 else 0.5

Attachments


WOW! Didn’t know it’s possible. Thanks for the tip.
And this “1 if X>0 else 0.5” thing… What can I say… THANK YOU!!!
You don’t even imagine how many times I was writing all those weird equations in my drivers…
I didn’t know that it can be done in such elegant fashion.