SU2020: how to keep reference to an object even if its hidden on a layer?

I created a custom plugin to hide/unhide nested objects on selected pages. I select groups / components and run the plugin which loops the pages and hides the selected object(s) on the pages you selected in the dialog.

the selection in the code is created by: sel = model.selection

Problem with the new SU2020 is; sel looses reference to an object (reset to 0 if only selected one at the start) if the layer the selected object on is hidden on a page.

So my question is: how can I keep reference to a hidden nested object ?
Pre 2020 it worked fine.

The selection is supposed to only be able to select objects from the same geometric context. So any nested objects are sort of selected by default.

Assign a reference. It doesn’t matter to Ruby if SketchUp is hiding it from the view.
The object still exists in Ruby. Just hold a persistent reference.

Create a hash or an array and copy the references from the selection collection to your collection …

@selected = model.selection.to_a

… or …

@selected = {} # a hash whose keys are scenes
@selected[scene]= model.selection.to_a

… or …

@selected[scene]= model.selection.to_a.find_all do |object|
  object.visible?
end

Dan to the rescue. Thanks, just adding .to_a fixed it.
Strange though because you would expect sel = model.selection already is a reference or it is but just not persistent?

Just for testing; create something, group it and put it on a new layer. Select the group. Using this code it results to 1.

model = Sketchup.active_model
sel = model.selection
sel.count

Now hide the layer.

sel.count

results to 0

It is both a reference and it references a persistent object. Ie, model.selection returns the reference to the model’s singleton Sketchup::Selection collection object.
So when that collection changes you see the changes as your reference is still pointing at the one and only Sketchup::Selection collection for the model.

When you use .to_a you make an Array class copy that is a “snapshot” at the time that you reference this array copy.

Cannot do this as layers are not geometric collections. We cannot “put” objects “on” layers. This is why their name was changed to “tag”, because in reality you are just tagging objects to use a shared set of display properties.

1 Like

BTW, if you need to save these references from session to session, you’ll need to either attach attribute dictionaries to each entity (such as each scene) or 1 dictionary attached to the model that uses persistent_id.
Then when a model is loaded you’d need to read the dictionary and reset the scenario.

Another factor is that the model’s Selection is dynamically managed by the GUI to be consistent with what is selected in the GUI. Associating an entity with a non-visible layer/tag or changing the object’s current layer/tag to one that is non-visible forces the GUI to remove that entity from the model’s Selection because you can’t select an object that uses a non-visible layer/tag.

2 Likes

thanks @DanRathbun and @slbaumgartner for the additional info

1 Like