Eyeon:Script/Tutorials/Get Loader Filenames/Composition Scripts

From VFXPedia

Jump to: navigation, search

Lesson 2.1 : Composition Scripts

The console's primary use is for testing and debugging scripts. In most cases the testing you'll be doing in the console will be utilized in Composition Scripts. Composition Scripts are scripts that are run from the scripts menu.

To run comp scripts, they need to be in the Fusion:\Scripts\comp directory. To create a comp script, all you need is a text editor. We suggest using the SciTe editor, which comes with the default install of Fusion (Fusion:\Utils\SciTe\SciTe.exe). The SciTe editor has a number of helpful guides for scripting purposes, but if you prefer, notepad will suffice.

Before we begin, let's think about an example scenario: for whatever reason, a compositor needs a list of every single piece of footage being used in a composition, except they have well over fifty loaders in their comp. They begin copying every single Clip out of the loader and pasting it into notepad, but then you, being the scripter that you are, dash in, and save the day with the aid of a minimal amount of code, which gains you fame, fortune, and perhaps some sort of a tropical island, or at least a temperate peninsula.


Okay, perhaps the thanks won't be as grand as that, but there's no reason a task this rote needs to be done manually.

Go into your text editor and save a file called "List Loader Files.eyeonscript" in the comp scripts directory. To start, we should make a quick sketch of the steps we're going to need to go through to make the script work. We can accomplish this through comments.

A comment is a line in a script that will be ignored by the script interpreter. eyeonScript will ignore any code with two dashes ("--") in front of it.

This has two primary uses: 1) it allows the scripter to leave pieces of information about the code in plain language and 2) allows us to take out pieces of code without deleting them for testing purposes. To sketch out the steps that will need to be taken, we could write the following in our text editor:

-- Get a list of all tools
-- Cycle through all the tools
-- Find which ones are loaders
-- print their filenames to the console 

The first step, as we can see in our comments, is to get a list of all the tools. There are a couple of ways to do this. The first employs a function that you'll probably use quite frequently in scripting scenarios: the GetToolList function. Open a comp with a few loaders already in it. In the console:

toollist = composition:GetToolList()
dump(toollist)

In the above code, we asked composition (the composition variable is, by default, equal to whatever the currently open composition is) to give us a table of all the tools present in it and return it to the variable "toollist". The toollist variable was then dumped to the console, giving a list of every single tool. For loaders, the special Loader identifier is used, but in order to use that, we'd have to convert that object identifier into a string, and then check to see if the first characters were equal to "Loader". A much simpler method is to utilize the GetAttrs function and check to see if its registry ID (TOOLS_RegID) is equal to "Loader" (the RegID attribute identifies what kind of tool is being queried).

In the text editor:

-- Get a list of all tools
toollist = composition:GetToolList()
 
-- Cycle through all the tools
for i, tool in toollist do
    attrs = tool:GetAttrs()
 
    -- Find which ones are loaders
    if attrs["TOOLS_RegID"] == "Loader" then
    end
end

Let's take a second and break that down.

The first step is to get a table of all tools in the composition. The second part is to then use a for statement to cycle through every single entry in the toollist table. In the above case, i would be equal to the table index (in this case, the table is numerically identified), and tool would be equal to the entry's value (the current tool).

After that point, we've asked the current tool to give us its attributes. We've then checked the RegID entry to see if it's equal to "Loader". Note that two equal signs were used to denote this - see the chapter on relational operators for more information (). The script can now be completed.

-- Get a list of all tools
toollist = composition:GetToolList()
 
-- Cycle through all the tools
for i, tool in toollist do
    attrs = tool:GetAttrs()
 
    -- Find which ones are loaders
    if attrs["TOOLS_RegID"] == "Loader" then
        -- print their Clips to the console
        print(tool.Clip[composition.CurrentTime])
    end
end

Now that you're done, go into your scripts menu and run the script. In the console you should now have a full list of every single loader in the comp.

You may have noticed that this only functions on a bare bones level. For instance, it doesn't deal with cliplists (it only prints the Clip at the composition's current time), nor does it format the text in a nice way and lastly, it doesn't give any notices to the user to tell them what the script is doing.

The next lesson will cover these concepts.


Tips for Composition Scripts (edit)

EyeonTips:Script/Tutorials/Get Loader Filenames/Composition Scripts