User Login | Reseller Access
Products |Partners |Sales |Support |Downloads |News Events Community |Registration
Home » Techniques » Tips & Tutorials » Assigning Keyboard Shortcuts to Scripts


Assigning Keyboard Shortcuts to Scripts

Using the globals.HotKey table to run scripts with CTRL-SHIFT-#.

(Last Updated: June 29th, 2004)


Requirements: DFX+ 4.03b with Module 3 (scripting) or Digital Fusion 4.03b (or higher).
HotKeys.dfscriptlib:  script file (1 Kb)



As of version 4.03b, Digital Fusion (and DFX+ with Module 3) now allows you to trigger custom scripts using the keyboard shortcuts CTRL-SHIFT 0-9. You can use these key combinations to launch scripts, or just run basic functions.

For an example of how this is done, download the HotKeys.dfscriptlib file, and save the file in your Digital Fusion scripting subfolder. On my computer that folder was at c:\Program Files\Fusion\Scripts.

If you open HotKeys.dfscriptlib in a text editor (like notepad) you will see the first few lines look like this :

  globals.FlowHotKeys = {}
	
  FlowHotKeys[1]=[[Scripts:\Flow\Save New Version.dfscript]]
  FlowHotKeys[2]=function() flow:SaveAs() end
  FlowHotKeys[3]=[[Scripts:\Flow\Find Tool.dfscript]]
  FlowHotKeys[4]=[[Scripts:\Flow\List Frame Render Times.dfscript]]
	-- etc,.

What exactly is going on here? When you open or create a flow, Fusion will load and run this script. That will setup a table named FlowHotKeys. When you select CTRL-SHIFT-1 Fusion will run whatever script or code snippet is assigned as a value to FlowHotKeys[1]. In our example, that would be the flow script "Save New Version". You will note that virtual paths like 'Scripts:\' and 'Flow:\' are supported.

We can also run code snippets, as FlowHotKeys[2] demonstrates. Selecting CTRL-SHIFT-2 would run a function that would invoke flow:SaveAs() and display the Save As dialog.

We should have a closer look at FlowHotKeys[5] through [8] however, as these are tool scripts :

  FlowHotKeys[5]=[[Scripts:\Tool\Bake Animation.dfscript]]
  FlowHotKeys[6]=[[Scripts:\Tool\Split View Left.dfscript]]
  FlowHotKeys[7]=[[Scripts:\Tool\Split View Right.dfscript]]
  FlowHotKeys[8]=[[Scripts:\Tool\Mask Dependencies.dfscript]]

There is a problem here, because most tool scripts assume that they will be run with the variable 'tool' already pointing to the active tool. When we run a tool script from a keyboard shortcut, that turns out not to be the case, only the 'flow' variable is set. If you try to run these scripts without first adapting them for the possiblility that they may have been run from a shortcut, you will get an error message like this one :

  C:\Program Files\Fusion\Scripts\Tool\Split View Left.dfscript:31: 
    attempt to index global `tool' (a nil value)

We will have to modify any tool scripts we use slightly so they check the flow attribute FLOWH_ActiveTool. This attribute will contain a handle to the currently active tool in the flow, or nil if there is none. I use that to modify the scripts we call from our hotkeys with the following snippet of code :

  if not tool then
    tool = flow:GetAttrs().FLOWH_ActiveTool
    if not tool then
      print("This is a tool script, you must select a 
	  		tool in the flow to run this script")
      return
    end
  end

Versions of the scripts with the necessary changes are now available from the scripts webpage, or you can modify the scripts for yourself as an exercise if you already have copies.

Of course you can modify the HotKeys.dfscriptlib script to allow you to run any script or function you like, you are not limited to just running the examples here.

Thanks
Isaac Guenard
Product Manager, eyeon Software.

      © Copyright 1988-2012 eyeon Software | About Us | Contact Us | Careers | Privacy Policy