Eyeon:Script/Tutorials/Command Line/Command Line Lesson 3
From VFXPedia
Lesson 4.3 : Checking for Errors
Part III - Checking For Errors
This example continues to work on the script we started in Lesson 1 and Lesson 2. Load MakeAVI.eyeonScript into notepad (or your text editor of choice).
Following is a list of the arguments we use in our script, and some things we need to check about each argument:
- argument 1 - sourcefile
was an argument provided? does the file exist?
- argument 2 - outfile
was the argument provided?
- argument 3 - width
was the argument provided? is the argument a number?
- argument 4 - height
was the argument provided? is the argument a number?
Our script should check the arguments that were passed to it before doing anything else. Let start with the input filename. Add the following lines to the beginning of the script, before any other lines.
-- checking argument 1 : the sourcefile if arg[1] == nil then print("Please provide a path to the image sequence.") os.exit(10) end
Lets look at this code, line by line. The first line starts with two dashes (--). In EyeonScript, these characters represent a comment, and they are ignored when the script is run. Comments are extremely important, because they are plain English guides to what the code is supposed to be doing. Comment everything, even the most obvious bits. When you have to modify a script you haven't looked at since you wrote it a year ago, the things that are obvious now won't be quite as obvious anymore.
The second line is where the real work begins. We are checking if arg[1], our source file name, is nil. If it is, then that means the user of our script did not provide the filename of an image to load. So we should stop execution, tell the user about their mistake so they can correct it, and exit the script immediately.
Notice that we used two equals signs in our 'if' comparison, instead of just one. When we are assigning a value to a variable, we use a single equal sign, but when we want to compare two values, we use doubles.
The print() and os.exit() commands come after the 'if' statement, and they will only run if the statement evaluates to true. If the comparison evaluates to false, then EyeonScript will jump past these lines and execute the next statement appearing after the line 'end'.
Now that we have checked to see if our first argument was provided, we should check to see if the file named even exists. Add the following lines beneath our first 'if' statement
if not fileexists( arg[1] ) then print("The file ".. arg[1] .." does not exist.") os.exit(20) end
There are two things worth note here - the first is the function fileexists(), the second is the use of '..' to concatenate strings of text together with a variable.
The fileexists() function takes the path to a file as it's single argument. It checks to see if that file is present on the disk, and if it is it returns true. Otherwise it returns false. Our 'if' statement checks the result of the fileexists(), and if it is 'not' true, then it executes the block of lines before the end statement.
We have used the print command to output values, as well as strings. In this example we wanted to output both strings and values together. To do that we had to use the concatenation operator '..', which is two periods in a row. Try running the script using the name of a file that does not exist for the first argument to see how this works.
Argument 2 will be simpler since we only need to check that a path has been provided, since the output file can't reasonably be assumed to exist until after our script is executed. Add the following lines to the script
-- checking argument 2 : the output file if arg[2] == nil then print("You must provide the path of " .. "the output image to be rendered.") os.exit(30) end -- checking argument 3 and 4 if arg[3] == nil then print("You must provide a width " .. "to resize the footage to.") os.exit(40) elseif tonumber( arg[3] ) == nil then print("The width provided (" .. arg[3] .. ") is not a number.") os.exit(40) end if arg[4] == nil then print("You must provide a height " .. "to resize the footage to.") os.exit(40) elseif tonumber( arg[4] ) == nil then print("The width provided (" .. arg[4] .. ") is not a number.") os.exit(40) end
The last two sets of 'if' statements check argument 3 and 4, to make sure that the values provided are either numbers, or strings of text that can be converted to numbers. They use the built in function tonumber() to check. If tonumber returns a value of nil, then EyeonScript was not able to convert the argument into a usable number.
Another function you have seen in each of these 'if' statements is the os.exit() function. This halts the execution of the script, and returns an errorlevel value to the program that ran the script. Batch files and other programs can check this errorlevel value to see if the script completed without errors.
Finally, lets add some error handling to the Fusion() function so that our script will exit gracefully if no copy of Fusion is found running on the system. Add the following lines after the Fusion() function.
fusion = Fusion("localhost") if fusion == nil then print("No instances of Fusion were found " .. "running on the local machine.") end
That concludes Part III - save the script and experiment with it by running it with invalid data for each argument, and see how it responds. The completed script can be seen below.
-- checking argument 1 : the sourcefile if arg[1] == nil then print("Please provide a path to the image sequence.") os.exit(10) end if not fileexists( arg[1] ) then print("The file ".. arg[1] .." does not exist.") os.exit(20) end -- checking argument 2 : the output file if arg[2] == nil then print("You must provide the path of " .. "the output image to be rendered.") os.exit(30) end -- checking argument 3 and 4 if arg[3] == nil then print("You must provide a width " .. "to resize the footage to.") os.exit(40) elseif tonumber( arg[3] ) == nil then print("The width provided (" .. arg[3] .. ") is not a number.") os.exit(40) end if arg[4] == nil then print("You must provide a height " .. "to resize the footage to.") os.exit(40) elseif tonumber( arg[4] ) == nil then print("The width provided (" .. arg[4] .. ") is not a number.") os.exit(40) end fusion = Fusion("localhost") if fusion == nil then print("No instances of Fusion were found " .. "running on the local machine.") end comp = fusion:LoadComp("c:\\lessons.comp") SetActiveComp(comp) Loader1.Filename = arg[1] Resize1.Width = arg[3] Resize1.Height = arg[4] Saver1.Filename = arg[2] comp:Save("c:\\example_avi.comp") comp:Render(true, 0, 59, 1) comp:Close()
Tips for Command Line Lesson 3 (edit)
EyeonTips:Script/Tutorials/Command Line/Command Line Lesson 3