Old 05-21-2014, 05:49 AM   #1
Lawrence
Human being with feelings
 
Join Date: Mar 2007
Posts: 21,551
Default Python in Visual Studio

Quick tutorial for the other "behind the curve" people like myself who already use Visual Studio and maybe also want to write Python Scripts for Reaper in a familiar IDE, not use another IDE for Python if you already have Visual Studio.

There's maybe (likely?) a better way to do / approach all this, and likely better IDE's for Python in general, no clue, but this is working for me so far...
Step One: Install Python Tools for Visual Studio

http://pytools.codeplex.com/

This add in will allow you to create Python projects in Visual Studio....




Step Two: Add the reaper_python.py file to the project or your template ...



... which will also give you a reference to all of the functions in the Object Browser...



Step Three: Now as you write your script you'll be in a familar IDE, which might be more useful than scripting in an unfamiliar IDE while also trying to learn a new language at the same time.

One of Tim's scripts is loaded below for my own learning / studying of syntax and method ...




Step Four: Right click the *.py file you're working on in the Solution Explorer and click "Copy Full Path"...



... and paste that path into Reaper to import that script you're editing to test, paste the path into the dialog to load the script...



So now that you have the script loaded in Reaper, you can bind it to a toolbar button or key to run it in Reaper as you edit in in VS.

Additional Note: Because VS also has a Web Browser built into it, you can also tab the API help page or other web resources inside your project or working template in case you need additional help ...



That's pretty much it. Edit the script in VS, reference the Object Editor for the function names, and run / test the script in Reaper as you edit it.

All you need to do then is learn / study the Python language syntax and methods, the part I'm still having a little trouble with.

What I haven't been able to figure out with it in VS is how, if even possible, to reference those objects directly to add some kind of Intellisense to the code window. If that's possible, that would be nice.

Hope that helps another ReaScript ReaTard like myself.


Lawrence is offline   Reply With Quote
Old 05-21-2014, 07:49 AM   #2
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
Default

Quote:
Originally Posted by Lawrence View Post

What I haven't been able to figure out with it in VS is how, if even possible, to reference those objects directly to add some kind of Intellisense to the code window.
Oh crap, your post started so awesome I thought the payoff would indeed be code completion for Python code that uses the ReaScript API... If the code completion isn't happening, what benefit is there to using something so large and complicated like Visual Studio for writing ReaScript? I myself just use Notepad++ and read the Reaper generated ReaScript API html in Chrome...Far from ideal, but I haven't found anything better...If you happen to find a way to get the code completion/Intellisense working in Visual Studio, please do mention about it here...
__________________
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 05-21-2014, 08:19 AM   #3
Lawrence
Human being with feelings
 
Join Date: Mar 2007
Posts: 21,551
Default

Quote:
Originally Posted by Xenakios View Post
If you happen to find a way to get the code completion/Intellisense working in Visual Studio, please do mention about it here...
Well, it kinda sorta works but it's pretty literal, the way I'm addressing it. The code below is just trying that to see what pops up.

There's no way I can see to directly "reference" those functions but below if I reference the file as an object in code you kinda get it. Would be better if all that popped up when simply typing "RPR"... but that works.



But I don't yet know enough about Python to know my way around doing those kinds of things, or if a script doing that would even work in Reaper.

And it works for Python in general, but again, no clue if any of the resulting scripts would actually work in Reaper...


Last edited by Lawrence; 05-21-2014 at 11:48 AM.
Lawrence is offline   Reply With Quote
Old 05-21-2014, 09:15 AM   #4
Lawrence
Human being with feelings
 
Join Date: Mar 2007
Posts: 21,551
Default

Yep, it works. I took an example from the wiki and used the object method to use Intellisense and it still worked in Reaper.

The object naming syntax is case sensitive (and maybe Python is universally, dunno) ... but using the object identifier like below enables Intellisense for the entire function library. Obviously, you could shorten the identifier to something really simple like a lower case "r", just to have a quick and easy way to have code completion.

Code:
import reaper_python as RP

project = 0 # current project
firsttrack = 0 # first selected track 
 
# return pointer to track
track = RP.RPR_GetSelectedTrack(project, firsttrack)
 
# a track is selected
if track:
	# ask user
	title = "Delete track"
	msg = "Are you sure you want to delete this track?"
	type = 4 # a yes/no msg box
	# if result = 6 -> user pressed yes -> delete track
	if RP.RPR_MB(msg, title, type) == 6:
		RP.RPR_DeleteTrack(track)
As another side note, one reason I wanted to use the object browser is because it has quick search filtering of the functions...


Last edited by Lawrence; 05-21-2014 at 09:27 AM.
Lawrence is offline   Reply With Quote
Old 05-21-2014, 10:36 AM   #5
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
Default

Cool! I should look into this at some point...(I am not the biggest fan of Visual Studio, I don't even use it for C++ coding anymore, but if it works for ReaScript, that would be great.)
__________________
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 05-21-2014, 10:59 AM   #6
Lawrence
Human being with feelings
 
Join Date: Mar 2007
Posts: 21,551
Default

Yeah. Now I just have to learn how to code in Python. Like this "training wheels" script I wrote below that's not working...

Code:
import reaper_python as RP

# Get the total number of tracks
tracks = RP.RPR_CountTracks(0)

# Loop through each track and
# pull down all the faders to 0
for x in range(0, (tracks -1)):
    RP.RPR_SetMediaTrackInfo_Value(RP.RPR_GetTrack(0,x), 'D_VOL', 0);
Is my loop range wrong? [0 to the number of tracks -1], and or is the formatting of SetMediaTrackInfo wrong? Man, I am used to Intellisense giving me descriptions along the way.
Lawrence is offline   Reply With Quote
Old 05-21-2014, 11:07 AM   #7
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
Default

Quote:
Originally Posted by Lawrence View Post
Yeah. Now I just have to learn how to code in Python. Like this "training wheels" script I wrote below that's not working...

Code:
import reaper_python as RP

# Get the total number of tracks
tracks = RP.RPR_CountTracks(0)

# Loop through each track and
# pull down all the faders to 0
for x in range(0, (tracks -1)):
    RP.RPR_SetMediaTrackInfo_Value(RP.RPR_GetTrack(0,x), 'D_VOL', 0);
Is my loop range wrong? [0 to the number of tracks -1], and or is the formatting of SetMediaTrackInfo wrong? Man, I am used to Intellisense giving me descriptions along the way.
The loop range is wrong, don't do the substraction.

for x in range(0,3) produces 0,1,2 as the value of x, not 0,1,2,3
__________________
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 05-21-2014, 11:38 AM   #8
Lawrence
Human being with feelings
 
Join Date: Mar 2007
Posts: 21,551
Default

Ah, thanks,

I didn't realize the upper limits of ranges accounted for that.. I'm accustomed to manually accounting for the difference between a physical count that starts at 1 and an indexed series of the same collection that starts at 0, hence the subtraction, "count -1".

I get it now, 3 is literal, not 0 to 3 (4), but 3.

Thanks.
Lawrence is offline   Reply With Quote
Old 05-23-2014, 05:47 AM   #9
Lawrence
Human being with feelings
 
Join Date: Mar 2007
Posts: 21,551
Default

Quote:
Originally Posted by Xenakios View Post
Cool! I should look into this at some point...
If you do, I'd love to hear your impressions of all that.

As to Python in general, rather than me copying and pasting other people's scripts and still not be able to understand what they're doing, I just started reading some "beginner" (as relates to previous code experience) tutorials and my first impression is highly positive, how clean and subjectively easy a lot of it all seems to be. The syntax is much different from wbat I mostly code in, .Net, but it's also much cleaner in a lot of ways.

Nice language. I'll study and read it for a few week before I dig in. I think my biggest obstacle will be trying to grasp some of the API parts, some of the arguments in the longer functions. I don't have any particular script I want or need to write for Reaper, but just want to learn it mostly because it's there.

Last edited by Lawrence; 05-23-2014 at 05:53 AM.
Lawrence is offline   Reply With Quote
Old 05-23-2014, 08:30 AM   #10
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
Default

Quote:
Originally Posted by Lawrence View Post
Nice language
One of the things that enrages me (when it happens for example when copy pasting code) is that Python differentiates between tabs and spaces as the white space that is used for code blocks. And that style of white space must match. So Python can give an error because white space wasn't written properly...

Another thing I don't like is how some of the Python libraries fail by throwing exceptions, even when the error condition is trivial. That necessitates putting simple blocks of code inside verbose try-except blocks. (Because if the exception handling isn't in the code, the script run is just terminated at the point of failure, even if the failure would be recoverable.)

Otherwise, I mostly find Python a nice language too.
__________________
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 05-23-2014, 08:53 AM   #11
Lawrence
Human being with feelings
 
Join Date: Mar 2007
Posts: 21,551
Default

Yes, the four spaces thing is kinda .... different.

Anyway, I'm a tech nut so I'll dig in a little here and there and see if I can add Python to my highly amateur coding skill set.

As a side note, although I don't see any potential practical application for it for anything i do, IronPython is really nice, using the .Net common runtime library. Man, if people could make Python gui's that easy in Reaper scripts that would be so cool. I gave Tkinter a brief look and I likely won't be going there.

Thanks X.
Lawrence 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 04:00 AM.


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