Eyeon:Script/Tutorials/Get Loader Filenames/IO and Lua

From VFXPedia

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

2.3 : IO and Lua

In the previous example, the user needed a text dump of all of the loaders' Clips in a given composition. Hypothetically, what would the user do if they had multiple compositions and needed to export the information to someone else in a text file? Although the user could simply copy the console information to a text file, another option is to handle it directly through script.

To create a new file that we will write the data to, we can use the io.output() function. When the io.output() function is called, it returns a file object (or an error message if it is unable to write to the specified location) and creates a default file to write to. With the file object, eyeonScript can then write to the file that has just been created using the write function. For example, the script could write to a new text file on the c drive called LoaderList employing the method below:

-- Define the file to write to.
fh, errormessage = io.output("c:\\LoaderList.txt")
 
-- Write our data.
fh:write("This is the first line\nThis is the second line")
 
-- Close the file.
fh:close()

However, if there is an error message, it should probably be reported to the user:

-- Define the file to write to.
fh, errormessage = io.output("c:\\LoaderList.txt")
 
-- check to see if it returned a value for fh
if not fh then -- if fh is equal to nil
    print(errormessage) 
    return
end
 
-- Write our data.
fh:write("This is the first line\nThis is the second line")
 
-- Close the file.
fh:close()

Note: The above method will delete any text that was originally in the file. Additionally, the text file itself will only update once it is closed by script.

At this point, the above code can be adapted for use with the original script. We should probably forego adding the text formatting that was added to the last version, as it's doubtful that anyone for whom this text file is being output cares about what loaders are using what footage.

print("\n\n----------------------------------------------\nLIST LOADER FOOTAGE:\n----------------------------------------------\n\n")
 
-- Define the file to write to.
fh, errormessage = io.output("c:\\LoaderList.txt")
 
-- check to see if it returned a value for fh
if not fh then -- if fh is equal to nil
    print(errormessage) 
    return
end
 
-- 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])
            
            --Write our data.
            fh:write(attrs["TOOLST_Clip_Name"][j].."\n")
        end
    end
end
 
fh:close()
 
print("\nSCRIPT
COMPLETE:\n----------------------------------------------\n")

Even though we're getting much closer to a complete script, however, the final script would benefit from a few more features.

The user could have multiple clips with the same name in a comp, or they might not want the text dump to go to the above file.

They also might not want to overwrite the contents of the text file and instead append the information to the end of the file. The next lessons will cover these issues.


Tips for IO and Lua (edit)

EyeonTips:Script/Tutorials/Get Loader Filenames/IO and Lua