Eyeon:Script/Tutorials/Get Loader Filenames/AskUser Dialog
From VFXPedia
Lesson 2.6 : Ask User Dialogs
In the last lesson we improved on the code that was used to export the text file. What now needs to be done is integrate some input from the user. Adding the user interface at the end of the script is sometimes useful, because it can make script testing go a little quicker if the interface gets complex, but doing it at the start is equally correct.
In order to get input from the user, the AskUser function is called from the composition. The AskUser function is more complex than what's been covered so far, so taking a look at the AskUser documentation and the syntax right now might be worth the extra time.
In this case the user doesn't need to insert a whole lot of information. They should be asked where they want the text file to be output to, the file's name (which will be default based on that of the composition) and (if the file exists) if they want the data appended to the existing data or if they want their previous data overwritten. Looking at the documentation for AskUser, you can see how the syntax of the function is organized:
- AskUser(name, { table of inputs } )
Each input is a table structured as follows :
{Input Name, Input Type, Options ...} Input Name (string, required)
This name is the index value for the controls value as set by the user (i.e. dialog.Control or dialog["Control Name"]). It is also the label shown next to the control in the dialog, unless the Name option is also provided for the control.
Input Type (string, required)
A string value describing the type of control to display. Valid strings are FileBrowse, PathBrowse, Position, Slider, Screw, Checkbox, Dropdown, and Text. Each Input type has its own properties and optional values.
Options (misc)
Different control types accept different options that determine how that control appears and behaves in the dialog.
In order to call the function, it needs to have a name (which becomes the returned table's index value). As described under input name, this text is put into the corner of the AskUser dialog as well as next to the controls in the AskUser box. As such it needs to be passed on as a string and is a required variable (as is indicated in the brackets next to Input Name). First off, let's just pop an AskUser box up and dump its returned value.
==composition:AskUser("This is a Test", {})
If the user presses the OK button, a table value will be returned. If Cancel is pressed, the function return nil. This is useful when differentiating between the two states.
For example:
ret=composition:AskUser("This is a Test", {}) if ret then print("Success!") else print("failure") end
Above, no data was passed into the table of inputs. If you scroll down in the AskUser reference, you can see that there are a number of different control types available. We need a path browser, a user editable text box (for the file's name), and a Checkbox.
Let's set up that table.
ret = AskUser("Choose Export Options.",{ {"Path", "PathBrowse", Save = true}, {"Filename", "Text", Lines = 1, Default = "text.txt"}, {"Append data?", "Checkbox", Default = 1} }) dump(ret)
Try this code out. As you can see in the console, AskUser returns a table with all the information that was input. The filename defaulted to "text.txt", as we designated in the text field, and the Append Data field defaulted to 1. This code is perfectly valid, but what might be easier to do is simply use a file browser for designating the file's output location so that we don't have to concatenate (assemble) the values later.
ret = AskUser("Choose Export Options.",{ {"pathFilename", "FileBrowse", Save = true}, {"Append data?", "Checkbox", Default = 1} })
This will be much simpler for our uses later on. The next lesson will show us how to set up default values for the filebrowser.
Tips for AskUser Dialog (edit)
EyeonTips:Script/Tutorials/Get Loader Filenames/AskUser Dialog
