Old 09-27-2017, 03:26 PM   #1
elijahlucian
Human being with feelings
 
Join Date: Dec 2006
Posts: 53
Default Can you make your action take arguments?

I was trying to make a script do many things... which is where I may be going wrong, only problem is that all my actions use similar commands, so instead of breaking up my script, I want to just have a master script and have "myScript.py -a doTheThing" be in the action panel, then I can have "-a doOtherThing" and just have a switch pick what to do with each running of the script.
elijahlucian is offline   Reply With Quote
Old 09-27-2017, 03:33 PM   #2
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,970
Default

No but it can be simulated by making multiple entry point scripts, using temporary extstates, detecting the action's filename at runtime (get_action_context) or even prompting for user input.
cfillion is offline   Reply With Quote
Old 09-27-2017, 04:02 PM   #3
elijahlucian
Human being with feelings
 
Join Date: Dec 2006
Posts: 53
Default

hmmm what do you mean by entry point scripts?

so you would have say

doFirstThing.lua be like

reaper.Action "mainscript.py -a doTheThing"

doSecondThing.lua would be

reaper.Action "mainscript.py -a doSecondThing"
elijahlucian is offline   Reply With Quote
Old 09-27-2017, 04:15 PM   #4
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,970
Default

For example:

doFirstThing.lua
Code:
thing = 1
loadfile('mainScript.lua')
doSecondThing.lua
Code:
thing = 2
loadfile('mainScript.lua')
Python surely has a similar way of including files (from my googling it seems to vary between python 2 and 3).

Solution #2 using extstates which especially useful for signaling something to a running deferred script:
Code:
reaper.defer(function() -- using defer just to disable the automatic undo point
  reaper.SetExtState('elijahlucian_super_duper_script', 'the_thing', '1', false)
end)
And the deferred script would do something like this:
Code:
local EXT_SECTION = 'elijahlucian_super_duper_script'
local EXT_THING = 'the_thing'

function detectThing()
  if reaper.HasExtState(EXT_SECTION, EXT_THING) then
    local thing = tonumber(reaper.GetExtState(EXT_SECTION, EXT_THING))
    reaper.DeleteExtState(EXT_SECTION, EXT_THING, false);

    doTheThing(thing)
  end
end

function loop()
  -- some code here...

  detectThing()

  -- more code here...

  reaper.defer(loop)
end

loop()

Last edited by cfillion; 09-27-2017 at 04:25 PM.
cfillion is offline   Reply With Quote
Old 03-25-2024, 02:50 PM   #5
Scoox
Human being with feelings
 
Scoox's Avatar
 
Join Date: Jun 2009
Posts: 280
Default

I was just trying to write my first reascript to do something very specific but I would need to have 6 versions of the same script with essentially the same code except for a value that is different in each, which in the long run makes the code difficult to maintain. Is this still not possible?
Scoox is online now   Reply With Quote
Old 03-25-2024, 02:53 PM   #6
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 14,791
Default

You can use global memory shared between Scripts (and JSFXes).
mschnell is offline   Reply With Quote
Old 03-25-2024, 03:04 PM   #7
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,970
Default

Quote:
Originally Posted by Scoox View Post
I was just trying to write my first reascript to do something very specific but I would need to have 6 versions of the same script with essentially the same code except for a value that is different in each, which in the long run makes the code difficult to maintain. Is this still not possible?
Using dofile/loadfile, code duplication can be kept to a minimum. One script with the entire code, the others just set variables then include the main file.

To further improve maintainability by completely removing the need for extra files, ReaPack can install multiple copies then the script can read its own filename to know which copy it is.
cfillion is offline   Reply With Quote
Old 03-25-2024, 04:42 PM   #8
Scoox
Human being with feelings
 
Scoox's Avatar
 
Join Date: Jun 2009
Posts: 280
Default

Quote:
Originally Posted by cfillion View Post
Using dofile/loadfile, code duplication can be kept to a minimum. One script with the entire code, the others just set variables then include the main file.

To further improve maintainability by completely removing the need for extra files, ReaPack can install multiple copies then the script can read its own filename to know which copy it is.
Thanks for pointing me in the right direction. I think dofile will work here, but I'm getting error "Audio switcher.lua:9: bad argument #3 to 'SetMediaTrackInfo_Value' (number expected, got nil)":

Action code:

Code:
local mains_mute = 1
local cubes_mute = 1
local cube_mute = 0
local subpac_mute = 0
local hp4_mute = 1
local phones_mute = 1

-- Path relative to script file path
local info = debug.getinfo(1,'S');
script_path = info.source:match[[^@?(.*[\/])[^\/]-$]]
dofile(script_path .. "Audio switcher.lua")
Shared code:

Code:
local number = 0
local mains = reaper.GetTrack(0, number)
reaper.SetMediaTrackInfo_Value(mains, "B_MUTE", mains_mute)

number = number + 1
local cubes = reaper.GetTrack(0, number)
reaper.SetMediaTrackInfo_Value(cubes, "B_MUTE", cubes_mute)

number = number + 1
local cube = reaper.GetTrack(0, number)
reaper.SetMediaTrackInfo_Value(cube, "B_MUTE", cube_mute)

number = number + 1
local subpac = reaper.GetTrack(0, number)
reaper.SetMediaTrackInfo_Value(subpac, "B_MUTE", subpac_mute)

number = number + 1
local hp4 = reaper.GetTrack(0, number)
reaper.SetMediaTrackInfo_Value(hp4, "B_MUTE", hp4_mute)

number = number + 1
local hp4 = reaper.GetTrack(0, number)
reaper.SetMediaTrackInfo_Value(hp4, "B_MUTE", phones_mute)
When I put everything in one script (without the relative path code) it works.

The error line is:

Code:
reaper.SetMediaTrackInfo_Value(mains, "B_MUTE", mains_mute)
which means the shared code isn't getting the value of mains_mute. What am I doing wrong?

Note: I realize this code isn't optimal and probably could be streamlined using a loop construct and arrays, but it's my first lua script and I am not familiar with the language yet. Right now I just want to get something working to get me going.
Scoox is online now   Reply With Quote
Old 03-25-2024, 04:53 PM   #9
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,970
Default

Quote:
Originally Posted by Scoox View Post
What am I doing wrong?
Local variables are not shared between files. Make them global to let the included file see them.
cfillion is offline   Reply With Quote
Old 03-26-2024, 01:49 PM   #10
Scoox
Human being with feelings
 
Scoox's Avatar
 
Join Date: Jun 2009
Posts: 280
Default

Quote:
Originally Posted by cfillion View Post
Local variables are not shared between files. Make them global to let the included file see them.
Fixed and working now!
Scoox is online now   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 08:46 AM.


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