Eyeon:Script/Tutorials/Slate Manipulation/Applying Settings
From VFXPedia
Lesson 3.2 : Applying Settings to a Slate
Now that all the information required to create a slate has been input by the user and returned in the variable "ret", that information can be used to manipulate the fields in the slate. First, however, we have to open it.
The LoadComp() function will load a composition and return a composition object for it. Before we launch the slate, we should make a variable to hold the previous composition object just in case there's something else we need from it.
oldcomposition = composition
At this point the slate comp can be opened. The version saved for the purposes of this lesson was stored on the C drive. If you've saved it elsewhere, you can alter your code accordingly.
composition = fusion:LoadComp("c:\\slate.comp")
The LoadComp() function returns a composition object equal to that of the comp opened by the function. However, the composition affected by script is still the one where the original script was run. In order to force eyeonScript to affect the newly loaded composition, the SetActiveComp() function can be employed:
SetActiveComp(composition)
At any point, the currently active composition for scripting purposes can be changed with the SetActiveComp() function.
slatecomposition = composition composition = SetActiveComp(oldcomposition) composition = SetActiveComp(slatecomposition)
Because we set the AskUser() dialog to get the location of the composition to open, the appropriate code would look like this:
oldcomposition = composition composition = fusion:LoadComp(ret["Slate Location"])
The next step is to start affecting the tools. One of the things that will make slate generation an easy process is properly naming every tool that will be affected by script at creation. As such, all tools have been named in a way that will make editing them through script much easier than if they were brought in with their default names.
Go into one of the text tools and look at the name of the field the text will be input. A Text+ tool's text input field is called its "StyledText" input. As such, in order to manipulate the "StyledText" of, say, the Date tool, we would need to do the following:
Date.StyledText = ret["Date"]
Although we probably want some indicator that the field is the date field.
Date.StyledText = "DATE: "..ret["Date"]
From this juncture, we can go on and define all of the composition's text fields.
Project.StyledText = "PROJECT: "..ret["Project"] ShotName.StyledText = "SHOT NAME: "..ret["Shot Name"] FrameRange.StyledText = "FRAME RANGE: "..ret["Frame RAnge"] Artist.StyledText = "ARTIST: "..ret["Artist"] Comments.StyledText = "COMMENTS: "..ret["Comments"]
Finally, the saver:
SlateSaver.Clip = ret["Output"]
To set the height and width, we could enter each tool's name, but there might be a faster way. As an alternative, the script could search through all tools in a composition and check for which ones are Background or TextPlus tools. For that we can use the tool's GetID() function.
toollist = composition:GetToolList() for i, tool in toollist do if tool:GetID() == "Background" or tool:GetID() == "TextPlus" then tool.Height = ret["Height"] tool.Width = ret["Width"] end end
The last step is to set the render range for the slate. Usually a slate will be the first frame of a sequence, so that can be defined by grabbing the render start time of the previous composition and subtracting one.
renderStart = oldcomposition:GetAttrs().COMPN_RenderStart - 1
However, the composition might be set to start its render range at 0. Fusion can't render negative frame ranges for sequences. As such the best thing to do in that case would be to stick the frame at the end of the sequence.
if oldcomposition:GetAttrs().COMPN_RenderStart == 0 then renderStart = oldcomposition:GetAttrs().COMPN_RenderEnd +1 else renderStart = oldcomposition:GetAttrs().COMPN_RenderStart - 1 end
To now apply the changes to the composition's attributes, the SetAttrs() function is employed. This allows us to pass a table into an object's attributes with the following syntax:
composition:SetAttrs({COMPN_RenderStart = renderStart, COMPN_RenderEnd = renderStart})
In the above case, the only attributes that were affected by the SetAttrs function were its render start and end times. Note that some attributes are read only and as such cannot be altered by SetAttrs.
We'll leave it up to the user to hit the render button in case there's anything wrong with what was input (although you can easily do this through script - check out composition:Render()). The final code would look like this:
function GetSavers(composition) local toollist = composition:GetToolList() saverstoReturn = {} for i, tool in toollist do if tool:GetID() == "Saver" then table.insert(saverstoReturn, tool) end end return saverstoReturn end function stripExtension(stringVal) startScan = string.len(stringVal) -- for the length of the string to one, stepping at a -1 interval for i = startScan, 1, -1 do -- if the character being checked was a period if string.sub(stringVal,i,i) == "." then -- take all characters before the character just checked toreturn = string.sub(stringVal,1,i-1) -- return it return toreturn end end end shotnameDefault = stripExtension(composition:GetAttrs().COMPS_Name) usernameDefault=fusion:GetEnv("USERNAME") if os.date("%b") == "May" then dateDefault = os.date("%b %d, %Y.") else dateDefault = os.date("%b. %d, %Y.") end framerangeDefault = composition:GetAttrs().COMPN_RenderStart.." - "..composition:GetAttrs().COMPN_RenderEnd -- Get a list of all the savers. saverlist = GetSavers(composition) -- Check to see if there are more than one. if saverlist then if table.getn(saverlist) == 1 then saverNames = {} -- Insert all of the saver names into a table that can be read by the user. for i, tool in saverlist do saverNames[i] = tool:GetAttrs().TOOLS_Name end -- Ask the user for their saver preference. ret = composition:AskUser("Choose Your Saver Output", {{"Saver", "Dropdown", Options = saverNames}}) if ret == nil then print("Cancelled!") return end -- The saver in the list table will be equal to the returned index + 1 saverDefault = saverlist[ret["Saver"]+1] else if table.getn(saverlist) == 1 then saverDefault = saverlist[1] else -- setup the Saver list's Clip for situations without savers saverDefault = {} saverDefault.Clip = {} saverDefault.Clip[TIME_UNDEFINED] = "" end end end -- Get input from the user ret = AskUser("Slate Options", {{"Slate Location", "FileBrowse", Default = "c:\\Slate.comp", Save = false}, {"Project Name", "Text", Default = "Example Project", Lines = 1}, {"Shot Name", "Text", Default = shotnameDefault, Lines = 1 }, {"Date", "Text", Default = dateDefault, Lines = 1}, {"Artist", "Text", Default = usernameDefault, Lines =1}, {"Frame Range", "Text", Default = framerangeDefault, Lines =1}, {"Output", "FileBrowse", Default = saverDefault.Clip[TIME_UNDEFINED], Save = true}, {"Additional Comments", "Text", Default = "", Lines = 2}, {"Width", "Slider", Min = 0, Max = 6000, Default = 2048, DisplayedPrecision = 0}, {"Height", "Slider", Min = 0, Max = 6000, Default =1556, DisplayedPrecision = 0} }) -- Store the current composition object in a variable for lateruse. oldcomposition = composition -- use fusion:LoadComp to load the slate in question composition = fusion:LoadComp(ret["Slate Location"]) SetActiveComp(composition) -- Set all the parameters Date.StyledText = "DATE: "..ret["Date"] Project.StyledText = "PROJECT: "..ret["Project Name"] ShotName.StyledText = "SHOT NAME: "..ret["Shot Name"] FrameRange.StyledText = "FRAME RANGE: "..ret["Frame Range"] Artist.StyledText = "ARTIST: "..ret["Artist"] Comments.StyledText = "COMMENTS: "..ret["Additional Comments"] SlateSaver.Clip = ret["Output"] -- Get a toollist to set the height / width toollist = composition:GetToolList() -- Set the height and width of all the tools in the comp. for i, tool in toollist do if tool:GetID == "Background" or tool:GetID() == "TextPlus" then tool.Height = ret["Height"] tool.Width = ret["Width"] end end -- Set the Render range for the comp using the old compositions Render End and Start times if oldcomposition:GetAttrs().COMPN_RenderStart == 0 then renderstart = oldcomposition:GetAttrs().COMPN_RenderEnd +1 else renderstart = oldcomposition:GetAttrs().COMPN_RenderStart - 1 end composition:SetAttrs({COMPN_RenderStart = renderstart, COMPN_RenderEnd = renderstart}
Tips for Applying Settings (edit)
EyeonTips:Script/Tutorials/Slate Manipulation/Applying Settings
