How to find parent group in a model?

In SketchUp Pro 2015, I’ve got a two ComponentInstances, contained within a group. When selecting a ComponentInstance, I want to be able to go up the heirarchy, looking at the parent groups. However, entity#parent is not working as I’d expect:


As you can see above, the parent of a ComponentInstance gives me it’s definition instead of it’s parent. Is there another method for getting the container object?

Unfortunately, no simple way. SketchUp keeps track of outside-to-inside nesting because that’s what it needs to draw Entities, but not inside-to-outside, which the engine doesn’t need. Various people have tackled this problem, though none of the solutions are a simple one-liner. Perhaps one of them will chime in…

Because definitions have entities collections, and instances do not.

What may be throwing you off (a bit) here, is that the Group class has an API “shortcut” method that is basically:

def entities()

This is because groups are really “special” component instances, whose definitions are hidden from the listing in the Component Manager.
So groups (being instances) do not really own entities either. It is their definitions that “own” the entities collection.

Secondly, you are likely confounding two separate API methods of the same name. Just because they both use the name “parent” does not mean they do the same thing. (There is, at this time, at least 4 different “parent” methods in the API.)

So calling the parent upon an instance of the Sketchup::Entities collection class, can return different object references than calling the same method upon a Sketchup::Entity subclass instance. Likewise if called upon a Sketchup::AttributeDictionary object (which always returns an instance of a Sketchup::AttributeDictionaries collection.)

All that being said, the easiest way to get the instance chain, is when the bottom instance is the active editing context.

Then you can use Sketchup::Model#active_path

But yet there is no way to (easily) set the current editing context in Ruby. The API defines a method to exit or back out of editing contexts, but not enter them.

So I believe that hacks are done .

You save the current selection, and the active context. Then use a system call to cause (simulate) a mouse click upon the object you want until the model.active_entities == target.definition.entities then get the active editing path from the method described above.

Afterward you do an undo n number of times, or back out of the editing context and restore the previous one.

This may provide some ideas. There may be better methods, but I just map out the entire model.

This maps the entire model into an array of Instance Nodes which have a reference the ComponentInstance and its path (or parent Instances). From the path you can get to the parent instance, and also calculate the instance’s transformation in world coordinates.

Use a Hash instead of an Array for faster lookups. I included the Instance name to be able to find instances by name. You can include whatever data about the instances you need.

Example of a simple Node array:

  @name="Child 1",
  @name="Grand Child 1",
  @name="Child 2",

1 Like

In the parent instances array will be a component definition that is the group. The component definition will have the method group? set true. Since this is a top down lost of parents, the last item in the list should be the parent group if one is available.

group = entity.parent.instances[-1] if entity && entity.parent.typename == "ComponentDefinition" &&

Should return the group, or nil if it isn’t in a group. Remove the if you want the parent component instead.


g = e.parent.instances[-1] if e && e.parent.typename == "ComponentDefinition" &&


Side-note: avoid using .typename - it’s very slow. Prefer .is_a? instead.

1 Like

Cool Thanks