Eyeon:Script/Tutorials/Command Line/Command Line Lesson 2

From VFXPedia

< Eyeon:Script | Tutorials | Command Line
Revision as of 05:42, 4 November 2008 by Daniel (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Lesson 4.2 : Adding Arguments

Part II - Making Our Script Useful

In Lesson 1 we created a script that resizes a predetermined clip to a fixed size, and saves the result out to a fixed name. This is not very useful. To make this script more exciting we need to make it flexible, and that means enabling it to accept arguments.

Arguments are entered at the command line, and can be used to customize the behavior of your script when it runs. For example in part I we ran eyeonScript with the argument '-i', which told it to run in interactive mode.

We can pass arguments to our own script as well. In the case of the our first script, we could benefit from being able to tell the script what file to load, what file to save, and what size to rescale to. So lets add this ability.

eyeonScript stores the arguments it recieves when it runs in a table called 'arg'. To demonstrate this, open notepad and enter the following script:

print("Checking arguments\n")
print(arg[1])
print(arg[2])
print(arg[3])

Save the file as args.eyeonScript in the directory where EyeonScript.exe is located. (Usually this will be the same directory as your copy of Fusion)

Now open a command prompt, navigate to your scripting directory, and run your script as follows.

C:\Fusion\Script eyeonScript args.eyeonScript

The output of your script should be:

Fusion Script Interpreter
Copyright (C) 2002 eyeon Software

checking arguments
nil
nil
nil

Now run the script as follows:

C:\Fusion\Script eyeonScript args.eyeonscript 100 Testing

The output should be:

checking arguments
100
Testing
nil

What we learn from this is that each argument is stored in a table, indexed in the order that they appear. The first argument is stored as arg[1] - the second as arg[2]. The third element in our table is nil, because we did not provide a third argument to our script.

Now lets modify the script we created in Part I to allow us to specify the input filename, the output filename, and the scale. Load the script you saved in the last lesson (you did save it, right?) We need to make the following changes :

fusion = Fusion("localhost")
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()

Once you have made the required changes save the script and try running it as follows.

eyeonScript MakeAVI "c:\images.0000.jpg" "c:\example_avi.comp" 160 120 

The result should be exactly like it was in Lesson 1, the source files are turned into an AVI with a resolution of 160 X 120. But now if you re-run it as:

eyeonScript MakeAVI "c:\images.0000.jpg" "c:\example_avi.comp" 640 480

The result will be a new .avi file using the same source but at a much larger size.

Now our script is becoming more flexible, but it is still weak in a few respects. For starters - if one of the arguments is wrong - the script does no checking to see what the source of the problem may be. Our next example lesson will cover adding error checking to the arguments, assigning the arguments to more readable variables, and using if statements.


Tips for Command Line Lesson 2 (edit)

EyeonTips:Script/Tutorials/Command Line/Command Line Lesson 2