Old 09-17-2018, 10:21 AM   #1
Cedrik0s
Human being with feelings
 
Join Date: Oct 2014
Posts: 77
Default Renaming source files from a script

Hi there.
Using Reascript in Python.

I would like to programmatically rename some item's take source file.
I can't seem to do that because the file is in use (presumably by Reaper itself).
Though it must be feasible since actions like "Xenakios/SWS: Rename takes and source files..." (_XENAKIOS_RENMTAKEANDSOURCE) can do that (but can't be used programmatically since they expect a GUI input).

I tried to create a copy of the source file and rename it as I wish, and then create a source with PCM_Source_CreateFromFile. Then I would use SetMediaItemTake_Source to tell the item to use this new source. Then I would call PCM_Source_Destroy on the old source.

The call to SetMediaItemTake_Source returns correctly, but actually changes nothing in the item (item properties still refer to the old file), and PCM_Source_Destroy has reaper crash.

Can someone tell me what the correct (and working) approach would be?

Thanks a lot.
Cedrik0s is offline   Reply With Quote
Old 09-17-2018, 10:37 AM   #2
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
Default

It should basically work like that. Maybe you got your item and take pointers mixed up somewhere? (Much of the API calls deal with takes and not items.) Can you post your full code?
__________________
I am no longer part of the REAPER community. Please don't contact me with any REAPER-related issues.
Xenakios is offline   Reply With Quote
Old 09-17-2018, 10:48 AM   #3
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,937
Default

Looks like a bug in the Python API (doesn't work here either).
Code:
source = RPR_PCM_Source_CreateFromFile("/home/cfillion/test.wav")
item   = RPR_GetSelectedMediaItem(0, 0)
take   = RPR_GetActiveTake(item)
RPR_SetMediaItemTake_Source(take, source)
The same thing in Lua works just fine (https://forum.cockos.com/showthread.php?p=1889202):
Code:
source = reaper.PCM_Source_CreateFromFile("/home/cfillion/test.wav")
item   = reaper.GetSelectedMediaItem(0, 0)
take   = reaper.GetActiveTake(item)
reaper.SetMediaItemTake_Source(take, source)

Last edited by cfillion; 09-17-2018 at 11:18 AM.
cfillion is offline   Reply With Quote
Old 09-17-2018, 11:16 AM   #4
mpl
Human being with feelings
 
mpl's Avatar
 
Join Date: Oct 2013
Location: Moscow, Russia
Posts: 3,960
Default

Offline any items using that source then change SRC and get back online

Last edited by mpl; 09-17-2018 at 11:22 AM.
mpl is offline   Reply With Quote
Old 09-17-2018, 12:40 PM   #5
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,875
Default

I have few scripts to batch rename source files and it there was quite some tricky things to prevent conflict with existing file, but simple case can works with few lines of code (like cfillion code).
X-Raym is offline   Reply With Quote
Old 09-17-2018, 12:56 PM   #6
Cedrik0s
Human being with feelings
 
Join Date: Oct 2014
Posts: 77
Default

Thank you all for your answers.

As cfillion shows, there must be a bug in the python implementation, since the same code works in Lua.

As far as I understand, mpl's suggestion to put items offline before anything could be the right way to let files be unused by reaper and be able to be renamed directly (without a copy involved).

I'll try that tomorrow.
Thank you all.
Cedrik0s is offline   Reply With Quote
Old 09-18-2018, 12:56 AM   #7
Cedrik0s
Human being with feelings
 
Join Date: Oct 2014
Posts: 77
Default

Hi all.

I tried Cfillion's code and confirm in won't work in Python here either, just like my first attempts seemed to do nothing. It does work in Lua.

I wanted to try to set the item offline as suggested by mpl but actually can't seem to find a way to do so. How can I set an item offline?

Anyway I tested that, setting the item offline by hand in the Item/media menu before hand and restoring it online after running the script. This won't work either. Nothing seems to happen.

Is there a way to call some Lua code from a Python script?
I can always create a lua action and call it from my python script, but can I pass arguments?

Thank you guys
Cedrik0s is offline   Reply With Quote
Old 09-18-2018, 02:36 AM   #8
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,875
Default

Quote:
As far as I understand, mpl's suggestion to put items offline before anything could be the right way to let files be unused by reaper and be able to be renamed directly (without a copy involved).

It is not just that, you have to put offline the each items which could have the file in one of his takes in every opened projects !


There is native action to put item offline.


But in last SWS release you should have access to dedicated funciton like:
Code:
 reaper.NF_TakeFX_SetOffline( take, fx, bool )
X-Raym is offline   Reply With Quote
Old 09-18-2018, 02:51 AM   #9
Cedrik0s
Human being with feelings
 
Join Date: Oct 2014
Posts: 77
Default

Quote:
Originally Posted by X-Raym View Post
It is not just that, you have to put offline the each items which could have the file in one of his takes in every opened projects !
Of course, but in my test session there is no other item involved, and so putting offline just this one is enough. But it actually won't work.

Quote:
Originally Posted by X-Raym View Post
But in last SWS release you should have access to dedicated function like:
Code:
 reaper.NF_TakeFX_SetOffline( take, fx, bool )
Ah, I'll check that, but by its name, I'd think it has to do with the Take's FX, not with the item or take itself...

Thanks
Cedrik0s is offline   Reply With Quote
Old 09-18-2018, 06:04 AM   #10
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,875
Default

oops sorry wrong copy paste, but check if it the item function equivalent is not already in sws already.

If not I think the function exists in a sws beta build.
X-Raym is offline   Reply With Quote
Old 09-18-2018, 08:12 AM   #11
Cedrik0s
Human being with feelings
 
Join Date: Oct 2014
Posts: 77
Default

Quote:
Originally Posted by X-Raym View Post
oops sorry wrong copy paste, but check if it the item function equivalent is not already in sws already.

If not I think the function exists in a sws beta build.
I couldn't find anything like that.
Besides, there are actions available to set the selected items offline, but I wish I could do that on a specific item.

This would allow me to rename the file without conflicting with the OS, and then set the source of the item to this file.

But again, setting the source to another file doesn't work in Python anyway...
Am I suppose to report a bug for that? If so, where do I do that?

Thanks
Cedrik0s is offline   Reply With Quote
Old 09-18-2018, 09:40 AM   #12
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 9,875
Default

You didnt show us your code.


In python a special line has to be added to import sws functions. The function for setting source file is from sws.



checker reateam template repo for python minimal script.
X-Raym is offline   Reply With Quote
Old 09-18-2018, 10:04 AM   #13
Cedrik0s
Human being with feelings
 
Join Date: Oct 2014
Posts: 77
Default

Quote:
Originally Posted by X-Raym View Post
You didnt show us your code.


In python a special line has to be added to import sws functions. The function for setting source file is from sws.



checker reateam template repo for python minimal script.
I have used SWS functions in other scripts with no problem, but it's not the point here.
My test code is just that of Cfillion above. Bare simple, and not using SWS. Working in Lua but not in Pyhon.
The function I use, like Cfillion, for setting source file is RPR_SetMediaItemTake_Source. Which one do you refer to?

Thanks.
Cedrik0s is offline   Reply With Quote
Old 09-18-2018, 10:54 AM   #14
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,937
Default

SWS has BR_SetTakeSourceFromFile from the time REAPER didn't have SetMediaItemTake_Source yet. It seems to work fine with Python. It runs this code: https://github.com/reaper-oss/sws/bl...pp#L1294-L1318.

Quote:
Originally Posted by X-Raym View Post
But in last SWS release you should have access to dedicated funciton
The future function is CF_SetMediaSourceOnline from my PCM_source API pull request.

Quote:
Originally Posted by Cedrik0s View Post
I can always create a lua action and call it from my python script, but can I pass arguments?
Only through temporary extstates...

Quote:
Originally Posted by Cedrik0s View Post
Am I suppose to report a bug for that? If so, where do I do that?
It appears SetMediaItemTake_Source was broken in Python from the time it was added (in v5): https://forum.cockos.com/showthread.php?t=165074.
cfillion is offline   Reply With Quote
Old 09-18-2018, 12:31 PM   #15
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig
Posts: 6,621
Default

Just a quick note, has someone considered to report this bug in the proper forum?
Just an idea

Can't do it myself, as I have no experience with Python in Reaper at all, so I can't describe it properly...

Edit: I should have read cfillion's post first
Meo-Ada Mespotine is offline   Reply With Quote
Old 09-19-2018, 07:47 AM   #16
Cedrik0s
Human being with feelings
 
Join Date: Oct 2014
Posts: 77
Default

Thanks for your answer cfillion

Quote:
Originally Posted by cfillion View Post
SWS has BR_SetTakeSourceFromFile from the time REAPER didn't have SetMediaItemTake_Source yet. It seems to work fine with Python. It runs this code: https://github.com/reaper-oss/sws/bl...pp#L1294-L1318.
Ah very interesting! I had missed that.
Interesting link too!

Quote:
Originally Posted by cfillion View Post
The future function is CF_SetMediaSourceOnline from my PCM_source API pull request.
Ok, great. Future looks good

Quote:
Originally Posted by cfillion View Post
Only through temporary extstates...
I see.

Quote:
Originally Posted by cfillion View Post
It appears SetMediaItemTake_Source was broken in Python from the time it was added (in v5): https://forum.cockos.com/showthread.php?t=165074.
Oh this is a known problem then, which is good, but still unresolved, which is less good

Thanks a lot for your answer Cfillion.
Cedrik0s is offline   Reply With Quote
Old 09-20-2018, 04:05 AM   #17
Cedrik0s
Human being with feelings
 
Join Date: Oct 2014
Posts: 77
Default

Hi all.

Just to update you.

I could do what I wanted to thanks to BR_SetTakeSourceFromFile.
The only pity is that I am unable to set offline a specific item, but only the selected ones through the action #40440.
This is ok in my case because I am renaming the sourcefile of an item that has just been recorded by the script. So I only do that on the active take of the selected item.
Of course being able to set offline/online any given item with reascript (independently on the selection) would make it much better and general.

Can I ask for this feature to be added? How?

Thanks,
Cedric.
Cedrik0s is offline   Reply With Quote
Old 09-20-2018, 09:52 AM   #18
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,937
Default

That's what CF_SetMediaSourceOnline does...
cfillion is offline   Reply With Quote
Old 09-20-2018, 10:06 AM   #19
Cedrik0s
Human being with feelings
 
Join Date: Oct 2014
Posts: 77
Default

Quote:
Originally Posted by cfillion View Post
That's what CF_SetMediaSourceOnline does...
Of course
For some reason I thought it should be added to RPR functions as well...


Thanks cfillion.
Cedrik0s 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 09:03 AM.


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