Old 01-30-2015, 04:19 AM   #1
jrp
Human being with feelings
 
Join Date: Nov 2014
Posts: 149
Default store a variable in file for use with other script

Hello,

I have, by copy and pasting from examples i found, tried to write a variable a into a file, to late restore it with another script.

I have the feeling this could be quite handy for different tasks - eg to let a script know if a action has already been done, to set global "modes" for a number of scripts, etc.
For example one script to be used to set a variable in the file to 1 or 0.
Other scripts could look at that variable and show different behaviors deppending on what number they see.



Below are my experiments, if you feel this should be handled differently please give me a hint - i really have no idea what i am actually doing here.


With this code (adapted from the superglue scripts) i can actually create a file flags.db

I have no idea if i am actually writing to it, opening the file in text editor shows a bunch of strange symbols...

also if i try to define a = 1 and call it real, integer or number i get an error that the variable type is not supported. Only works with text like this:

Code:
import sqlite3


curr_proj = 0		
project_path = ""	

(curr_proj, project_path, bufsz) = RPR_GetProjectPathEx(curr_proj, "", 256)

def store():
    a = "h" # would in the later script be defined by conditions...
    conn = sqlite3.connect(project_path + "/flags.db")
    c = conn.cursor()
    c.execute("CREATE TABLE IF NOT EXISTS flag (a text)")
    c.execute("INSERT INTO flag VALUES (?)", (a))
    conn.commit()
    conn.close()
store();

Trying to read it back with another script doesn´t work at all, i am probably on the wrong track here, right?

i get an error with the line results = c.execute("SELECT a FROM flag WHERE a = ?", (a,))

a is not defined...

Code:
import sqlite3


curr_proj = 0		
project_path = ""	

(curr_proj, project_path, bufsz) = RPR_GetProjectPathEx(curr_proj, "", 256)


def read():
    conn = sqlite3.connect(project_path + "/flags.db")
    c = conn.cursor()
    c.execute("CREATE TABLE IF NOT EXISTS flag (a text)")
    results = c.execute("SELECT a FROM flag WHERE a = ?", (a,))
    conn.commit()
    RPR_ShowConsoleMsg(a)
    conn.close()

read();
jrp is offline   Reply With Quote
Old 01-30-2015, 04:36 AM   #2
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 7,713
Default

Using a Sqlite database to write and read simple variable values is overkill! (And likely pretty slow too.)

The Reaper API already has SetExtState, GetExtState, DeleteExtState, HasExtState, functions which allow storing and loading state data into/from an automatically managed ini-file.
__________________
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 online now   Reply With Quote
Old 01-30-2015, 05:04 AM   #3
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 7,713
Default

I wrote some example scripts.

To store :

Code:
curpos=RPR_GetCursorPosition()
RPR_SetExtState("my_script", "editcursorposition", curpos, True)
To load :

Code:
exists=RPR_HasExtState("my_script", "editcursorposition")
if exists==True:
  cursorposition=RPR_GetExtState("my_script","editcursorposition")
  RPR_SetEditCurPos(float(cursorposition), False, False)
As you can see, a bit simpler than the sqlite database approach...
__________________
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 online now   Reply With Quote
Old 01-30-2015, 05:11 AM   #4
jrp
Human being with feelings
 
Join Date: Nov 2014
Posts: 149
Default

wonderfull!
I had the feeling my approach was a little unfortunate...
This happens if a noob learns by copy and paste...
And now i understand what those ExtState commands are about.
I say thank you!
jrp is offline   Reply With Quote
Old 01-30-2015, 05:20 AM   #5
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 7,713
Default

If you need to examine the ini-file, it's located on Windows somewhere like :

C:\Users\MyName\AppData\Roaming\REAPER\reaper-extstate.ini

I don't remember the location off-hand for OS-X.

Note that using this ini-file works best for cases where you don't need per-project specific data. (The ini file is shared between Reaper projects.) However, if you feel like going hacky, you can of course try to store project specific data if you can come up with suitable unique key strings for the data entries. To get proper per-project script state save/load, you will need to wait for Reaper 5. (Or if you dare, use the prerelease version already available.)
__________________
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 online now   Reply With Quote
Old 01-31-2015, 07:13 AM   #6
jrp
Human being with feelings
 
Join Date: Nov 2014
Posts: 149
Default

now i had a little time to play with this.

one scenario i can imagine is using a key to toggle through a number (5 in the excample) of modes for other scripts.
maybe make F1 to F12 choose custom colors, markers, track templates, fx-chain templets, inputs or outputs... stuff like that.

Works nicely like that:

Code:
# increment and store variable a from 1 to 5
exists=RPR_HasExtState("my_script", "flag_a")
if exists==False:
    a=0
    RPR_SetExtState("my_script", "flag_a", a, False)
    
a=RPR_GetExtState("my_script","flag_a")   
a = int(a)+1
if (a==6):
    a=1    
RPR_SetExtState("my_script", "flag_a", a, False)
Code:
exists=RPR_HasExtState("my_script", "flag_a")
if exists==False: # variable not set yet by other script? so we set it ourself...
    a=1
if exists==True:
    a=RPR_GetExtState("my_script","flag_a")
##############
if (a==1):
    #do something
if (a==2):
    #do something else
if (a==3):
    #and so on
Unfortunately i see no simple way to show the state of variable a on the UI.
I am thinking if the read script could be made to send out a midi command. that could easily be translated into a led going on or off, hardwarewise...


Anyway, if only two states are necessary, eg to have something like a sticky modifier for certain tasks, the SWS/S&M Dummy Toggle is great, as it can be included in the main toolbar to show it´s toggle state.

set it:
Code:
RPR_Main_OnCommand(RPR_NamedCommandLookup("_S&M_DUMMY_TGL1"), 0)
read it:
Code:
a = RPR_GetToggleCommandState(RPR_NamedCommandLookup("_S&M_DUMMY_TGL1"));
jrp 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 05:39 AM.


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