[Info] Allowable Classes for "set_attribute"
-
My railroad plugin relies heavily on these data being transformed. When a track is manually moved the bezier control points are moved with it and read by the extension so it knows where the track is located and trains can run on it. I was quite worried how to code this until I accidentally ran into this behavior. It can be really useful and if you don't want it it's easy to store the coordinates in an array instead of Point3d or Vector3d (which is what I do in another part of the plugin).
However this really really really should be documented!
-
@eneroth3 said:
However this really really really should be documented!
Indeed. I'll look into this next week to check out what you indicated to be inconsistencies. That worries me.
-
If I understand it right, an attribute with the same Point3d object that an entity uses (for example directly obtained from the entity
face.vertices.first.position
) will move with the entity.
A clone/copy of such a Point3d (like point + [0,0,0]) might have initially the same position, but does not show this behavior.If this behavior is documented, then it's fine. If it is decided to cleanup/simplify it by removing that behavior, we could (?) still use a Point3d relative to the entity/group's boundingbox origin, which will move with the entity. Does that make sense, Christina?
-
All points and vectors change, even if you add Geom::Point3d.new to the attribute, not just those that refers to a moved vertex.
I suppose a change in the behavior would affect more plugins than just mine. Also it would break backward compability unless you write separate code for older versions which will be much harder to maintain. Also the bounding box is always aligned with the coordinate axes so rotating entity would break the relation between the bounding box origin and a point relative to the entity.
-
I'm very new at this, so pardon if I'm terribly mistaken, but....
Small, additional quirks worth mentioning:
Directly storing / recovering Point3ds and Vector3ds in hashes will fail via Dan's method, as the inspection string will not have proper constructors for them. This is Ruby-specific, not Sketchup. That, and I'm assuming the recently noted entity-attributes-auto-transformation will not apply, as Sketchup will only see a string stored as the attribute.
The hash creation failure:
point = Geom;;Point3d.new(0,0,0) hash = {;origin => point} hash_str = hash.inspect new_hash = eval(hash_str) # Will fail
hash.inspect will return => {:origin=>Point3d(0, 0, 0)}
If you eval that it will throw the error: #<NoMethodError: undefined method `Point3d' for main:Object>
Simple, quick solution? Store Point3ds and Vector3ds as arrays, of course.
hash = {;origin => point.to_a}
Or, alternatively, if you were dead set on keeping them as Geoms...
hash_str = hash.inspect hash_str.gsub!("Point3d", "Geom;;Point3d.new") # Same for vectors if needed
This will successfully eval to a new hash containing a Point3d (though, as previously mentioned, the point will not auto-transform with the entity it is attached to... I assume).
-
Good point(s) (pun intended!)
hash_str.gsub!("Point3d", "Geom::Point3d.new")
is effectively fixing flawedinspect
methods for API classes. (The hash's method callsinspect
upon it's members.)inspect strings should be evaluable!
.. but someone got lazy, and decided to use
inspect
for specially formatted output. -
I just discovered empty arrays cannot be saved as attributes between sessions in SU 6-2015 (tested on windows 7). However it works just as expected within sessions o.0. Bug report has been submitted.
-
@eneroth3 said:
I just discovered empty arrays cannot be saved as attributes between sessions in SU 6-2015 (tested on windows 7). However it works just as expected within sessions o.0. Bug report has been submitted.
But of course your code can simply sidestep that with:
...get_attribute(dict, key, **[]**)
which will either return any array which thekey
has as its value, OR with your own[]
as its default, e.g. when it is not setup OR empty, and you getnil
otherwise... -
@tig said:
@eneroth3 said:
I just discovered empty arrays cannot be saved as attributes between sessions in SU 6-2015 (tested on windows 7). However it works just as expected within sessions o.0. Bug report has been submitted.
But of course your code can simply sidestep that with:
...get_attribute(dict, key, **[]**)
which will either return any array which thekey
has as its value, OR with your own[]
as its default, e.g. when it is not setup OR empty, and you getnil
otherwise...Yea, it's really easy to work around it once you know about it.
-
@eneroth3 said:
@tig said:
@eneroth3 said:
I just discovered empty arrays cannot be saved as attributes between sessions in SU 6-2015 (tested on windows 7). However it works just as expected within sessions o.0. Bug report has been submitted.
But of course your code can simply sidestep that with:
...get_attribute(dict, key, **[]**)
which will either return any array which thekey
has as its value, OR with your own[]
as its default, e.g. when it is not setup OR empty, and you getnil
otherwise...Yea, it's really easy to work around it once you know about it.
I had run into the empty array problem earlier and I agree 100% with eneroth3. It is not hard to allow for this once you know about it, but there is no documentation anywhere that tells you that you need to. I wonder how many others have independently discovered this...
-
BUT the whole idea is that it always returns 'nil' if it's not set.
Doesn't [] === nil
so then that's as expected ?
When you 'get_' an attribute you should always default it to be the base level result:nil, 0, 0.0, "", [], false
and so on...
Then test for the 'class' of what's returned...
If you don't, then how do you know what's been returned ? -
@tig said:
BUT the whole idea is that it always returns 'nil' if it's not set.
Doesn't [] === nil
so then that's as expected ?
When you 'get_' an attribute you should always default it to be the base level result:nil, 0, 0.0, "", [], false
and so on...
Then test for the 'class' of what's returned...
If you don't, then how do you know what's been returned ?I believe in defensive programming, so don't get me wrong: you should always verify and/or default the return from get_.
nil is what is returned by get_ if you never assigned a value to a particular key and provided no default, but it should not be returned when SketchUp couldn't handle the value you happened to set! Transmuting [] into nil is wrong for multiple reasons. No, []===nil is false. Performing a test such as #empty? in case the Array wasn't populated will throw an Exception because nil is not an instance of the Array class.
Instead of converting [] into nil (or converting any other unsupported class instance to nil) the dictionary should raise an exception when you try to set the illegal value.
-
Storage:
dict["keyname"]= ary.inspect
Retrieval:
ary = eval(dict["keyname"])
It has no problem with an empty array.
There is something in the code of the
set_attribute
method, that will ignore even a string representation such as"[]"
?
Advertisement