BTNCS_Execute just one single function of script

User avatar
SirEdric
Fusionator
Posts: 1113
Joined: Tue Aug 05, 2014 10:04 am
Been thanked: 99 times
Contact:

BTNCS_Execute just one single function of script

#1

Post by SirEdric » Tue Jul 10, 2018 11:12 pm

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.

User avatar
SirEdric
Fusionator
Posts: 1113
Joined: Tue Aug 05, 2014 10:04 am
Been thanked: 99 times
Contact:

Re: BTNCS_Execute just one single function of script

#2

Post by SirEdric » Wed Jul 11, 2018 2:51 am

Most interestingly a starting script like

Code: Select all

myScript = [[C:\some\path\argTest.lua]]
loadfile(myScript)(10,20,30)
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...?

User avatar
PeterLoveday
Fusioneer
Posts: 127
Joined: Sun Sep 14, 2014 6:09 pm
Been thanked: 13 times

Re: BTNCS_Execute just one single function of script

#3

Post by PeterLoveday » Wed Jul 11, 2018 3:03 am

Maybe BTNCS_Execute = "loadfile('Scripts:\\Comp\\Dev\\argTest.lua')(10,20,30)" ?

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.

User avatar
SirEdric
Fusionator
Posts: 1113
Joined: Tue Aug 05, 2014 10:04 am
Been thanked: 99 times
Contact:

Re: BTNCS_Execute just one single function of script

#4

Post by SirEdric » Wed Jul 11, 2018 3:43 am

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.

User avatar
PeterLoveday
Fusioneer
Posts: 127
Joined: Sun Sep 14, 2014 6:09 pm
Been thanked: 13 times

Re: BTNCS_Execute just one single function of script

#5

Post by PeterLoveday » Wed Jul 11, 2018 5:02 am

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

User avatar
SirEdric
Fusionator
Posts: 1113
Joined: Tue Aug 05, 2014 10:04 am
Been thanked: 99 times
Contact:

Re: BTNCS_Execute just one single function of script

#6

Post by SirEdric » Wed Jul 11, 2018 6:07 am

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

User avatar
SirEdric
Fusionator
Posts: 1113
Joined: Tue Aug 05, 2014 10:04 am
Been thanked: 99 times
Contact:

Re: BTNCS_Execute just one single function of script

#7

Post by SirEdric » Thu Jul 12, 2018 5:48 am

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",
					LINKID_DataType = "Number",
					LINKS_Name = "WTF",
				},
				BlaBut = {
					INP_Integer = false,
					INPID_InputControl = "ButtonControl",
					BTNCS_Execute = "dofile(tool:GetData('runFrom')[tool['location'][1]+1] .. 'argTest.lua'); return",
					LINKID_DataType = "Number",
					LINKS_Name = "BlaButton",
				},
				TestBut = {
					INP_Integer = false,
					INPID_InputControl = "ButtonControl",
					BTNCS_Execute = "print(tool['location'][1])",
					LINKID_DataType = "Number",
					LINKS_Name = "TestButton",
				},
				location = {
					LINKS_Name = "location",
					LINKID_DataType = "Number",
					INPID_InputControl = "ComboControl",
					INP_Integer = false,
					{ CCS_AddString = "loc1" },
					{ CCS_AddString = "loc2" },
					CC_LabelPosition = "Horizontal",
				}
			}
		}
	},
	ActiveTool = "t1_6"
}