BTNCS_Execute just one single function of script

SirEdric
Fusionista
Posts: 853
Joined: Tue Aug 05, 2014 10:04 am
Been thanked: 60 times
Contact:

BTNCS_Execute just one single function of script

Hey there.

With a command like BTNCS_Execute = "@Scripts:\\Comp\\Dev\\myScript.lua" one could run a script
from a (custom) Button of a tool.
But is there a way to run only a certain function out of that script?
Something like BTNCS_Execute = "@Scripts:\\Comp\\Dev\\myScript.lua[function1()]"?

Reason is, that I have quite a few scripts that need to be run from quite a few buttons of one tool,
and instead of having to maintain 10 individual scripts, I'd like to have just a single one,
but executing only certain blocks of it.

Another idea would be if there was a way to pass a value on to that script,
according to which button was pressed in the tool,
so that I could do something in the script itself like

Code: Select all

if button == "Click Me" then
function1()
elseif button == "Hit Me" then
function2()
else
function3()
end

Cheers & all the best.

Eric.

SirEdric
Fusionista
Posts: 853
Joined: Tue Aug 05, 2014 10:04 am
Been thanked: 60 times
Contact:

Re: BTNCS_Execute just one single function of script

Most interestingly a starting script like

Code: Select all

myScript = [[C:\some\path\argTest.lua]]

With myScript containing

Code: Select all

local arg={...}
print(...)
print("First Arg: " .. arg[1])

works just fine when called from said script.
Unfortunately BTNCS_Execute = "loadfile(@Scripts:\\Comp\\Dev\\argTest.lua)(10,20,30)", (and a few variations thereof) won't work.
I feel I'm getting closer to the solution but probably missing some vital syntax here...?

PeterLoveday
Fusioneer
Posts: 119
Joined: Sun Sep 14, 2014 6:09 pm
Been thanked: 5 times

Re: BTNCS_Execute just one single function of script

Another option is to use the info passed to BTNCS_Execute - 'self' and 'tool'.

'tool' is less useful for which button, but of course useful for other things, but 'self' refers to the button (Input) that is executing it. So self.ID will give you the ID of that button, which might be able to be used to uniquely trigger different script behaviour.

SirEdric
Fusionista
Posts: 853
Joined: Tue Aug 05, 2014 10:04 am
Been thanked: 60 times
Contact:

Re: BTNCS_Execute just one single function of script

Thanks for chiming in Peter!

But BTNCS_Execute = "loadfile('@Scripts:\\Comp\\Dev\\argTest.lua')(10,20,30)" only gives me
[string "???"]:1: invalid escape sequence near ''@Scripts:'

The self.ID sounds intriguing, but how would I pass that on to the script?

Salmiakki.

Eric.

Added in 36 minutes 28 seconds:
Righto....so here's what seems to work...

1.) Apparently using loadfile() or such in a button context seems to hate backslashes.
So neither 'C:\some\path\script.lua', nor 'C:\\some\\path\\script.lua' will work.
It actually has to be 'C:/some/path/script.lua' with forward slashes.

2.) Using that method, the @Scripts: variable won't work anymore.
So instead of '@Scripts:/Comp/Dev/script.lua' we need to lay out the entire path like
'C:/Users/bla/AppData/Roaming/Blackmagic Design/Fusion/Scripts/Comp/Dev/script.lua'

3.) We need to suffix the BTNCS_Execute with a return, so the entire line looks like:
BTNCS_Execute = "loadfile('C:/Users/bla/AppData/Roaming/Blackmagic Design/Fusion/Scripts/Comp/Dev/argTest.lua')(10,20,30); return",

4.) Now for the cool stuff!
The self.ID @PeterLoveday stated apparently gets passed on to the called script automagically!!
So with a buttonID of "WTF", the script

Code: Select all

print("Hello")
print(self.ID)
local arg={...}
print(...)
print("First Arg: " .. arg[1])
print("Bye")

Will nicely output

Code: Select all

Hello
WTF
10 20 30
First Arg: 10
Bye

Awesome. Thanks for the hints!

Eric.

Added in 4 minutes 44 seconds:
Oh, one more thing.
Apparently when we want to use that method, we must pass at least one empty arg to the script.
So BTNCS_Execute = "loadfile('C:/some/path/argTest.lua')(); return", would nicely print self.ID,
while BTNCS_Execute = "loadfile('C:/some/path/argTest.lua'); return", won't.

PeterLoveday
Fusioneer
Posts: 119
Joined: Sun Sep 14, 2014 6:09 pm
Been thanked: 5 times

Re: BTNCS_Execute just one single function of script

If you're using self.ID just go back to @Scripts: and forget all the loadfile stuff.

SirEdric
Fusionista
Posts: 853
Joined: Tue Aug 05, 2014 10:04 am
Been thanked: 60 times
Contact:

Re: BTNCS_Execute just one single function of script

Would be nice if it worked, but
BTNCS_Execute = "@Scripts:/Comp/Dev/argTest.lua",
does not seem to pass along self......:-/

Added in 6 minutes 56 seconds:
Ah!
It's actually BTNCS_Execute = "@Scripts:/Comp/Dev/argTest.lua"(), (with those parenthesis) then!

Added in 8 minutes 58 seconds:
Hmmm....but it only works, when I have just one Button in my tool.
As soon as there are more than on, without the loadfile the tool won't even paste into the Fusion comp....

SirEdric
Fusionista
Posts: 853
Joined: Tue Aug 05, 2014 10:04 am
Been thanked: 60 times
Contact:

Re: BTNCS_Execute just one single function of script

Actually....if you have multiple buttons and want some flexibility in terms of where to run the script(s) from,
you can even utilize the CustomData of the tool...

Code: Select all

{
Tools = ordered() {
t1_6 = Shape3D {
CtrlWZoom = false,
NameSet = true,
CustomData = {
runFrom = { "C:\\Users\\e.westphal\\AppData\\Roaming\\Blackmagic Design\\Fusion\\Scripts\\Comp\\Dev\\", "C:\\some\\other\\path\\" },
},
Inputs = {
["MtlStdInputs.MaterialID"] = Input { Value = 1, },
["SurfacePlaneInputs.ObjectID.ObjectID"] = Input { Value = 1, },
},
ViewInfo = OperatorInfo { Pos = { 2255, 610.5 } },
UserControls = ordered() {
WTF = {
INP_Integer = false,
INPID_InputControl = "ButtonControl",
BTNCS_Execute = "dofile(tool:GetData('runFrom')[tool['location'][1]+1] .. 'argTest.lua'); return",
},
BlaBut = {
INP_Integer = false,
INPID_InputControl = "ButtonControl",
BTNCS_Execute = "dofile(tool:GetData('runFrom')[tool['location'][1]+1] .. 'argTest.lua'); return",
},
TestBut = {
INP_Integer = false,
INPID_InputControl = "ButtonControl",
BTNCS_Execute = "print(tool['location'][1])",
},
location = {