Storing Data
-
Hello,
How do you guys go about storing data between opening and closing your models?
If you have a class which draws say a box of a size entered by the user and you have several methods in your class for adjusting the box, obviously the object created in your script is deleted when you close down Sketchup, Also I haven't tested but is your object deleted if you select another tool?
Ideally being able to store everything in the model with no external files seems the more convinent approach, but I can see how the file size and model performance could be comprimised.
-
You want to keep a reference to an object between sessions.
To do so you'd have to add an ID attribute to your object - then look for it when you load the model again.
Note! There is no easy to to find that - you'd have to scan your model. This can be time consuming so I'd recommend a lazy search - as in only look for it when the user wants to interact with it. Don't attach an observer to onModelOpen and start scanning every entity in every model. You'll impact performance too much and make the user experience too slow.If the object is in a group or component you can tag the ComponentDefinition - and you only have to scan the list of definitions.
@cheez said:
Also I haven't tested but is your object deleted if you select another tool?
No - but various operations might kill the entity you reference. In general - you should be careful about keeping permanent reference to en entity. Always make sure to validate the reference to your entity as erased entities can crash SketchUp if you try to interact with them.
-
Let's assume your 'box' is a component.
Let's assume there is a value 'x' thet you want it to 'remember' for another time...
You have a reference to the definition - let's say it's 'defn
'...
Then this code remembers it:
defn.set_attribute("Cheez_Data","x",x)
Provided that you save the model the next time you want to access the SKP and that definition - perhaps via a selected instance... likedefn=model.selection[0].definition
- after you test for it being a suitable instance etc...
You could use:
x=defn.get_attribute("Cheez_Data","x",0)
Here it's set to return 0 if it's not been set before - otherwise it's 'nil'.You can store loads of different data in an object's attribute-dictionary.
The data types can be integer, float, string, boolean, array...
A hash has to be stored as an array for the 'set'... and reconstructed into a hash after the 'get'.You could attach data to the the model itself, BUT since you are going to change a specific definition you might be best storing it there...
You can attached attribute data to most 'types', but some like geometry are more 'transient'...
Advertisement