Old 09-15-2020, 05:34 AM   #1
Stroudy
Human being with feelings
 
Stroudy's Avatar
 
Join Date: Jul 2014
Location: London
Posts: 471
Default Functions not running in order - script help?

Hi all,

I'm just trying to run some actions in order. the actions are made up of native actions, scripts and SWS commands.

However, the results of the 3rd function [AutoSaveEachTrack()] are coming in before the 2nd [SelectRegionProtectedTracks()]. This results in the SWS command giving an error message.

I guess I need to get the 3rd function [AutoSaveEachTrack()] to wait for the 2nd [SelectRegionProtectedTracks()] to complete?

How does one do this?

Code:
local act1 = "_S&M_SAVE_TRTEMPLATE_SLOT2" --SWS/S&M: Resources - Auto-save track template (with items, envelopes)
local act2 = "40297" -- Track: Unselect all tracks
local act3 = "_RS0add2f1eba667a29a38690b137a9f957baa42e35" -- Script: HeDa_Track Inspector VIP_Tags Select RPTs.lua Region protcted tracks

--This script will save tracks tagged with "RPTs" (using HeDa's VIP track inspector)
--as Track Templates. The template will be placed in the SWS:resources: Track Templates:Last slot
--The accompanying script ["(post-region edit) restore selected tracks"] will restore the tracks to
-- a pre-region edit state and will then delete the last slot in the SWS:resources: Track Templates

local function AutoSaveEachTrack()
  if string.sub(act1, 1, 1) == "_" then
  act1 = reaper.NamedCommandLookup(act1)
  end
  
  act1 = tonumber(act1)
  if act1 and act1 > 0 then
  reaper.Main_OnCommand(act1, 0)
  end
end

local function UnselectTracks()
  if string.sub(act2, 1, 1) == "_" then
  act2 = reaper.NamedCommandLookup(act2)
  end
  
  act2 = tonumber(act2)
  if act2 and act2 > 0 then
  reaper.Main_OnCommand(act2, 0)
  end
end

local function SelectRegionProtectedTracks() --video tracks, dialogue and sound design tracks
  if string.sub(act3, 1, 1) == "_" then
  act3 = reaper.NamedCommandLookup(act3)
  end
  
  act3 = tonumber(act3)
  if act3 and act3 > 0 then
  reaper.Main_OnCommand(act3, 0)
  end
end





----------------------------------------------------------------------
----------------------------------------------------------------------
----------------------------------------------------------------------

reaper.Undo_BeginBlock()
reaper.PreventUIRefresh(1)
UnselectTracks() --act2
SelectRegionProtectedTracks() --act3
AutoSaveEachTrack() --act1

reaper.PreventUIRefresh(-1)
reaper.UpdateArrange()
Stroudy is offline   Reply With Quote
Old 09-15-2020, 06:04 AM   #2
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: Apr 2020
Location: Leipzig
Posts: 1,567
Default

Use
https://mespotin.uber.space/Ultrasch...tTrackSelected

to unselect selected tracks instead and see, if that changes the behavior of the script.

Actions aren't run as function. You can run action two even if action one is not finished yet, as the script will not wait for its finished execution.
If you solve it in code, you get rid of that problem.
__________________
Use you/she/her, when contacting me, please. Thanks :) Not mentoring via PMs, sorry.
Ultraschall-API - 1294 ReaScript functions for Reaper - Reaper Internals - Developerdocs4Reaper - Donate, if you wish
Meo-Ada Mespotine is online now   Reply With Quote
Old 09-15-2020, 07:32 AM   #3
Stroudy
Human being with feelings
 
Stroudy's Avatar
 
Join Date: Jul 2014
Location: London
Posts: 471
Default

Quote:
Originally Posted by Meo-Ada Mespotine View Post
Use
https://mespotin.uber.space/Ultrasch...tTrackSelected

to unselect selected tracks instead and see, if that changes the behavior of the script.

Actions aren't run as function. You can run action two even if action one is not finished yet, as the script will not wait for its finished execution.
If you solve it in code, you get rid of that problem.
Thanks, I'm not sure how to use that function to set the track selection to nothing though. How do I do this?
Stroudy is offline   Reply With Quote
Old 09-15-2020, 08:21 AM   #4
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: Apr 2020
Location: Leipzig
Posts: 1,567
Default

The following code allows you to set all tracks selected/unselected.
Just set are_tracks_selected=false for unselecting all tracks, and set are_tracks_selected=true for selecting all tracks.


Code:
are_tracks_selected=false

for i=0, reaper.CountTracks(0)-1 do
  reaper.SetTrackSelected(reaper.GetTrack(0,i), are_tracks_selected)
end
__________________
Use you/she/her, when contacting me, please. Thanks :) Not mentoring via PMs, sorry.
Ultraschall-API - 1294 ReaScript functions for Reaper - Reaper Internals - Developerdocs4Reaper - Donate, if you wish
Meo-Ada Mespotine is online now   Reply With Quote
Old 09-15-2020, 09:29 AM   #5
Stroudy
Human being with feelings
 
Stroudy's Avatar
 
Join Date: Jul 2014
Location: London
Posts: 471
Default

Thanks. I get the same problem... the SWS command runs before the Heda function

How can I get the SWS command to wait for the HeDa script to complete?

thanks
Stroudy is offline   Reply With Quote
Old 09-15-2020, 09:36 AM   #6
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: Apr 2020
Location: Leipzig
Posts: 1,567
Default

Why not simply copying his script into your script and run it from there?

The problem is: actions never wait for another action to complete, so you would need to add functions into heda's script who signal to you "I'm finished".

It's possible, but modifications to Heda's script will get lost, if you update it at some point. So adding it into your script is probably the best way.

You can do it like:

Code:
function HedaScript()
  -- put in here all the lines of heda's script and then call the function HedaScript, when needed
end
but this probably only works, if Heda's script is not a background-script. To find that out, just look for reaper.defer in his script. If you can't find reaper.defer, you can use the approach that I pointed out above.
__________________
Use you/she/her, when contacting me, please. Thanks :) Not mentoring via PMs, sorry.
Ultraschall-API - 1294 ReaScript functions for Reaper - Reaper Internals - Developerdocs4Reaper - Donate, if you wish
Meo-Ada Mespotine is online now   Reply With Quote
Old 09-15-2020, 09:45 AM   #7
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: Apr 2020
Location: Leipzig
Posts: 1,567
Default

Just to clarify even further:

all actions run by reaper.Main_OnCommand will be run immediately AND the script will continue to execute, no matter if the action has been finished yet.
You can not rely on any order of execution.

all functions, like AutoSaveEachTrack and UnselectTracks will be run in order, means: one function finishes before continuing further.
So you can rely on a certain order of execution.
(Unless you use fancy concepts as defer or coroutines...but they are another story...)
__________________
Use you/she/her, when contacting me, please. Thanks :) Not mentoring via PMs, sorry.
Ultraschall-API - 1294 ReaScript functions for Reaper - Reaper Internals - Developerdocs4Reaper - Donate, if you wish
Meo-Ada Mespotine is online now   Reply With Quote
Old 09-16-2020, 04:30 AM   #8
Stroudy
Human being with feelings
 
Stroudy's Avatar
 
Join Date: Jul 2014
Location: London
Posts: 471
Default

Thanks - I can run HeDa's script by copying it into my own, however, I still have the same problem - the SWS command will not wait.

As you can tell, I'm no expert scripter (hahaha - to put it mildly). Is there a way to set a While loop. While reaper.CountSelectedTracks == 0 ? Something that exits the loop when reaper.CountSelectedTracks > 0. Then it can trigger the sws command.

I'm trying to do a repeat function but I don't really know how to run this inside another function, which is what I need - I think.
Code:
function sws_autosave_track_template

   while (track_count < 1)
      if track_count >= 1
         reaper.Main_OnCommand(_sws_autosavetracktemplate)
      end
   end
end
This is the structure I'm envisaging, but I'm not sure how to do this at all

Thanks for any help
Stroudy is offline   Reply With Quote
Old 09-16-2020, 05:32 AM   #9
heda
Human being with feelings
 
heda's Avatar
 
Join Date: Jun 2012
Location: Spain
Posts: 6,093
Default

mmmm it wont work with my action
It wasn't designed to be used like that. sorry. it only tells TI to do the job and finishes. and then your script continues but it doesn't wait for TI to do the job.
I think I could me TI to set a extstate after it finishes. then you could do a loop after my action waiting for that variable before continuing your script

it is not as easy as doing a while loop.. you need to do it as a defer script
heda is offline   Reply With Quote
Old 09-16-2020, 06:41 AM   #10
Stroudy
Human being with feelings
 
Stroudy's Avatar
 
Join Date: Jul 2014
Location: London
Posts: 471
Default

Could I run that function in its own script.

Basically a script that will only action the SWS command once the track count is not nil.

That way I could have a custom action that

1. Unselects all tracks
2. Runs your tag script which selects tracks with *** tag
3. A script that will only action the SWS command once the track count is not nil.

That should work, no?
Stroudy is offline   Reply With Quote
Old 09-16-2020, 06:56 AM   #11
heda
Human being with feelings
 
heda's Avatar
 
Join Date: Jun 2012
Location: Spain
Posts: 6,093
Default

yes
that script should be a defer script checking for selected tracks before running the SWS action
heda is offline   Reply With Quote
Old 09-17-2020, 08:06 AM   #12
Stroudy
Human being with feelings
 
Stroudy's Avatar
 
Join Date: Jul 2014
Location: London
Posts: 471
Default

If anyone can share some resources on how to do this?

I am aware of the basic structure but not the syntax or pitfalls. e.g. I believe there needs to be some kind of "time out" feature to the while loop, otherwise it could potentially run indefinitely. Is this correct?

thanks
Stroudy is offline   Reply With Quote
Old 09-17-2020, 09:05 AM   #13
heda
Human being with feelings
 
heda's Avatar
 
Join Date: Jun 2012
Location: Spain
Posts: 6,093
Default

Quote:
Originally Posted by Stroudy View Post
If anyone can share some resources on how to do this?

I am aware of the basic structure but not the syntax or pitfalls. e.g. I believe there needs to be some kind of "time out" feature to the while loop, otherwise it could potentially run indefinitely. Is this correct?

thanks
something like this

Code:
function loop()
	if reaper.CountSelectedTracks(0) == 0 then 
		reaper.defer(loop)
	else
		reaper.MB("running SWS now", "SWS", 0)		
	end
end

loop()
heda 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 06:19 AM.


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