Old 06-03-2015, 10:28 PM   #1
tomfrankly
Human being with feelings
 
Join Date: Oct 2012
Posts: 59
Default passing variables between scripts?

Hi, I'm new to ReaScript. I'm trying to make something involving storing the selected media item's source file, which will then later be recalled and inserted into the project. I want to store the source with one hotkey and then recall/paste it with another. Presumably, I'd need two separate scripts to make that work. How can I pass information between multiple scripts?
tomfrankly is offline   Reply With Quote
Old 06-03-2015, 10:38 PM   #2
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,900
Default

Hi !
You could use project Exstate. It is a way to store infos (with a type, key + value) right in the .rpp file. This way, infos can be called within different scriots, and even if you close / reopen your project !
The is of course a Get and a Set function for these, check in the API doc how to use them
X-Raym is offline   Reply With Quote
Old 06-03-2015, 11:00 PM   #3
tomfrankly
Human being with feelings
 
Join Date: Oct 2012
Posts: 59
Default

That looks promising! Thanks for the quick response.
tomfrankly is offline   Reply With Quote
Old 06-04-2015, 05:08 AM   #4
heda
Human being with feelings
 
heda's Avatar
 
Join Date: Jun 2012
Location: Spain
Posts: 7,268
Default

After pasting, you can recall SetProjExtState with a empty string as value, to clean the data so it is not saved in the project.

or use just SetExtState with false as persist parameter just for temporary storage


items or tracks are custom type variable. Not just a string or a number, I don't know how that would work. I would have to investigate this.
heda is offline   Reply With Quote
Old 06-04-2015, 06:00 AM   #5
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,900
Default

@heda

Maybe write the GUID as string ?
X-Raym is offline   Reply With Quote
Old 06-04-2015, 06:07 AM   #6
heda
Human being with feelings
 
heda's Avatar
 
Join Date: Jun 2012
Location: Spain
Posts: 7,268
Default

SetExtState works with the takes

spk77 was using SetExtState in a script to store selected takes.
see here the example
http://forum.cockos.com/showpost.php...08&postcount=4
heda is offline   Reply With Quote
Old 06-04-2015, 07:48 AM   #7
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,668
Default

(Note: This works in REAPER 5pre only)

Maybe storing the take source filename would be the best way?


The first part (storing) could be something like this:

Store take source filename.lua
PHP Code:
function msg(m)
  
reaper.ShowConsoleMsg(tostring(m).."\n")
end

function store_take_source_filename()
  
local item reaper.GetSelectedMediaItem(0,0) -- get first selected item
  
-- No selected items
  
if item == nil then
    msg
("No item selected")
  return 
end
  
  
-- Empty item (no takes in item)
  
local take reaper.GetActiveTake(item)
  if 
take == nil then
    msg
("Empty item?")
  return 
end
  
  
-- Active take in selected item is a MIDI take
  
if reaper.TakeIsMIDI(takethen
    msg
("Please select an audio take")
  return 
end


  local take_source 
reaper.GetMediaItemTake_Source(take) -- get media source
  local take_source_filename 
reaper.GetMediaSourceFileName(take_source"")
  if 
take_source_filename ~= "" then
    reaper
.SetExtState("Take source filenames""file name1"take_source_filenametrue)
  
end
end

store_take_source_filename
() 






This function could be useful for creating new takes from the stored filename:
PHP Code:
Luaboolean reaper.BR_SetTakeSourceFromFile(MediaItem_Take takestring filenameInboolean inProjectData
spk77 is offline   Reply With Quote
Old 06-04-2015, 08:43 AM   #8
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,668
Default

I saw your other thread:
Hotkeys to insert/stamp media? (press F1 to insert kick, F2 for snare, etc.)
http://forum.cockos.com/showthread.p...88#post1527288


The storing function would need some more information to be stored: source take position in item, source item length etc.

I'll take a look at it
spk77 is offline   Reply With Quote
Old 06-05-2015, 12:51 AM   #9
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,668
Default

Ok, now it almost works.

Throw items aimlessly around arrange view in sadness and hate.lua




("Create media item from stored take source file name.lua" might be more descriptive name )
spk77 is offline   Reply With Quote
Old 06-05-2015, 02:42 AM   #10
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,900
Default

That name ^^

The action looks very nice to me good work !
X-Raym is offline   Reply With Quote
Old 06-05-2015, 05:10 AM   #11
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,668
Default

Quote:
Originally Posted by X-Raym View Post
That name ^^

The action looks very nice to me good work !
It's a script for emo kids



There's a possible bug in GetMediaSourceFileName:
  • If section or reverse is selected, "GetMediaSourceFileName" returns an empty string.
  • Code:
    Lua: string filenamebuf reaper.GetMediaSourceFileName(PCM_source source, string filenamebuf)


(take media source properties)
spk77 is offline   Reply With Quote
Old 06-10-2015, 09:00 AM   #12
Alkamist
Human being with feelings
 
Join Date: Dec 2011
Posts: 506
Default

I'm very interested in being able to copy an item's source with a hotkey and replace another item's source with it with another hotkey. This would really simplify my workflow.

I wrote a script that works with the copy script previously posted in this thread.

Code:
function msg(m)
  reaper.ShowConsoleMsg(tostring(m).."\n")
end

function replace_take_source_file()
  reaper.Undo_BeginBlock() -- Begining of the undo block. Leave it at the top of your main function.
  stored_source_filename = reaper.GetExtState("Take source filenames", "file name1")
  stored_source = reaper.PCM_Source_CreateFromFile(stored_source_filename)

  selected_items_count = reaper.CountSelectedMediaItems(0)
  for i = 0, selected_items_count - 1 do
    item = reaper.GetSelectedMediaItem(0, i) -- Get selected item i
    take = reaper.GetActiveTake(item) -- Get the active take
    
    reaper.SetMediaItemTake_Source(take, stored_source) -- Set the take source to the stored source   
  end
  reaper.Undo_EndBlock("Replace selected item's source with copied item source", 0) -- End of the undo block. Leave it at the bottom of your main function.
end

replace_take_source_file()
This works, but it has problems.
Firstly, the graphics of an item are very lazy to reflect the change of a source file.
The biggest problem though, is that if you try to replace the source of multiple items, Reaper will crash if you try to delete any of them afterward. It says something about a pure virtual function.

Is this problem on my end or is it a problem with the API?
Alkamist is offline   Reply With Quote
Old 06-10-2015, 10:22 AM   #13
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,668
Default

@Alkamist
I can't remember why we can't use PCM_Source_CreateFromFile, but fortunately Breeder made reaper.BR_SetTakeSourceFromFile function:


1.Store source file name (now it doesn't create an undo point)
PHP Code:
function msg(m
  
reaper.ShowConsoleMsg(tostring(m).."\n"
end 

function store_take_source_filename() 
  
local item reaper.GetSelectedMediaItem(0,0) -- get first selected item 
  
-- No selected items 
  
if item == nil then 
    msg
("No item selected"
  return 
end 
   
  
-- Empty item (no takes in item
  
local take reaper.GetActiveTake(item
  if 
take == nil then 
    msg
("Empty item?"
  return 
end 
   
  
-- Active take in selected item is a MIDI take 
  
if reaper.TakeIsMIDI(takethen 
    msg
("Please select an audio take"
  return 
end 


  local take_source 
reaper.GetMediaItemTake_Source(take) -- get media source 
  local take_source_filename 
reaper.GetMediaSourceFileName(take_source""
  if 
take_source_filename ~= "" then 
    reaper
.SetExtState("Take source filenames""file name1"take_source_filenametrue
  
end 
end 

store_take_source_filename
() 

(This is a simple version - item/take properties are not "copied" etc.)
2. Replace take sources (selected items, active take)
PHP Code:
function msg(m)
  
reaper.ShowConsoleMsg(tostring(m).."\n")
end

function replace_take_sources()
  if 
not reaper.HasExtState("Take source filenames""file name1"then
    
return
  
end
  
  local file_name 
reaper.GetExtState("Take source filenames""file name1")
  if 
file_name == "" then
    
return
  
end
  reaper
.Undo_BeginBlock()
  
local get_act_take reaper.GetActiveTake
  local get_sel_item 
reaper.GetSelectedMediaItem
  local sel_item_count 
reaper.CountSelectedMediaItems(0)
  
  for 
i=1sel_item_count do
    
local take get_act_take(get_sel_item(0i-1))
    
local source_added_ok reaper.BR_SetTakeSourceFromFile(takefile_namefalse)
    --
msg("Source added: " .. tostring(source_added_ok)) -- debug
  end
  reaper
.UpdateArrange()
  
reaper.Main_OnCommand(404410) -- rebuild peaks for selected items
  reaper
.Main_OnCommand(406120) -- Set items length to source length (comment out if not needed)
  
reaper.Undo_EndBlock("Replace take source(s)", -1)
end

replace_take_sources
() 

Last edited by spk77; 06-10-2015 at 10:27 AM. Reason: variables to "local"
spk77 is offline   Reply With Quote
Old 06-10-2015, 10:54 AM   #14
Alkamist
Human being with feelings
 
Join Date: Dec 2011
Posts: 506
Default

@spk77 Thank you for the help! For some reason though, it's telling me that reaper.BR_SetTakeSourceFromFile is unknown. I have the latest SWS extension, and when I tell Reaper to show the ReaScript documentation, that function is there. Any idea what might be going wrong?
Alkamist is offline   Reply With Quote
Old 06-10-2015, 11:03 AM   #15
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,668
Default

Make sure that you have this version of the SWS extensions:
v2.7.1 #0 pre-release build (May 7, 2015)
http://forum.cockos.com/showthread.php?t=153702



("reaper.BR_SetTakeSourceFromFile" and some other sws-reascript functions have been in the API help docs for a long time, but they haven't been working from EEL or Lua. They should work if you download the latest pre-release build of the SWS)
spk77 is offline   Reply With Quote
Old 06-10-2015, 11:32 AM   #16
Alkamist
Human being with feelings
 
Join Date: Dec 2011
Posts: 506
Default

Quote:
Originally Posted by spk77 View Post
Make sure that you have this version of the SWS extensions:
v2.7.1 #0 pre-release build (May 7, 2015)
http://forum.cockos.com/showthread.php?t=153702



("reaper.BR_SetTakeSourceFromFile" and some other sws-reascript functions have been in the API help docs for a long time, but they haven't been working from EEL or Lua. They should work if you download the latest pre-release build of the SWS)
It works now. Thank you, you are awesome!

Now I just need to figure out how to get it to keep all of the settings like start offset and item edges. That shouldn't be too hard though.
Alkamist is offline   Reply With Quote
Old 06-10-2015, 11:51 AM   #17
Alkamist
Human being with feelings
 
Join Date: Dec 2011
Posts: 506
Default

I just realized it already does that. I can just comment out the reaper.Main_OnCommand(40612, 0) line to get it to do what I want. Thanks again! This will really speed up my workflow!
Alkamist is offline   Reply With Quote
Old 06-10-2015, 12:13 PM   #18
spk77
Human being with feelings
 
Join Date: Aug 2012
Location: Finland
Posts: 2,668
Default

Glad it worked!

I'm trying to make a more complex version which will store:
  • Source item properties
  • Source take properties
  • Take media source properties + source filename

Then it would be possible to f.ex create media items with the stored properties, even if the source item/take is deleted from the project. (This is closely related to the script request by tomfrankly.)

Currently these are stored in "reaper-extstate.ini":
PHP Code:
[item1_take1]
take_D_STARTOFFS=4.0
take_D_VOL
=1.0
take_D_PAN
=0.0
take_D_PANLAW
=-1.0
take_D_PLAYRATE
=1.0
take_D_PITCH
=0.0
take_B_PPITCH
=1.0
take_I_CHANMODE
=0.0
take_I_PITCHMODE
=-1.0
take_I_CUSTOMCOLOR
=0.0
item_B_MUTE
=0.0
item_B_LOOPSRC
=1.0
item_B_ALLTAKESPLAY
=0.0
item_B_UISEL
=1.0
item_C_BEATATTACHMODE
=-1.0
item_C_LOCK
=0.0
item_D_VOL
=1.0
item_D_POSITION
=76.0
item_D_LENGTH
=42.814557823129
item_D_SNAPOFFSET
=0.0
item_D_FADEINLEN
=0.0
item_D_FADEOUTLEN
=0.0
item_D_FADEINLEN_AUTO
=0.0
item_D_FADEOUTLEN_AUTO
=0.0
item_C_FADEINSHAPE
=1.0
item_C_FADEOUTSHAPE
=1.0
item_I_GROUPID
=0.0
item_I_CUSTOMCOLOR
=0.0
item_I_CURTAKE
=0.0
sourcefilename
=C:\Users\Pavillion 11\Desktop\Muistikortilta\MULTIBOOT\REAPER\BIISIT\allstemma3\07-kitara soolo-06 render 001.wav 
spk77 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 11:45 PM.


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