Latest news
SketchUp Ruby API Guide: Managing Models, Entities, and ComponentsKarma XPU Guide: Solaris Rendering Workflows in Houdini H20Unreal Engine PCG Framework guide: procedural content generation with nodesMemphis Art Museum to Open on December 6 in Herzog & de Meuron DesignBlender EEVEE Ray Tracing Guide: Settings for Interior ScenesCamera Tracking in Blender: A Guide to 3D Solving and Track CleanupA Guide to Color-Correcting Architectural Renders with Blender CompositorCreating Architectural Materials with Blender Principled BSDF: A Step-by-Step GuideArchitectural Image Generation with Nano Banana 2: A Step-by-Step Prompt GuideAdding Linework and a Technical Look to Architectural Renders with Blender Freestyle
Tips & GuidesSketchUp

SketchUp Ruby API Guide: Managing Models, Entities, and Components

The SketchUp Ruby API lets you inspect and modify geometry, selections, and component instances in the open model through code. This guide explains key classes in the official documentation and how to work safely.

Bright design studio with an architectural model and material samplesAI image
Representative image, generated with AI.Image: 3dsınıfı / FCA AI

In brief

  1. Sketchup.active_model provides access to the currently open SketchUp model.
  2. model.entities can be used to access the root model contents, while model.active_entities accesses objects in the active editing context.
  3. entityID does not persist between sessions; persistent_id is supported only for certain entity types.
  4. A ComponentInstance provides access to the component definition it uses and its transformation in the model.

The SketchUp Ruby API provides tools for accessing model objects and working with them through code. SketchUp’s official Model, Entity, and ComponentInstance documentation describes the core structure, from model collections and selections to component instances and entity attributes. The class pages list SketchUp 6.0 as the version, but this does not mean every method was available in that release. Check each method’s version notes separately as well.

1. Identify the model and editing context

The first step is to choose which model the script will operate on. Sketchup.active_model provides access to the model the user has open. From this object, you can access resources such as the root geometry collection, component definitions, materials, and current selection. This gives the script a single starting point for working with the model.

Decide up front whether you want to target objects throughout the model or only those inside the group or component the user is currently editing. model.entities returns the model’s root collection. model.active_entities returns the entities in the open group or component being edited. If no such editing context is open, active_entities returns the same collection as entities. This distinction is especially important when a script runs while the user is editing a group within the model.

ruby
model = Sketchup.active_model
entities = model.active_entities
selection = model.selection

These initial variables let you handle the active editing context and the selection separately. If you want to target objects at the model root, set the entities variable to model.entities. If the script should operate only on selected objects, use the selection collection; if objects outside the selection should also be included, consider the relevant entity collection separately.

2. Inspect the selection by type

model.selection contains the entities the user has selected. Don’t assume the selection contains only one type of object: different items, such as faces, edges, or groups, may be selected together. The Entity class provides common methods for these objects, while is_a? or typename can be used to check an object’s type. For example, to count only faces, you can iterate over the selection and check whether each item is a Sketchup::Face.

ruby
selection = Sketchup.active_model.selection
face_count = 0

selection.each do |entity|
  face_count += 1 if entity.is_a?(Sketchup::Face)
end

puts "Seçimde #{face_count} yüz var."

Checking types helps prevent a script from performing an unsuitable operation on the wrong object. typename returns the type as text; if you’re working with a specific class, checking the class directly is a more straightforward approach. The Entity documentation also demonstrates using is_a? to identify an object’s type.

Don’t confuse the selection collection with all the model’s contents. The selection is limited to the items the user has selected; entity collections provide access to objects within a specified context. Clarifying the script’s purpose in advance helps reduce the risk of running it in the wrong context.

3. Distinguish a component instance from its definition

A component instance is an object that uses a component definition in the model at a particular position and transformation. The definition method on ComponentInstance returns the definition associated with the instance. transformation returns the instance’s transformation in the model. Don’t treat the placed instance as though it were the same thing as the content that defines it.

ruby
model = Sketchup.active_model
instance = model.entities.grep(Sketchup::ComponentInstance).first

if instance
  definition = instance.definition
  transform = instance.transformation
end

This example searches for a component instance in the model’s root entity collection. If you want to target an instance inside an open group or component, direct the search to the model.active_entities collection. When another definition is assigned with definition=, the instance’s existing transformation is preserved. make_unique creates a definition for the instance that is not used by other instances; explode converts the component instance into separate entities. Since these methods can change the model structure, determine the intended result before adding them to an automated workflow.

4. Handle IDs and attributes carefully

entityID provides a unique ID for an entity, but the value is not persistent between sessions. So, entityID is not suitable for storing as a permanent key for a reference you’ll reuse later. persistent_id is intended for persistence, but the official documentation states that it is supported only for certain entity types. If your workflow relies on this method, check whether the target entity type supports it.

Attribute dictionaries and key-value attributes can be used to attach additional information to entities. set_attribute writes a value, while get_attribute reads one. If the requested key does not exist, get_attribute returns nil by default. If you provide an optional default value, it returns that value instead; it does not create the attribute automatically. A missing dictionary can be created by using the creation option in attribute_dictionary. Methods for deleting attributes are also available; the documentation also describes deletion restrictions for certain special dictionaries.

Remember to open and close operations that modify the model. start_operation begins an undoable operation; commit_operation completes it, while abort_operation cancels the operation in progress. To avoid leaving an operation open after an error, you can use the abort method in a rescue flow. The official documentation warns against aborting a transparent operation because this can also abort the operation it is chained to.

ruby
model = Sketchup.active_model

model.start_operation("Örnek geometri ekle")
begin
  model.entities.add_line([0, 0, 0], [500, 500, 0])
  model.commit_operation
rescue
  model.abort_operation
  raise
end

This example adds a new line and completes the operation successfully. If an error occurs, the operation is aborted and the error is raised again. In a real plugin, error handling should be designed around the change being made and whether the operation is transparent.

Use in architecture and archviz workflows

This API structure can help architecture and visualization teams automate tasks such as checking selected faces, targeting objects in the active editing context, inspecting component instances, or attaching additional data to model objects. Before scripting repetitive model checks, define the scope of the operation: the selection, the open group, or the model root?

Before sharing a script within an office, check the SketchUp version in use and the version information for every method being called. Although the Model and Entity pages list SketchUp 6.0 as the class version, some methods have different version notes. Also, don’t treat an entity ID as permanent, and account for the fact that exploding a component changes the model structure. The source documentation does not provide hardware requirements or licensing terms, so consult the product documentation separately for those details.

Common mistakes

  • Using the model’s root collection while overlooking the user’s editing context inside an open group.

  • Assuming that every object in the selection is the same type.

  • Storing an entityID value as though it were valid between sessions.

  • Assuming that persistent_id is supported for every entity type.

  • Assuming that get_attribute automatically creates an attribute that doesn’t exist.

  • Starting an operation and forgetting to close it on success or failure.

These checks make it clearer which objects scripts written with the SketchUp Ruby API affect and help manage model changes more carefully.

Sources

3 sources
R
ruby.sketchup.comruby.sketchup.com/Sketchup/Model.html
Summary
R
ruby.sketchup.comruby.sketchup.com/Sketchup/Entity.html
Summary
R
ruby.sketchup.comruby.sketchup.com/Sketchup/ComponentInstance.html
Summary

Source texts are not republished; short quotes are marked, everything else is our own summary and commentary.

3dsınıfı’s take
3dEditor’s assessment

For architecture offices and visualization teams in Turkey, the practical value of this API is that it can move repetitive model checks and edits into scripts. Getting the distinctions between selections, components, and entity IDs right in particular can reduce the risk of automation running in the wrong context.

The documentation does not provide information about hardware requirements or licensing costs, so it is not enough on its own for an investment assessment. Offices should first test the required methods in the SketchUp version they use, check the active editing context, and make sure the model is not left in an open operation state when errors occur.

Frequently asked questions

How do you access the active model in the SketchUp Ruby API?

`Sketchup.active_model` provides access to the currently open model. From the model object, you can access collections such as entities, the selection, and component definitions.

What is the difference between entities and active_entities in the SketchUp Ruby API?

`entities` returns the model’s root collection. `active_entities` works with the collection in the open editing context; if no group or component is being edited, it corresponds to the model’s root collection.

Does a SketchUp entityID persist between sessions?

No. According to the official Entity documentation, `entityID` does not persist between sessions. `persistent_id` is supported for some entity types.

Comments and the forum are in Turkish.Join the discussion
+

Related news