Finding all defined Groups in a Model

Is there a way to find all of the defined groups within a model without having to dig down through every group looking for sub-groups?

When my models get large with 100+ wall panels such a method that is not too computationally expensive eludes me.

I don’t care about components I’m only looking for groups at this point.

Once I get the total list or array of groups I then can look through this list and pull out specific ones and act upon them (ie. add them to the selection set).

How do you mean a defined group? Are there groups that are not defined?

Sorry my wording is a little confusing, I just mean existing group or maybe I should just say group. Some groups may be nested deep within others, I’m just trying to grab a list of all groups nested or not.

group_definitions = Sketchup.active_model.definitions.find_all{|e| }


This finds all the definitions. I guess I’m not understanding the difference between the definition and the instance. For a group shouldn’t it be one and the same?

Groups have a ComponentDefinition object, just as ComponentInstances have. The difference is that the definition of a group doesn’t show up in the In Model component list.

As far as I see the # group? method used by @TIG here will find a # definition’s which hold the elements of a group . But will not necessarily give you all the groups belongs to it! (?)

See in # make_unique :
Copying a group using the copy tool in SketchUp will create copies of the group that share a common definition until an instance is edited manually or this method is used. If multiple copies are made, all copies share a definition until all copies are edited manually, or all copies have this method used on them. This method ensures that the group uses a unique definition entry in the drawing database.

Or I’m totally wrong?

To get the group instances, use the instances method on the ComponentDefinition object.

From TIG:

group_definitions = Sketchup.active_model.definitions.find_all{|e| }

’ + I guess something like that:

groups = []
group_definitions.each {|e| groups<< e.instances }


1 Like

This will result in an array of arrays, (ie, e.instances returns an array.)
It can be either iterated or flattened before iterating.

To skip the flattening … you can do …

groups = []
group_definitions.each {|e| groups.concat(e.instances) }

Or …

groups = group_definitions.collect {|e| e.instances }.flatten

Groups are just a special class of component instance.

You may be confused by the group.entities method which is actually a wrapper (shortcut) for group.definition.entities.

As said, group definitions do not appear in the “In Model” component collection list (“Materials” inspector panel,) and group instances are automatically made unique if they aren’t, when manually entered for editing.

I know I’ve pointed you to this instructional thread in the past.
You really should use the bookmarking feature of the forum (or your browser.)

Also, you can ask the admins to unlock your previous topic if need be …

1 Like

If you are looking to collect all group-instances with a certain name then use something like this, I’ve split up the steps for more clarity…

group_definitions = Sketchup.active_model.definitions.find_all{|e| }
group_definitions_by_name_hash = {}
name = "MyLovelyGroup" ### the instance name you desire
  instances = []
    instances << i if == name
  group_definitions_by_name_hash[e] = instances if instances[0]
### we have a hash of all group definitions with arrays of instances
### having a name matching 'name'
### to collect the definitions and instances respectively use
definitions = group_definitions_by_name_hash.keys ### an array
### to process the individual definitions' instances in turn use:
  instances = group_definitions_by_name_hash[d]
  ### do something with the array of instances of that one definition
### also, to process all instances
instances = group_definitions_by_name_hash.values
### an array of arrays of instances matches to name
### either process each array in turn, or if you want to do it en mass...
instances.flatten! ### will combine the various nested arrays into one array...

If you want to match part of a name then substitute:
instances << i if == name
which only matches the name ‘MyLovelyGroup’
use perhaps:
instances << i if =~ /^#{name}/
to collect all groups with a name starting with ‘MyLovelyGroup’ etc.
MyLovelyGroup, MyLovelyGroup#1, MyLovelyGroup#2 etc…

There are many permutations of pattern matching…

1 Like