5

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.

Samoth
  • 3,234
  • 3
  • 21
  • 85
3pointedit
  • 8,938
  • 1
  • 22
  • 48
  • 1
    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. – Samoth Mar 03 '16 at 13:03
  • 1
    Oh and the "trimmed start time" of a Strip is probably already available to you via its frame_final_start attribute. – Samoth Mar 03 '16 at 13:50
  • 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. – 3pointedit Mar 06 '16 at 22:18
  • 1
    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. – Samoth Mar 07 '16 at 10:03
  • Is there something you're still missing in my answer below? – Samoth Mar 07 '16 at 22:19
  • Oops, sorry I forgot to tick it. I don't ask very often so this is a bit new to me. Thank you for your help Samoth – 3pointedit Mar 08 '16 at 00:37

2 Answers2

2

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... ;-)

Samoth
  • 3,234
  • 3
  • 21
  • 85
  • 2
    Can also use 1 if (expr) else 0 .. unnecessary for booleans handy for returning other values, eg 10 if(var > frame) else 99 – batFINGER Mar 03 '16 at 13:36
  • Kind of... I updated the answer, you need to use a function for this. Don't forget to upvote ;-) – Samoth Mar 03 '16 at 13:52
  • Can I define a name for variables in the driver window to use on other drivers? I thought you could only do that in text editor as a script. EDIT: oh i see so I define name via script which i can use in the driver entry – 3pointedit Mar 05 '16 at 01:18
  • The variable names in the driver window should match with the ones in the function (in your script), otherwise it should work as well when you assign them directly in the function call like var + function(var=frame). But when there is only one variable that explicit name assignment might be omitted like in the example where frame in the driver properties will be assigned to var inside the function for calculation... The python documentation should help you out here... ;-) – Samoth Mar 05 '16 at 07:59
  • Come on - what was the reason for a downvote here again? – Samoth Mar 05 '16 at 10:11
  • " Basically any numerical result below 1.0 will result in False and starting from 1.0 and above will result in True." Clarification on this, are you saying bool(0.9) and bool(-1) equate to False? – batFINGER Mar 05 '16 at 10:20
  • No, not when you use the bool(x) -> bool function (Returns True when the argument x is true, False otherwise.) and I didn't say this - but when you put these values into the Scripted Expression field in the driver to drive the Switch Node you'll see that it does so... – Samoth Mar 05 '16 at 10:26
  • 1
    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. – batFINGER Mar 05 '16 at 15:18
  • @3pointedit related: https://www.youtube.com/watch?v=jvFqhDE_vW8&feature=youtu.be ;-) – Samoth Mar 19 '16 at 21:55
  • 1
    Ummm, thanks... thats me! I made the video after all of the help that I got here. Thanks everyone :D – 3pointedit Mar 20 '16 at 13:22
  • 1
    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) – Coby Randal Mar 25 '24 at 04:08
0

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:

<,>,<=,>=

Coby Randal
  • 716
  • 4
  • 9