6
$\begingroup$

In the compositor I would like to add a driver to the Switch Node. I want to watch a frame value from a VSE strip.
When the current frame count equals the Strip start frame I want to switch the node.

Here is an example of what I hope to achieve:

  • When current frame is less than abs(var)+var_001 then pass no picture,

  • if current frame is greater than abs(var)+var_001 then switch source through.

Of course what I have entered is unformatted nonsense. And the switch node expects to return a true or false, not a value.

*nb: abs(var) changes negative number to integer.

Example switch driver concept

Can I define the abs(var)+var_001 (it is the VSE strip trimmed start time) as a function of some sort to call elsewhere? It's a pain to define it all the time.

$\endgroup$
6
  • 1
    $\begingroup$ abs(var) doesn't change a value into an integer value (which are only natural numbers), but gives their absolute value, which is the same like multiplying by -1 if negative or in other words removing the negative sign if applicably. $\endgroup$
    – Samoth
    Commented Mar 3, 2016 at 13:03
  • 1
    $\begingroup$ Oh and the "trimmed start time" of a Strip is probably already available to you via its frame_final_start attribute. $\endgroup$
    – Samoth
    Commented Mar 3, 2016 at 13:50
  • $\begingroup$ Thanks for the tip, I was only grabbing paths from the variables exposed in the Strip Properties, I never thought to use any of the other properties. $\endgroup$
    – 3pointedit
    Commented Mar 6, 2016 at 22:18
  • 1
    $\begingroup$ To find out, which attributes exist from a given Property, just copy the Path like bpy.data.scenes['Scene'].sequence_editor.sequences_all[0]. including the following . into a Python Console Editor view and hit Ctrl + Space. The autocompletion feature will suggest you all available ones. $\endgroup$
    – Samoth
    Commented Mar 7, 2016 at 10:03
  • $\begingroup$ Is there something you're still missing in my answer below? $\endgroup$
    – Samoth
    Commented Mar 7, 2016 at 22:19

2 Answers 2

2
$\begingroup$

It works for me with the plain Expr: abs(var) < frame. So basically you just need a Boolean expression (omit the if in your expression) for it to drive the Switch. Basically any numerical result below 1.0 will result in Off and starting from 1.0 and above will result in On.

You can put something like (abs(var) < frame) and (abs(var) + var_001 > frame) into the box to evaluate only True during the duration of your Strip after you changed your var_001 to the frame_final_duration.

Furthermore you could Copy'n'Paste drivers if you want to use them again.

When you want to use your own function to add more complicated stuff like conditional expressions, stick to the Example code in the documentation about the Driver Namespace:

This script will add a function to the driver namespace, which can then be used in the expression driver_func(frame):

import bpy

def driver_func(val):
    return val * val    # return val squared

# add function to driver_namespace
bpy.app.driver_namespace['driver_func'] = driver_func

But giving your variables meaningful names as soon as possible is a good practise... ;-)

$\endgroup$
11
  • 2
    $\begingroup$ Can also use 1 if (expr) else 0 .. unnecessary for booleans handy for returning other values, eg 10 if(var > frame) else 99 $\endgroup$
    – batFINGER
    Commented Mar 3, 2016 at 13:36
  • $\begingroup$ Kind of... I updated the answer, you need to use a function for this. Don't forget to upvote ;-) $\endgroup$
    – Samoth
    Commented Mar 3, 2016 at 13:52
  • 1
    $\begingroup$ See what you mean, it either works as you say or throws an error TypeError: bpy_struct: item.attr = val: SomeObjectType.some_bool_prop expected True/False or 0/1, not float Think I'll stick with using 0/1 or True/False for on / off. $\endgroup$
    – batFINGER
    Commented Mar 5, 2016 at 15:18
  • 1
    $\begingroup$ Ummm, thanks... thats me! I made the video after all of the help that I got here. Thanks everyone :D $\endgroup$
    – 3pointedit
    Commented Mar 20, 2016 at 13:22
  • 1
    $\begingroup$ I'd like to say thank you because your answer and batFINGER's comments pointed me in the right direction. I needed an Integer slider with possible values 1, 2, 3, to be the driver that activated and deactivated 3 constraints. I wanted value 1 to activate constraint 1 (and deactivate constraints 2 and 3), value 2 to activate constraint 2 (and deactivate constraint 1 and 3), and value 3 to activate constraint 3 (and deactivate constraints 1 and 2). These are the three I used: (1 if (1==(your_var_here)) else 0) (1 if (2==(your_var_here)) else 0) (1 if (3==(your_var_here)) else 0) $\endgroup$ Commented Mar 25 at 4:08
1
$\begingroup$

I'd like to say thank you because your answer and batFINGER's comments pointed me in the right direction. I needed an Integer slider with possible values 1, 2, 3, to be the driver that activated and deactivated 3 constraints. I wanted value 1 to activate constraint 1 (and deactivate constraints 2 and 3), value 2 to activate constraint 2 (and deactivate constraint 1 and 3), and value 3 to activate constraint 3 (and deactivate constraints 1 and 2). These are the three scripted driver expressions I used, one in each of the 3 constraints:

(1 if (1==(your_var_here)) else 0)

(1 if (2==(your_var_here)) else 0)

(1 if (3==(your_var_here)) else 0)

You can modify this for greater than, less than, greater than or equal to, or less than or equal to with:

<,>,<=,>=

Maybe you'd like to set a range of values that activates the boolean to true? This example sets the boolean to true if your_var_here is greater than 1, but lower than 2.

(1 if (1<(your_var_here)<2) else 0)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .