Understanding the move an existing group into another group Challenge

I know this may have been covered several times already but, not necessarily in one place and in a “This is the best way to do that” sort of way. Can you help me gather current best practices and strategies about this quirky aspect of the API into one place?

Please forgive me if I have misunderstood or misstated the actual facts below as I am trying to understand this. Ideally, I can update this post to be more accurate and useful for future users if you can let me know what needs to be revised.

Basic Assumptions:

  • You have several existing groups and/or components at the root level in a particular Sketchup model.
  • Within those root-level groups and/or components, there are many more nested groups and/or components, each which may have their own nested groups and/or components, and so on.
  • You would like to search the entire model and copy every group and/or component at all levels of nesting with a certain attribute into a new parent group
  • the new parent group will be created at the root level

It does not matter WHY you want to do this, you just do. Maybe you want a copy of every doorknob in an entire hotel project to be pulled out of their variously nested locations so you can analyze knob surface area… whatever.

Problems (as I understand them):

  • There is no copy method. Most strategies describe a workaround of some sort and @barry_milliken_droid came up with this custom method Copy entities (@last) but I am not sure if this works across contexts.
  • The API is apparently incorrect in its description of the .add_group() method. It can accept “an Entity, a list of Entities, or an array of Entities” as it’s argument. @slbaumgartner Move all entities to new group
  • MOST IMPORTANTLY: Sketchup will Crash when attempting to “copy” the groups and/or components into the new parent group if:

the groups and/or components you want to “copy” already exist in that same context

AND

the context you want to “copy” them to is anything other than Sketchup.active_model.active_entities (basically you can’t have group A open for editing and attempt to “copy” one of it’s child groups into another group B at say the root level.)

The Workaround

It appears that you can workaround the first Crash problem by using .add_instance() instead of add_group() and access the group or component’s definition entities (which do not exist in the active model context).

@eneroth3 : Add an array of entities into an existing group

@TIG: Entities.add_group causes Bugsplat!

The second Crash condition appears to be more tricky to workaround. If you want to “copy” a child group or component to your new parent_group at the root level, you are obviously working across contexts.

What are the best workarounds to this second issue?

Ideally there is a way of “copying out” to the root level but that seems to be a non-starter. What if I created an array of matching groups and/or comps as I probe the various levels of the model and then attempt to create a collection of “handles” to copy all matching entities later while at the root level? This is where I appear to be MOST stuck.

Also the Transformation Problem

I also understand that when working between nested and un-nested contexts, there is an issue with positioning where the transformation of each entity needs to be combined with its parent’s transformation. I wanted to see if I can make headway on the first issue before trying to work through this second one because there is a lot to unpack here (for me at least). :upside_down_face:

Thanks!

This snippet of wisdom by @TIG makes a good bookmark.

1 Like

Please remove the word “Bug” from topic title. It isn’t a bug, it’s the way things are. (Implementation detail.) I would prefer that SketchUp trap such an attempt and not splat however.

Use a word like “challenge” or “workflow” etc.


In the case where you are down in some editing context, and you’d like to return to the root level this can be done with code …

model.close_active while model.active_entities != model.entities

… or …

model.close_active until model.active_entities == model.entities
2 Likes

The bug, as @danrathbun wrote, is that SketchUp fails to trap a condition that is known to cause a crash.

2 Likes

@DanRathbun changed as suggested.

1 Like

added thanks!