Hi there.
Spoiler: It seems I found how to make it work. I had to defer the renaming part after the recording stopped.
Anyway, find below a simplify (working) code.
Arm a track and launch the script. It will record on this track for little more than 0.50 sec, rename the source file to NewName_1.wav, then move the play head ahead and record another one, name it NewName_2.wav... Three times (gNumRecordings=3)
Delete these files from your disk if you want to run the script another time, otherwise a "file already exists" error will occur.
There are basically 3 functions calling each other:
- BeforeRec: starts recording and calls the wait function
- WaitTillPosition: defers itself until at least 0.5s has passed. Then stops the recording and calls AfterRec()
- AfterRec(): rename the source file then call BeforeRec again to restart the loop.
It is now working.
What I was doing wrong (without it being obviously wrong even afterwards) was that in the function WaitTillPosition(...) I was calling directly
instead of defering it like this
Code:
RPR_defer("AfterRec()")
Here's the whole code:
Code:
import os
from sws_python import *
gNumRecordings = 3
gCounter = 0
gProjPath = RPR_GetProjectPath("", 512)[0]
def msg(str):
RPR_ShowConsoleMsg(str + "\n")
###################################################################
def BeforeRec():
global gCounter
if gCounter >= gNumRecordings:
return
gCounter += 1
msg(f"BeforeRec: counter = {gCounter}")
#Transport: Record
RPR_Main_OnCommand(1013, 0)
WaitTillPosition(RPR_GetPlayPosition() + 0.5) #stop recording after roughly 0.5f
###################################################################
def WaitTillPosition(endPos):
pos = RPR_GetPlayPosition()
if pos >= endPos:
msg(f"Stopping recording at {pos}")
#Transport: Stop (save all recorded media)
RPR_Main_OnCommand(40667, 0)
RPR_defer("AfterRec()")
return
RPR_defer(f"WaitTillPosition({endPos})")
###################################################################
def AfterRec():
msg(f"AfterRec: Counter = {gCounter}")
newfilepath = os.path.join(gProjPath, f"NewName_{gCounter}.wav")
#### RENAMING HERE ####
it = RPR_GetSelectedMediaItem(0,0)
take = RPR_GetActiveTake(it)
src = RPR_GetMediaItemTake_Source(take)
oldfile = " " * 256;
(_, oldfile, _) = RPR_GetMediaSourceFileName(src, "", 256)
#Item: Set selected media offline
RPR_Main_OnCommand(40440, 0)
if os.path.exists(newfilepath):
msg("Error: Destination file already exists.")
#Item: Set selected media online
RPR_Main_OnCommand(40439, 0)
return 0;
msg(oldfile)
msg(newfilepath)
os.rename(oldfile, newfilepath)
#RPR_SetMediaItemTake_Source(take, source)
res = BR_SetTakeSourceFromFile(take, newfilepath, False) #, True)
#Item: Set selected media online
RPR_Main_OnCommand(40439, 0)
RPR_GetSetMediaItemTakeInfo_String(take, "P_NAME", os.path.basename(newfilepath), True)
##### RENAMING DONE #####
#Peaks: Rebuild peaks for selected items
RPR_Main_OnCommand(40441, 0)
RPR_UpdateArrange()
# move cursor ahead for next recording
RPR_SetEditCurPos(BR_GetNextGridDivision(RPR_GetPlayPosition()+1.0), True, False)
BeforeRec()
BeforeRec()
The weird thing is that when it is not working (not defering the call to AfterRec() in WaitTillPosition()), only the second recording raises the error. The first one gets renamed successfully.
That's it.