Page 1 of 1

Lua scripting - Using modules with Resolve Studio fuscript?

PostPosted: Wed May 23, 2018 5:30 pm
by roger.magnusson
Lua scripts started inside Resolve Studio are running fine. My scripts are located in %AppData%\Blackmagic Design\DaVinci Resolve\Fusion\Scripts\Comp.

The scripts use custom Lua modules in %AppData%\Blackmagic Design\DaVinci Resolve\Fusion\Modules since I noticed that's where Resolve looks for modules.

However, if I start a script using the command prompt instead (fuscript.exe), Resolve can't find the modules. Is there any way to register a module path or is this not working yet?

Lua scripting - Using modules with Resolve Studio fuscript

PostPosted: Thu May 24, 2018 4:18 am
by Andrew Hazelden
Hi Roger.

I use Fusion primarily on a Mac Laptop, so I will use macOS based filepaths for this reply but you can do the exact same steps on Windows by translating the general concepts.

Are you running fuscript with the path to an existing Lua script, or trying to use the fuscript interactive prompt? Try running a .lua file off disk and see if it works better for you.

You could look at manually defining the LUA_PATH and LUA_CPATH environment variables on your system in the command line session. Then fuscript should pick up the settings you want to use automatically when you launch a lua script from the command line.

On a macOS or Linux system you would use the export command in the terminal. (On Windows you would define the environment variables in the System Control Panel > Advanced System Settings > Environment Variables... window.)

Code: Select all
export LUA_CPATH="/Library/Application Support/Blackmagic Design/DaVinci Resolve/Fusion/Modules/Lua/?.so;"
export LUA_PATH="/Library/Application Support/Blackmagic Design/DaVinci Resolve/Fusion/Modules/Lua/?.lua;"


When you define a LUA_PATH environment variable each folder path entry in the list needs to end with "/?.so;" (Linux/macOS) or "/?.dll;" (Windows).

When you define a LUA_CPATH environment variable each folder path entry in the list needs to end with "/?.lua;".

The "package.path" and "package.cpath" constant values in Lua can be printed out and they will let you see what the active Lua modules path values are in your current fuscript session.

Save the following code to a .lua file and then run it in fuscript, and also run it for comparison in the Fusion Console window:

Code: Select all
print('\n\n[LUA_CPATH]')
print(package.cpath)

print('\n\n[LUA_PATH]')
print(package.path)



If you want to open a Resolve based fuscript session and connect to Resolve and the Fusion page variables you could use a command line snippet like this macOS example below. (To use this command on Windows, switch the fuscript path in the example over to use the reall Windows based fuscript path):

Code: Select all
'/Applications/DaVinci Resolve/DaVinci Resolve.app/Contents/Libraries/Fusion/fuscript' -l lua  -x 'fusion = bmd.scriptapp("Fusion", "localhost");if fusion ~= nil then fu = fusion;app = fu;composition = fu.CurrentComp;comp = composition;SetActiveComp(comp) else print("[Error] Please open up the Fusion GUI before running this tool.") end;resolve = bmd.scriptapp("Resolve", "localhost");if resolve ~= nil then res = resolve;else print("[Error] Please open up the Fusion GUI before running this tool.") end;' '/Users/andrew/Desktop/fuscript module path.lua'


Fuscript and custom Lua Module Paths in the Command Prompt.png
Fuscript and custom Lua Module Paths in the Command Prompt.png (107.79 KiB) Viewed 12376 times


Running this command on my macOS system results in the following output in the terminal:

Code: Select all
[LUA_CPATH]
/Library/Application Support/Blackmagic Design/DaVinci Resolve/Fusion/Modules/Lua/?.so;


[LUA_PATH]
/Library/Application Support/Blackmagic Design/DaVinci Resolve/Fusion/Modules/Lua/?.lua;



With no environment variable override defined for LUA_CPATH and LUA_PATH I get back the following output in the terminal:

Code: Select all
[LUA_CPATH]
./?.so;/usr/local/lib/lua/5.1/?.so;/Users/macbuild/build/remote_bmd-eng_TEi2/bmdcpt-temp/install/LuaJIT-2.1.0_2/mac///lua/5.1/?.so;/usr/local/lib/lua/5.1/loadall.so


[LUA_PATH]
./?.lua;/Users/macbuild/build/remote_bmd-eng_TEi2/bmdcpt-temp/install/LuaJIT-2.1.0_2/mac//share/luajit-2.1.0-beta3/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;/Users/macbuild/build/remote_bmd-eng_TEi2/bmdcpt-temp/install/LuaJIT-2.1.0_2/mac//share/lua/5.1/?.lua;/Users/macbuild/build/remote_bmd-eng_TEi2/bmdcpt-temp/install/LuaJIT-2.1.0_2/mac//share/lua/5.1/?/init.lua


If I drag this same lua script into the Resolve Fusion page console view I get the output shown in this screenshot:

Resolve Fusion Console.png
Resolve Fusion Console.png (125.14 KiB) Viewed 12374 times

Re: Lua scripting - Using modules with Resolve Studio fuscri

PostPosted: Thu May 24, 2018 8:37 am
by roger.magnusson
Thank you Andrew for the very detailed description. That helps my understanding and provides me with a workaround. But surely BMD should have defined the same package.path when executing inside Resolve and when executing using fuscript?

Lua scripting - Using modules with Resolve Studio fuscript

PostPosted: Thu May 24, 2018 11:25 am
by Andrew Hazelden
roger.magnusson wrote:But surely BMD should have defined the same package.path when executing inside Resolve and when executing using fuscript?


That's why we have this Resolve 15 Beta forum to discuss issues we care about with BMD. :)

If I had to guess I would say it relates to how the Fusion Standalone/Resolve global and comp specific PathMaps are dealt with from fuscript that is the issue you are experiencing.

In the command prompt, if you add the -i flag when you run fuscript it is able to be run as as standalone tool with an interactive prompt that is basically a copy of the raw Lua 5.1/LuaJIT interpreter. fuscript operates in this minimal base "LuaJIT interpreter" like mode until you tell it to connect to any host app at which time you then are given access to all of the extra features the host app is able to provide.

(This initial fuscript launching mode does not use or need your Fusion Standalone/Resolve Studio dongle to be present since you haven't connected or requested access to any functions from those host apps that would require the license.)

Code: Select all
fuscript -i

DaVinci Resolve Script Interpreter
Copyright (C) 2005 - 2018 Blackmagic Design Pty. Ltd.


Only when you connect to bmd.scriptapp() have you defined if you want to interface with Fusion Standalone or Resolve. It is possible for fuscript to bind against a local or remote instance of Fusion Standalone or Resolve Studio so it is a bit arbitrary which system's LuaModules: PathMap settings you would inherit until the connection is made, isn't it. ;)

From my basic knowledge of fuscript the bmd.scriptapp() function parameters you could use are:

Code: Select all
 bmd.scriptapp('Resolve', ip, timeout, uuid, subtype)
 bmd.scriptapp('FusionServer', ip, timeout, uuid, subtype)
 bmd.scriptapp('Fusion', ip, timeout, uuid, subtype)


Also in the past I remember reading somewhere you could also use the bmd.scriptapp() function parameters like this:

Code: Select all
 bmd.scriptapp('Generation', ip, timeout, uuid, subtype)
 bmd.scriptapp('StudioPlayer', ip, timeout, uuid, subtype)
 bmd.scriptapp(GetAppName(), '127.0.0.1', timeout, GetAppUUID())


Running bmd.scriptapp() in the console returns a pointer to the host with a value like:

Code: Select all
Lua> == bmd.scriptapp('Resolve', ip, timeout, uuid, subtype)
-- Result: Resolve (0x0x1118bcdf0) [App: 'Fusion' on 127.0.0.1, UUID: 7f4b8cf5-4bf4-4b75-a639-7816341c920d]


The Lua function GetAppName() typically returns "Fusion" when you are inside of Resolve which allows you to run the bmd.scriptapp() with that return value and then access the pointer for the fu:/fusion:/app: style of classes. You can print this value out in the Fusion page console window by typing in:

Code: Select all
dump(GetAppName())

or
Code: Select all
==GetAppName()


If you run GetAppName() from fuscript at the command prompt you will get back something like:

Code: Select all
Lua> ==GetAppName()
-- Result: FusionScript


The Lua function GetAppUUID() returns a unique resource id number that is formatted like 7f4b8cf5-4bf4-4b75-a639-7816341c920d.

You can print this value out in the Console window or command line by typing in:

Code: Select all
dump(GetAppUUID())

or
Code: Select all
== GetAppUUID()


This returns a value like:

Code: Select all
Lua> == GetAppUUID()
-- Result: 7f4b8cf5-4bf4-4b75-a639-7816341c920d


I haven't personally had to go all the way to needing to use the UUID value when accessing bmd.scriptapp() from a Lua script but my understanding is this value acts a bit like a PID (process ID) code so you could actually target a specific copy of Fusion/Resolve's GUI if you had several fuscript compatible programs launched and running in separate sessions on the same workstations.

Re: Lua scripting - Using modules with Resolve Studio fuscri

PostPosted: Thu Jun 14, 2018 6:45 am
by roger.magnusson
In Beta5, package.path has changed (on Windows) when running inside Resolve and no longer includes user directories. This means I can't put a module in the LuaModules: path map and have it automatically work.

The release notes for beta 5 mentions new APIs and documentation, but to me the scripting readme.txt looks like the beta 4 version.

Re: Lua scripting - Using modules with Resolve Studio fuscri

PostPosted: Sun Jul 08, 2018 2:18 pm
by roger.magnusson
In Beta 6, package.path once again includes user directories on Windows, thanks.

As with Beta 5, the release notes claims there's been updates to the scripting documentation but it's unchanged as far as I can tell. Is there some miscommunication with regards to what is being publicly released or am I looking in the wrong place?

Re: Lua scripting - Using modules with Resolve Studio fuscri

PostPosted: Tue Sep 04, 2018 12:49 am
by MLanghausen
***Update***

I tried on my Mac Laptop - OS Sierra - and get the same behavior:
The other testing was done on Windows 10 OS

Tried removing the following line as well and still doesn't accept the new 'Resolution" settings

proj:SetSetting("timelineOutputResMismatchUseCustomPreset", tostring(0))
proj:SetSetting("timelineOutputResMismatchCustomPreset", "None")

***EndUpdate***

Hello!
New to the forum/resolve/scripting.
I've spent the last 3 days diving into How to Script with Lua and feel like I'm starting to get the hang of it. I'm working on a script that eventually I will connect into the GUI and except data input from user.
As of now, I'm just testing out different lines of code and seeing how Resolve reacts.

Changes to System Settings:
The follow line of code works every time.

Code: Select all

local resolve = Resolve()
local pm = resolve:GetProjectManager()
local proj = pm:GetCurrentProject()

proj:SetSetting('timelineResolutionHeight', "720")
proj:SetSetting('timelineResolutionWidth', "2000")
proj:SetSetting("timelineOutputResMismatchBehavior", "stretch")



However, with the following line of code, executed through Resolves Console, I am unable to change the "Resolution" or the "Custom" drop down. Everything else seems to be working as intended.

Maybe the issue is that the following line, no matter how many times I change it and dump the table, it always stays at "None" ????

timelineOutputResMismatchCustomPreset = None

Code: Select all
local resolve = Resolve()
local pm = resolve:GetProjectManager()
local proj = pm:GetCurrentProject()


proj:SetSetting("timelineOutputResMatchTimelineRes", tostring(0))
proj:SetSetting("timelineOutputResMismatchUseCustomPreset", tostring(0))
proj:SetSetting("timelineOutputResMismatchCustomPreset", "None")
proj:SetSetting("timelineOutputResolutionWidth", "720")
proj:SetSetting("timelineOutputResolutionHeight", "2000")
proj:SetSetting("timelineOutputPixelAspectRatio", "square")
proj:SetSetting("timelineOutputResMismatchBehavior", "scaleToCrop")