[Script] Launching scripts from buttons

User avatar
Tilt
Global Moderator
Posts: 336
Joined: Sat Aug 02, 2014 4:10 am
Location: Munich, Germany
Contact:

[Script] Launching scripts from buttons

#1

Post by Tilt » Wed Sep 17, 2014 3:41 pm

Fusion 7 finally allows you to add buttons as UserControls that can be used to launch scripts.

This is super useful if you want the user of your macros (or modified standard tools) to execute some additional functions right where he's using the tool. Previously, your first option was a tool script that the user had to find somewhere deep inside a context menu. Your second option was a custom menu command but that requires you to explain to your tool's users that they need to configure a menu somewhere.

So... long story short, you can now have a button that executes scripts. :lol:

More explanations from Peter Loveday can be found on pigsfly.

edit: I'll just quote Peter here so the documentation isn't scattered across the web
As has been partially pointed out, some variables are available for the script execution:
  • comp - the current Composition
  • tool - the Tool that owns this button
  • self - the Input for the button itself
Generally tool and comp are probably the most useful; the button input is of limited value. However, it does allow the same script file to be used with multiple buttons, and dispatch based on Input ID. The intention here being it may be more desirable in terms of maintainability and distribution to have a single external script used for several buttons within the same setting, rather than a collection of them.

As with most places script snippets can be used, it is possible to use a variety of prefixes to alter behaviour here:
  • An @ symbol at the start will specify that this is a filename, rather than a script snippet. This path will be mapped with the Comp's path-mappings, in some cases it might be desirable to position the script alongside the setting file. (Also see above comment regarding reuse of a single script with multiple buttons) eg:

    Code: Select all

    BTNCS_Execute = "@Scripts:Blah.eyeonscript"
    The script file language will be detected from the extension in this case. Python scripts with ".py", "py2" or ".py3" will force the appropriate language.
  • A language prefix can be applied. The default is Lua, but one can override that with using an ! and language name. Currently supported options are:
    "!Lua: " - Lua script - default
    "!Py2: " - Python 2.x script
    "!Py3: " - Python 3.x script
    "!Py: " - Python default version script
    Note the colon and space must be included, eg:

    Code: Select all

    BTNCS_Execute = "!Py3: print(tool)"

User avatar
SecondMan
Site Admin
Posts: 2257
Joined: Thu Jul 31, 2014 5:31 pm
Location: Vancouver, Canada
Been thanked: 44 times
Contact:

Re: [Script] Launching scripts from buttons

#2

Post by SecondMan » Wed Sep 17, 2014 4:35 pm

It's an absolutely awesome new feature! I'm planning to get a lot of use out of that in the future.

Suffice to say my Tracker has a few new buttons already :)

User avatar
Midgardsormr
Fusionista
Posts: 517
Joined: Wed Nov 26, 2014 8:04 pm
Location: Los Angeles, CA, USA
Been thanked: 37 times
Contact:

Executing a Script From a Button UserControl

#3

Post by Midgardsormr » Tue Feb 13, 2018 1:56 pm

In a conversation related to @ShadowMaker SdR's new Color Curves macro, he asked about executing scripts from a button on a tool. I told him I'd share an example I had lying around.
Code: [Select all] [Expand/Collapse] [Download] (Saver_Saver.setting)
  1. {
  2.     Tools = ordered() {
  3.         Saver1 = Saver {
  4.             CtrlWZoom = false,
  5.             Inputs = {
  6.                 ProcessWhenBlendIs00 = Input { Value = 0, },
  7.                 Clip = Input {
  8.                     Value = Clip {
  9.                         Length = 0,
  10.                         Saving = true,
  11.                         TrimIn = 0,
  12.                         ExtendFirst = 0,
  13.                         ExtendLast = 0,
  14.                         Loop = 1,
  15.                         AspectMode = 1,
  16.                         Depth = 1,
  17.                         GlobalStart = -2000000000,
  18.                         GlobalEnd = 0,
  19.                     },
  20.                 },
  21.                 OutputFormat = Input { Value = FuID { "TargaFormat", }, },
  22.                 ["Gamut.SLogVersion"] = Input { Value = FuID { "SLog2", }, },
  23.             },
  24.             ViewInfo = OperatorInfo { Pos = { 495, 82.5, }, },
  25.             UserControls = ordered() {
  26.                 MakeLoader = {
  27.                     ICS_ControlPage = "File",
  28.                     INPID_InputControl = "ButtonControl",
  29.                     LINKS_Name = "Make Loader",
  30.                     BTNCS_Execute = [[
  31.                         comp:Lock()
  32.                         flow = comp.CurrentFrame.FlowView
  33.                         posx, posy = flow:GetPos(tool)
  34.                         filepath = tool.Clip[1]
  35.                         myloader = comp:AddTool("Loader", posx+1, posy - 1)
  36.                         myloader.Clip = filepath
  37.                        
  38.                         -- parse file name
  39.                         pathtable = eyeon.split(filepath, "\\")
  40.                         -- find last index of table
  41.                         k = 0
  42.                         for i in ipairs(pathtable) do
  43.                             k = i
  44.                         end
  45.                         filename = pathtable[k]
  46.                         j,k = string.find(filename, "_v")
  47.                         if k then
  48.                             l = string.find(filename, "%.", k+1)
  49.                             version = string.sub(filename, k, l-1)
  50.                             filename = string.sub(filename, 1, j-1)
  51.                                                
  52.                             myloader:SetAttrs({TOOLS_Name = version.."_"..filename, TOOLB_NameSet = true})
  53.                         end
  54.                         comp:Unlock()
  55.                     ]],
  56.                 },
  57.             },
  58.         },
  59.     },
  60.     ActiveTool = "Saver1",
  61. }
This is our Saver default setting. The UserControls include a button called MakeLoader which, when pressed, executes a Loader from Saver tool script. The script is stored inside a Lua literal: [[ ]]. The literal characters allow you to create a string in which you don't have to escape quotation marks or other reserved characters. You can therefore write a script, enclose it in the double brackets, and assign that entire string to BTNS_Execute. When the user clicks the button, the script executes.

This also demonstrates that if you save settings on a node with a User Control, that control will be a part of the tool every time you make one. I have another script on our Loaders called Make Local, which checks to see if the file is in the proper project directory. If it isn't, it is copied there and relinked. Very handy if you have the habit of just saving elements to your desktop. I won't bother sharing that one here since it won't work outside the Muse pipeline. I just offer it as another example of the kinds of things you can do with a button of this kind.

User avatar
SecondMan
Site Admin
Posts: 2257
Joined: Thu Jul 31, 2014 5:31 pm
Location: Vancouver, Canada
Been thanked: 44 times
Contact:

Re: [Script] Launching scripts from buttons

#4

Post by SecondMan » Tue Feb 13, 2018 2:46 pm

Merged into earlier topic ;)

Thanks for the additional example! :cheer: