Old 08-07-2020, 11:38 PM   #1
Colox
Human being with feelings
 
Join Date: Feb 2012
Location: Sweden
Posts: 1,206
Default Lua: set default render path, difficulties.

I’m trying to set the default render path, for each individual project. I’m using a Lua script with API calls to achieve this, but I’m having problems making it work.

I begin by calling up a ‘browse for folder’ dialogue box, to get the path address to desired target folder and store that path in a String variable:

Code:
local numberreturn, pathreturn = reaper.JS_Dialog_BrowseForFolder("Caption", "C:/")
The path address gets returned into “pathreturn” string. I then use a function to insert this address into Reaper’s RENDER_FILE parameter:

Code:
function InsertPath()
reaper.GetSetProjectInfo_String(0, 'RENDER_FILE', pathreturn, true)
end
The address path does get stored into the "pathreturn" variable. I know this for certain. And I get no error message from the function either.

But when I open the “Render Project to disk ..” dialogue (pic below), and look at the “Directory” text field, it is empty. There is no path to the standard folder for this project inserted.



But there is another script like this, which does roughly the same thing. It’s written by renown Reaper user MPL (thanks, man). His script is made to set the default render path to the Windows desktop address (C:\Users\Username\Desktop).

When I run his script, it works. When I open the Render Project do disk, and look at the Directory field, it says “C:\Users\Username\Desktop” there. This is what I wanted to achieve for myself.



MPL’s script looks like this:

Code:
function GetDesktopPath()
local desktopPath = os.getenv("appdata")
local appDataStart = string.find( desktopPath, "AppData" )
if( appDataStart ) then
desktopPath = string.sub( desktopPath, 1, appDataStart-1 )
desktopPath = desktopPath .. "Desktop\"
end
return true, desktopPath
end

function main()
local ret, desktopPath = GetDesktopPath()
if ret and GetOS():match('Win') then
reaper.GetSetProjectInfo_String(0, 'RENDER_FILE', desktopPath, true) 
end
end
What makes MPL’s script successful in inserting the default render path, and my script unsuccessful in achieving this? I have much to learn about advanced Lua, but I can follow code reasonably well I think. Yet, I'm having problem to decode the magic factor here.

Looking at MPL’s code, it seems to me that MPL’s GetDesktopPath function is basically a search function which searches out the exact address to the desktop. And his main function is quite similar to mine, except for details like MPL employs a ‘Windows only’ condition test.

I am having problems seeing how I can learn from MPL’s good script, to make my own implementation as successful as his is. I’d love if someone could lend me a hand and walk me through that.

thanx
/C.
__________________
There are only two kinds of people in the world: those who entertain, and those who are absurd.
- Britney Spears
Colox is offline   Reply With Quote
Old 08-07-2020, 11:59 PM   #2
solger
Human being with feelings
 
solger's Avatar
 
Join Date: Mar 2013
Posts: 5,844
Default

I haven't tried the code, but just a guess:

In your example, the pathreturn variable is declared as local variable. So the scope of this variable is limited to a certain code block and probably doesn't reach inside the InsertPath function.
More info here:
Depending on how the rest of your code looks, one option is to pass the (local) variable as parameter to the InsertPath function.
Code:
local numberreturn, pathreturn = reaper.JS_Dialog_BrowseForFolder("Caption", "C:/")
if numberreturn == 1 then 
    InsertPath(pathreturn)
end

function InsertPath(pathreturn)
    reaper.GetSetProjectInfo_String(0, 'RENDER_FILE', pathreturn, true)
end
Or to call both lines inside the same function:
Code:
function OpenBrowseDialog()
    local numberreturn, pathreturn = reaper.JS_Dialog_BrowseForFolder("Caption", "C:/")
    if numberreturn == 1 then 
       reaper.GetSetProjectInfo_String(0, 'RENDER_FILE', pathreturn, true)
    end
...

Another option would be to make the pathreturn variable global (by removing the 'local' modifier):
Code:
numberreturn, pathreturn = reaper.JS_Dialog_BrowseForFolder("Caption", "C:/")

...

function InsertPath()
    reaper.GetSetProjectInfo_String(0, 'RENDER_FILE', pathreturn, true)
end
--------------------

Quote:
Originally Posted by Colox View Post
MPL’s script looks like this:

Code:
function GetDesktopPath()
   local desktopPath = os.getenv("appdata")
   local appDataStart = string.find( desktopPath, "AppData" )
   if( appDataStart ) then
       desktopPath = string.sub( desktopPath, 1, appDataStart-1 )
       desktopPath = desktopPath .. "Desktop\"
   end
   return true, desktopPath
end

function main()
    local ret, desktopPath = GetDesktopPath()
    if ret and GetOS():match('Win') then
        reaper.GetSetProjectInfo_String(0, 'RENDER_FILE', desktopPath, true) 
    end
end
In MPL’s script, the local desktopPath variable in the main function (used in reaper.GetSetProjectInfo_String) is the return value from the GetDesktopPath() function.
Code:
local ret, desktopPath = GetDesktopPath()
...
   reaper.GetSetProjectInfo_String(0, 'RENDER_FILE', desktopPath, true) 
...


The scope of the two local desktopPath variables (in the GetDesktopPath and main function) is perhaps easier to see when using different variable names for them:
Code:
function GetDesktopPath()
   local path = os.getenv("appdata")
   local appDataStart = string.find( path , "AppData" )
   if( appDataStart ) then
       path = string.sub( path , 1, appDataStart-1 )
       path = path .. "Desktop"
   end
   return true, path
end

function main()
    local ret, desktopPath = GetDesktopPath()
    if ret and GetOS():match('Win') then
        reaper.GetSetProjectInfo_String(0, 'RENDER_FILE', desktopPath, true) 
    end
end
The scope of the local path variable is limited to the GetDesktopPath function (where the local variable was declared). And the scope of the local desktopPath variable is limited to the main function.

I hope this info is helpful
__________________
ReaLauncher

Last edited by solger; 08-08-2020 at 01:39 AM. Reason: merged 2 posts into 1
solger is offline   Reply With Quote
Old 02-20-2023, 04:07 AM   #3
haarbol
Human being with feelings
 
Join Date: Jun 2022
Posts: 27
Default

Probably too late to help you, but for others searching this topic:

In order for this value to be stored in the ExtData, you need to save your session afterwards. Once you do that, you will see that the string in the rpp file now reflects your change.
haarbol is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 02:56 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.