7

I started to get into Drives. I want one cube(green) to rotate 90° whenever the other cube(red) is doing it's last 90° turn on the way to the full 360°. Now i've come up with following Logic depending on the red cubes rotation(rot).

[when sin(rot)<0 and cos(rot)>0 add 90° to current rotation]

Can someone show me the right way to a working expression?

enter image description here

Järv
  • 91
  • 1

1 Answers1

4

This can be done two ways :
scripted expression : which should be one line of code and it may get complicated
driver function : this will make it a lot easier to control and create complex driver, we define a function then use it in the scripted expression


scripted expression :

floor(b/(2*pi))*pi/2 + (cos(b)>0 and sin(b)<0)*(b-1.5*pi) + (b< 0)*2*pi

where b is the angle of the red cube

enter image description here


driver function:

we define a new function calc_angle() and add it to the driver namespace, this function calculates the angle the same as the scripted expression but its easier to read, update and reuse in multiple drivers . run the script in blender's text editor

import bpy
from math import *

two_pi = 2*pi
pi_two = pi/2

#create new function
def calc_angle(angle):

        new_angle = floor(angle/two_pi)*pi_two

        if cos(angle) > 0 and sin(angle) < 0 :                
            if angle > 0 :
                new_angle += angle-1.5*pi
            else :
                new_angle += two_pi + angle-1.5*pi
        return new_angle

#add the function to the driver namespace    
bpy.app.driver_namespace['calc_angle'] = calc_angle

and setup the driver as follows :

enter image description here

Chebhou
  • 19,533
  • 51
  • 98