How to save settings? Req. help, rubyscript.
-
I want to make a "set defaults menu" using UI.inputbox and I want the selected option to be saved for next time the script is used. First idea was a "settings" .txt file, where to write the values, and each time I load the script, it reads the values from there. A second idea was to write inside the .rb file itself, but I don't really know how to get to my variable and then update the value.
I hope there is a more simple and direct solution for this.
Thanks in advance.
-
If you set the tool's variables inside a
def
using@xxx
variables they are remembered during that use of the tool that session. With aTool
it's during it's use/reuse, unless you use@@xxx
variables declared outside of adef
.You can save your setting to the Model itself as Attributes [boolean/float/integer/string/array] and get them later, use:
variable1=model.get_attribute("NewOne","SomeVariable1",nil)
Then,if not variable1
it's not pre-set, so set it to a default value and continue...
On successful completion use:
model.set_attribute("NewOne","SomeVariable1",@newvariable)
See my '2D Text Tool' for an example of this...These attribute variables are specific to each Model - of course you could set attributes to anything - say acomponent instance - and if that were selected the dialog's values would reflect that instance's values...
To save 'global' values, so they are the same [for each user on that PC/Mac] whatever Model you open and so they are remembered as the very last settings you OK'd, you need to write values to the registry/ini and then read them back from there - use http://code.google.com/apis/sketchup/docs/ourdoc/sketchup.html#read_default and http://code.google.com/apis/sketchup/docs/ourdoc/sketchup.html#write_default
as Section/Variable/Value - very much like an attribute for Sketchup itself rather than a Model or Component etc...If you want the same settings for all users on that PC or perhaps even everyone on a network then you'll have to save the settings to your own 'ini' file kept somewhere that's accessible to all - somewhere on a server - perhaps with you licence files ? Then trap for it not being available - with some defaults set if not found... Read and write to that file as before - typical format would be "var1=123 \n var2=456" etc: read the lines in the file one at a time and split them by "=" and you'll have the variable name [0] and its value [1] - because it'll all be text you need to change variables from a string using "123".to_f or "456".to_i etc as required...
-
Most people don't think about code that writes code. It's usually fun and, as it's writing plain text, it's easy to debug.
Once the values are chosen, write "settings.rb". Next run, just load it. Here's one "settings.rb" that returns, when asked, a volume setting (10) and whether you want to punch up the base (you do):
def volume 10 end def loudness true end
A very little bit of code should take a name and value as arguments and write:
def {name here} {value here} end
To read:
load "settings.rb" vol = volume loud = loudness
-
@tig said:
To save 'global' values, so they are the same [for each user on that PC/Mac] whatever Model you open and so they are remembered as the very last settings you OK'd, you need to write values to the registry/ini and then read them back from there - use http://code.google.com/apis/sketchup/docs/ourdoc/sketchup.html#read_default and http://code.google.com/apis/sketchup/docs/ourdoc/sketchup.html#write_default
as Section/Variable/Value - very much like an attribute for Sketchup itself rather than a Model or Component etc...
"...of a .INI file or registry (within the Software > @Last Software > SketchUp section)." This is what I read in documentation. I don't understand really where that .INI file has to be. Or can I set a path?
-
@newone said:
"...of a .INI file or registry (within the Software > @Last Software > SketchUp section)." This is what I read in documentation. I don't understand really where that .INI file has to be. Or can I set a path?
You don't need to worry about 'where it is'... just write the details as set out in the guide and it's saved/read from the right place in the PC's Registry (or a Mac's ini)...
Advertisement