Eyeon:Script/Tutorials/Get Loader Filenames/Adding Functionality

From VFXPedia

< Eyeon:Script | Tutorials | Get Loader Filenames
Revision as of 04:06, 4 November 2008 by Daniel (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Lesson 2.2 : Adding Functionality

The code in the previous example is useful in most circumstances, but not all. When you finish a script, you should try to look it over to see if the script could've been written in a more elegant manner, or if you can anticipate any scenarios where the script will fail.

The previous example showed you how to access an attribute to get the RegID for a tool. There's actually a built in function available in the eyeon.eyeonscriptlib file (Fusion:\scripts\) called GetLoaders that will return a table of all loaders in a composition. The method used by the function is similar to that shown in the previous example.

In this case, we should use this function instead, as it will decrease the amount of code in the script's body.

-- Get a list of all loaders
loaderlist = eyeon.GetLoaders(composition)
 
-- Cycle through all the loaders
for i, tool in loaderlist do
    -- print their Clips to the console
    print(tool.Clip[composition.CurrentTime])
end

However, this still doesn't take into situations where a loader has multiple clips. Choose a loader from your comp that has multiple clips (or make one) and dump its attributes to the console.

==Loader1:GetAttrs()

In this text dump, you should see an entry called TOOLST_Clip_Name. This table will contain all the clips being utilized by the loader in question. So, when we write our code, we'll want to do the following steps in addition what's above:

-- Get the tool's Clip List attribute
-- cycle through that table
-- print each entry to the console. 

First off, we've used the for statement in one specific way so far.

Another equally correct manner is to define the start and end counting points, and then count through the table in that manner. We know, by looking at the results from the above attributes dump, that the table will start at one, but the last number in the table is unknown.

To determine that, we could use the table.getn function, which will return a number equal to the number of numeric table indices (it will not count entries that have text names instead of numeric ones). That number should be equal to the total number of entries in the clip list.

-- Get the tool's Clip List attribute
attrs = tool:GetAttrs()
 
-- cycle through that table
for j = 1, table.getn(attrs["TOOLST_Clip_Name"]) do
    -- print each entry to the console.
    print(attrs["TOOLST_Clip_Name"][j])
end

In this case, j was equal to the table entry number. However, what if the loader being scanned has no clip in it at all? Get the attributes of a loader with no clip in it.


There's no TOOLST_Clip_Name entry in it, and without the attribute being present the variable value of TOOLST_Clip_Name will equal nil. This means that the above piece of code will provoke an error on loaders with no clips. We need to first check to see if the loader has a clip at all. To do this, we simply use an if statement to see if the entry has a value (if it does not equal nil).

The shorthand for this is seen below:

-- Get the tool's Clip List attribute
attrs = tool:GetAttrs()
 
-- check to see if there's a clip list
if attrs["TOOLST_Clip_Name"] then 
    -- cycle through that table  
    for j = 1, table.getn(attrs["TOOLST_Clip_Name"]) do
        -- print each entry to the console.
        print(attrs["TOOLST_Clip_Name"][j])
    end
end

Therefore, the code put together would look like this:

-- Get a list of all loaders
loaderlist = eyeon.GetLoaders(composition)
 
-- Cycle through all the loaders
for i, tool in loaderlist do
    -- Get the tool's Clip List attribute
    attrs = tool:GetAttrs()
 
    -- check to see if there's a clip list
    if attrs["TOOLST_Clip_Name"] then
        -- cycle through that table  
        for j = 1, table.getn(attrs["TOOLST_Clip_Name"]) do
            --print each entry to the console.
            print(attrs["TOOLST_Clip_Name"][j])
        end
    end
end

Making it Pretty

The last step is to make the text display in a more readable / aesthetically pleasing manner. We could do this by adding a 1) a header to the text displayed to the user when the script initially runs, letting them know where the information displayed from one script starts and the other ends, 2) Add a footnote to show that the script has ended, 3) print the loader name before the clips (stored in the attributes) and indent the clips. The third step is optional -- if the user needs to copy the information out of the console, then it might be best not to have any extra text formatting.

We'll deal with the first and second parts:

print("\n\n----------------------------------------------\nLIST
LOADER FOOTAGE:\n----------------------------------------------\n\n")
.
.
.
print("\nSCRIPT
COMPLETE\n----------------------------------------------\n")

Take notice of a couple of things. Firstly, the character \n is being used to create a line break, as is the standard for most programming languages. If you ever need a character to be recognized as a "\" for scripting purposes, you need to input two back slashes (\\ = \ in Lua).

Secondly, dashes don't create comments if they're inside quotation marks.

The next part is simple enough. All that we need to do is print the loader's name, indent the text with some characters (we'll use two dashes to align with the commenting method), and then add a line break afterwards. Put it all together and it looks like this:

print("\n\n----------------------------------------------\nLIST
LOADER FOOTAGE:\n----------------------------------------------\n\n")
 
-- Get a list of all loaders
loaderlist = eyeon.GetLoaders(composition)
 
-- Cycle through all the loaders
for i, tool in loaderlist do
    -- Get the tool's Clip List attribute
    attrs = tool:GetAttrs()
 
    -- check to see if there's a clip list
    if attrs["TOOLST_Clip_Name"] then
        -- print the loader's name
        print(attrs.TOOLS_Name)
 
        -- cycle through that table  
        for j = 1, table.getn(attrs["TOOLST_Clip_Name"]) do
            -- print each entry to the console.
            print("-- "..attrs["TOOLST_Clip_Name"][j])
        end
    end
end
 
print("\nSCRIPT COMPLETE:\n----------------------------------------------\n")

The last thing to note is that to put strings together, you need to use two periods to indicate to eyeonScript that it should stitch (concatenate) the values together.

In this chapter, we learned a bit about code optimization. In the next chapter, we'll learn how to output this information to a textfile.


Tips for Adding Functionality (edit)

EyeonTips:Script/Tutorials/Get Loader Filenames/Adding Functionality