I am trying to translate a piece of code I wrote from Python to Lua. I am using this code inside of compositing package Blackmagic Fusion.
Any help would be greatly appreciated!
Python script (working):
try:
comp.ActiveTool() # checks if a tool is selected
except:
print("nothing selected")
comp.AddTool("PolylineMask", -32768, -32768) # adds a tool if nothing's selected
Lua script (still not working and erroring):
if pcall (comp:ActiveTool()) then
print "Node Selected"
else
comp:AddTool("PolylineMask", -32768, -32768)
end
Lua's exception handling works a bit differently than in other languages. Instead of wrapping code in try/catch statements, you instead run a function in a 'protected environment' with
pcall
.The general syntax for pcall is:
Where
myfunc
is the function you want to call andarg1
and so on are the arguments. Note that you aren't actually calling the function, you are just passing it so thatpcall
can call it for you.BUT keep in mind that
tbl:method(arg1, arg2)
in Lua is syntax sugar fortbl.method(tbl, arg1, arg2)
. However, since you aren't calling the function yourself, you can't use that syntax. You need to pass in the table topcall
as the first argument, like so:Thus, in your case it would be: