Old 11-10-2019, 12:32 PM   #1
d.vyd
Human being with feelings
 
Join Date: Dec 2017
Posts: 63
Default Getting started with Python and Reaper

I have a few questions about Python scripting in Reaper:

1. Is it possible to access Reaper functionality through a Python library (e.g. "import reaper") instead of through Reaper's GUI?

2. How can I get a list of VSTs known by Reaper (through the directories I set previously in the GUI)?

3. Is TrackFX_AddByName the correct way to add a new track with VST? How does it look called via Python?

4. For VSTs like Reaktor, how can I load an ensemble/patch after loading the main VST? Can I access Reaktor's file menus via Reaper?

5. How can I get a list of a particular VST's parameters (controls), their current values, and their minimum and maximum acceptable values?

6. Is RPR_MIDI_InsertNote the correct way to add a note to a track? How does it look called via Python?

7. How can I render all current tracks to a stereo WAV file?

Thank you!
-da'ud vyd
d.vyd is offline   Reply With Quote
Old 11-10-2019, 12:55 PM   #2
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 7,990
Default

Quote:
Originally Posted by d.vyd View Post
I have a few questions about Python scripting in Reaper:

1. Is it possible to access Reaper functionality through a Python library (e.g. "import reaper") instead of through Reaper's GUI?

2. How can I get a list of VSTs known by Reaper (through the directories I set previously in the GUI)?

3. Is TrackFX_AddByName the correct way to add a new track with VST? How does it look called via Python?

4. For VSTs like Reaktor, how can I load an ensemble/patch after loading the main VST? Can I access Reaktor's file menus via Reaper?

5. How can I get a list of a particular VST's parameters (controls), their current values, and their minimum and maximum acceptable values?

6. Is RPR_MIDI_InsertNote the correct way to add a note to a track? How does it look called via Python?

7. How can I render all current tracks to a stereo WAV file?
I don't program with Python ReaScript anymore, so I can't give code examples.

1) No, Reaper does not work as a library usable from external programs. The Reaper process must be running and in the default ReaScript implementation the scripts must be run inside Reaper itself.

2) You may be able to do that by parsing the contents of the ini-files where the plugin scan results have been written in.

3) TrackFX_AddByName adds the effect to an existing track, it doesn't create new tracks for you.

4) Support for dealing with plugin presets is poor in ReaScript. Plugin menus etc are not accessible by standard ReaScript code.

5) TrackFX_GetNumParams, TrackFX_GetParamNormalized to enumerate the parameters and their current normalized values. TrackFX_GetParam may return information about the minimum and maximum values of a parameter, but it doesn't work with all plugins/formats.

6) RPR_MIDI_InsertNote is used to add a note to takes, not tracks. You need to have previously created MIDI media items and takes in them for that to work.

7) You can set the render file name etc with GetSetProjectInfo_String and then call the action "File: Render project, using the most recent render settings, auto-close render dialog" from the script. There is no official "easy" API to render projects, but the method explained should work.
__________________
For info on SWS Reaper extension plugin (including Xenakios' previous extension/actions) :
http://www.sws-extension.org/
https://github.com/Jeff0S/sws
--
Xenakios blog (about HourGlass, Paul(X)Stretch and λ) :
http://xenakios.wordpress.com/
Xenakios is offline   Reply With Quote
Old 11-10-2019, 01:01 PM   #3
d.vyd
Human being with feelings
 
Join Date: Dec 2017
Posts: 63
Default

Thanks Xenakios! Are there example Python scripts anywhere? That is disappointing about not being able to select presets in regular VSTs or load Reactor ensembles once Reaktor has been added to a track.
d.vyd is offline   Reply With Quote
Old 11-11-2019, 04:18 AM   #4
d.vyd
Human being with feelings
 
Join Date: Dec 2017
Posts: 63
Default

I see the API command "ShowActionList", but how do you call actions via the API? I'm not sure how to call "File: Render project, using the most recent render settings, auto-close render dialog".
d.vyd is offline   Reply With Quote
Old 11-11-2019, 05:40 AM   #5
-_~
Human being with feelings
 
Join Date: Oct 2019
Posts: 19
Default

You see that the actions have Command IDs in the Action List. You pass these numbers to the RPR_Main_OnCommand or RPR_Main_OnCommandEx API functions.
-_~ is offline   Reply With Quote
Old 11-11-2019, 06:07 AM   #6
d.vyd
Human being with feelings
 
Join Date: Dec 2017
Posts: 63
Default

Thank you!
d.vyd is offline   Reply With Quote
Old 11-11-2019, 11:42 AM   #7
d.vyd
Human being with feelings
 
Join Date: Dec 2017
Posts: 63
Default How do I run a Python script?

I have Python 3.7 installed on my Windows10 machine.

The Reaper instructions say:
https://www.reaper.fm/sdk/reascript/reascript.php
"To load a script, click ReaScript: "Load..." in the Actions window, and select the script file you want to add to the action list (multiple selection is allowed)."

I do not have a load command (see screenshot attached) and the run/edit command is for LUA/EEL only. It will not execute Python code (I tried).
Attached Images
File Type: png capture_reascript.png (9.9 KB, 22 views)
d.vyd is offline   Reply With Quote
Old 11-11-2019, 12:11 PM   #8
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 9,182
Default

You also need to enable your Python installation for ReaScript use (if you haven't done that already).
Here's how it looks here (though it may differ depending on Python installation), then 'python' should also show up alongside EEL and lua in the actions.

nofish is offline   Reply With Quote
Old 11-11-2019, 12:42 PM   #9
d.vyd
Human being with feelings
 
Join Date: Dec 2017
Posts: 63
Default

Unfortunately, I'm running into the problems discussed below and would prefer not to create a separate environment just for Reaper if possible. I'll contact Cockos unless someone has an idea here... I keep pointing to the DLLs and Reaper crashes the second I click on an action involving scripts! I'm using an Anaconda distribution with Python 3.7 64bit on Windows. I've setup the default environment with a bunch of packages I use regularly. Reaper is also 64bit.

https://forum.cockos.com/showthread.php?t=199301
d.vyd is offline   Reply With Quote
Old 11-12-2019, 08:37 AM   #10
d.vyd
Human being with feelings
 
Join Date: Dec 2017
Posts: 63
Default

I tried vanilla installations from python.org including Python27, Python33, and Python38. None of them worked. Sometimes Reaper crashed. Sometimes it complained that it didn't recognize the "py" extension. However, that extension is registered on my machine. I use Python everyday. I really don't understand the issue. Does it just need a Pythonxx.dll? Anything else?

Last edited by d.vyd; 11-12-2019 at 08:58 AM.
d.vyd is offline   Reply With Quote
Old 11-12-2019, 12:24 PM   #11
Justin
Administrator
 
Justin's Avatar
 
Join Date: Jan 2005
Location: NYC
Posts: 12,569
Default

I have not yet tested with Anaconda, however I tried the following steps (successfully):

1) Installed REAPER 5.984 x64
2) Installed Python 3.8 for x86-64 via https://www.python.org/downloads/release/python-380/ -- specifically, this installer: https://www.python.org/ftp/python/3.....8.0-amd64.exe
3) Launched REAPER/x64, went to Preferences/Plug-ins/ReaScript, clicked "Enable Python", specified C:\Program Files\Python38 for the path, specified python38.dll for the specific DLL. Clicked "OK", REAPER says that we must restart REAPER.
4) Restart REAPER
5) Go to actions window, choose ReaScript: New...
6) Specify a new file, e.g. "test.py"
7) The ReaScript IDE opens, type in RPR_MB("test","thing",0)
8) Type Ctrl+S to test it
9) (script displays MessageBox)

Edit: tested with Anaconda, created an environment and pointed REAPER to that. That does indeed cause REAPER to crash, the Python library aborts with the error "unable to load the file system codec". Not sure I feel like figuring out why.

Last edited by Justin; 11-12-2019 at 01:04 PM.
Justin is online now   Reply With Quote
Old 11-12-2019, 12:51 PM   #12
-_~
Human being with feelings
 
Join Date: Oct 2019
Posts: 19
Default

It works here with the Python 3.7 (64 bit) that Visual Studio installs for its Python dev features too.

One point is that there are two pythonX.dll files in that directory:

python3.dll 58KB
python37.dll 3661KB

I need to tell REAPER to use python37.dll in the "Force ReaScript to use specific Python dll" text box in Preferences->ReaScript. If I instead tell it to use python3.dll I get this when trying to load a script:

Quote:
Can't initialize Python.
Extension not supported "py"
-_~ is offline   Reply With Quote
Old 11-12-2019, 01:06 PM   #13
d.vyd
Human being with feelings
 
Join Date: Dec 2017
Posts: 63
Default

Thanks for looking into it. I'll follow your steps exactly and see if I can get it working!

EDIT: Yes, this is working now with Python38 64bit in C:\Program Files\. Thanks!

Last edited by d.vyd; 11-12-2019 at 02:21 PM.
d.vyd is offline   Reply With Quote
Old 11-13-2019, 01:04 AM   #14
junh1024
Human being with feelings
 
Join Date: Feb 2014
Posts: 127
Default

https://github.com/junh1024/Reaper-S...master/Scripts

Some simple stuff which may or may not be useful for you


See API: https://www.extremraym.com/cloud/reascript-doc/
junh1024 is offline   Reply With Quote
Old 11-13-2019, 01:35 AM   #15
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 8,151
Default

Quote:
Originally Posted by Justin View Post
2) Installed Python 3.8 for x86-64
Is Beyond compatible with that version of Python ?

-Michael
mschnell is offline   Reply With Quote
Old 11-13-2019, 10:42 AM   #16
d.vyd
Human being with feelings
 
Join Date: Dec 2017
Posts: 63
Default

Is this the main location of Beyond?
https://github.com/Samelot/Reaper/tr...20Python%20V26

How is it different from or similar to python-reapy:
https://pypi.org/project/python-reapy/

python-reapy looks more current, but does not claim to access the entire API.
d.vyd is offline   Reply With Quote
Old 11-13-2019, 02:54 PM   #17
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 8,151
Default

Beyond uses the OSC protocol to provide the Reaper API as Python object to a stand alone Python program that runs on the same box or on another box connected via network.

I understand that python-reapy lives completely inside of Reaper.

-Michael
mschnell is offline   Reply With Quote
Old 11-14-2019, 06:14 AM   #18
d.vyd
Human being with feelings
 
Join Date: Dec 2017
Posts: 63
Default

According to python-reapy docs:
>>>You can now import reapy from inside or outside REAPER as any standard Python module.

What is the current version of Beyond? V27.zip from 2007?
d.vyd is offline   Reply With Quote
Old 11-14-2019, 11:39 AM   #19
d.vyd
Human being with feelings
 
Join Date: Dec 2017
Posts: 63
Default

As a person familiar with Python and not familiar with Reaper's API, reapy is pretty intuitive so far. I'm able to work in the editor I prefer (Visual Studio Code) and connect to an open Reaper window!

# load Reaper library/API
import reapy

# connect to current project
project = reapy.Project()

# add a new track
project.add_track(name="drums")
d.vyd is offline   Reply With Quote
Old 11-16-2019, 06:34 AM   #20
romeo_despres
Human being with feelings
 
Join Date: Jan 2019
Posts: 35
Default

Quote:
Originally Posted by d.vyd View Post
python-reapy looks more current, but does not claim to access the entire API.
The entire ReaScript API is available in reapy. If you want for instance to use RPR_APITest, you can run:

Code:
>>> import reapy.reascript_api as RPR
>>> RPR.APITest()  # Or RPR.AnyReaScriptFunction()
reapy also intends to wrap the ReaScript API in a more intuitive and pythonic API. However this second API does not cover the whole ReaScript API yet, but only about a half of it.
romeo_despres is offline   Reply With Quote
Old 11-19-2019, 12:49 PM   #21
d.vyd
Human being with feelings
 
Join Date: Dec 2017
Posts: 63
Default

Is anyone finding that the notes in the MIDI take do not appear until you double-click on the MIDI take? Then I need to edit a note in the take to make all the notes appear in Reaper's main window.
d.vyd is offline   Reply With Quote
Old 11-24-2019, 12:54 PM   #22
d.vyd
Human being with feelings
 
Join Date: Dec 2017
Posts: 63
Default

I'm finding two problems with the script I wrote below. (1) the MIDI notes are not added to the take until I manually double-click on the take to open it, see the MIDI notes, and then click one of the MIDI notes. Then, suddenly they all appear in the main Reaper window. (2) action 41824 (render project) requires a confirm button be pressed in the GUI, which freezes Python. Is there a way to automate the whole rendering process from Python?

Code:
#load Reaper library/API
import reapy
from random import randint as r, choice

class note():
    def __init__(self,pitch,start,end,velocity):
        self.pitch = pitch
        self.start = start
        self.end = end
        self.velocity = velocity

def add_pattern_Reaper(pattern,channel=1):
    for n in range(len(pattern)):
        note = pattern[n]
        take.add_note(start=note.start,end=note.end,pitch=note.pitch,velocity=note.velocity,channel=channel,unit="beats")

project = reapy.Project()
track = project.add_track(name="synth")
track.add_fx("Ultra Analog Session 2")
item = track.add_midi_item(start=0,end=12)
take = item.active_take
# (created pitches, durations, velocities, and spacings here)
pattern_01=create_pattern(0,pitches,durations,velocities,spacings,32
add_pattern_Reaper(pattern_01)

project.perform_action(41824)

Last edited by d.vyd; 11-27-2019 at 08:44 AM.
d.vyd is offline   Reply With Quote
Old 11-27-2019, 01:21 PM   #23
romeo_despres
Human being with feelings
 
Join Date: Jan 2019
Posts: 35
Default

First question:

For some reason, it seems you have to add RPR.MIDI_Sort(take.id) at the end of your script (assuming you have imported reapy.reascript_api as RPR). I'll try to add it to the reapy source code so that one does not have to do it manually after adding notes, and let you know when it's done.

Second question:

Surpisingly, there doesn't seem to be any ReaScript function for script-based rendering. So I haven't found any better option than yours... :/
romeo_despres is offline   Reply With Quote
Old 11-28-2019, 02:03 AM   #24
d.vyd
Human being with feelings
 
Join Date: Dec 2017
Posts: 63
Default

Thanks Romeo! And thanks for your work on reapy!

I'll contact Cockos with the feature request to update the "render project" API with a "just do it!" parameter that keeps the GUI out of the way.
d.vyd 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 12:48 PM.


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