< Previous | Contents | Next >

The Real Script

Breaking down our intended script in steps the following functionality needs to be implemented:

1. Get and store the current selected tool, if it is a Saver.

2. Iterate through all Savers in the composition.

3. Set these to PassedThrough if they do not match our initial selection.

In Scripts that are executed directly within Fusion two variables are accessible by default:

fusion and composition. In order to save typing, you can also use the short form fu and comp.

As their names indicate with fusion, you gain access to the applications properties and methods, while composition represents anything in the composition.

As all the tasks in this particular script concern the Composition, all required methods are to be found in this object or its members. First of all:

comp:GetToolList(bool selected, string type = nil)


Returns all tools in the composition, or only the selected ones if the argument is set to true. The type argument is optional. It can be used to filter only specific types of tools.

The tool itself is in fact an object of type Tool or Operator. As you can see, Fusion’s application model follows the object-oriented programming concept, which will be examined in detail in the following chapters.

image

Note

Most of the objects in the Scripting API have a base class called Object. Objects may have common properties, one of them being the storage of Attributes. Attributes represent a serializable state of the tool beyond its actual Inputs.

A tool has various properties and methods. But what we are looking for is an Attribute.



The common attribute to read and write the PassThrough state of a tool is a boolean called TOOLB_ PassThrough.

Since in this case we will only be setting it, all we need is:


tools:SetAttrs( { TOOLB_PassThrough = True } )


Note that we pass in a tuple, hence the curly brackets, as we could pass in multiple attributes to be set at once.

With these two commands, we can accomplish all the tasks needed for this script.

if(currentSaver == currentSelectedSaver) then

for j, currentSelectedSaver in pairs(selectedSavers) do

Source File: 01 Disable Unselected Savers


comp:Lock()


local selectedSavers = comp:GetToolList(true, “Saver”) local allSavers = comp:GetToolList(false, “Saver”)

for i, currentSaver in pairs(allSavers) do


local isSelected = false





image

if isSelected == false then


currentSaver:SetAttrs( { TOOLB_PassThrough = true } ) end

end


comp:Unlock()

comp:Unlock()

comp:Lock()

end


end

isSelected = true

The first and last statement have not been introduced yet.



Whenever the composition needs to change its objects or data, you should Lock the composition, and Unlock it at the end. This guarantees to prevent race conditions, unnecessary redraws but also suppresses Dialogs, e.g., when a Loader or Saver is added to the Flow.

The following two lines simply return a tuple of all selected Saver and all Savers respectively.


selectedSavers = comp:GetToolList(true, “Saver”)


allSavers = comp:GetToolList(false, “Saver”)


The first loop iterates over all Savers.

The next iteration over each selected Saver compares all the selectedSavers with the currentSaver of the iteration. Since all the selected Savers are also within the collection of allSavers, we can tell for sure if the currentSaver has been selected or not.

If it has not been selected, then we set the currentSaver to PassThrough, which is equivalent to setting the tool to PassThrough in the FlowView.

At the end, we Unlock the composition as mentioned before.

Save the script. Switch to Fusion, create a bunch of Savers. Select few of them and run the script. All but the selected Savers should be set to PassThrough now.