Google Sketchup Ruby API

Since I am doing some work in Google Sketchup these days using it’s Ruby API, I would like to talk about it a little bit here.

Google Sketchup is a powerful tool that Google acquired couple of years back. Idea behind Sketchup is, that you can learn how to do some pretty interesting 3D modeling in a short period of time. There are a lot of 3D modeling software out there, but probably Sketchup is the one that is the easiest to learn, but at the same time pretty powerful.

In order for users to make their own plugins or extenitions Sketchup provides us with Ruby Sketchup API. As you know API stands for Application Programming Interface, which means that you are given a set of tools or calls that you can make to interact with Sketchup model with Ruby script.

Today we will take a look at a fairly easy Ruby program, which will show us how many elements there are in a model, and how many of them are currently selected.

Before we go into any code I would like you to download a plugin called Web Console made by Jim Foltz. It can be downloaded from this URL:

What Web Console allows us to do is input or load a Ruby script into Skecthup and try it out. Normaly without this plugin, we would have to restart Sketchup every time we made a change to our plugin code to retry it.

So after you installed Web Console plugin you are ready to follow this tutorial. (plugins are saved in plugins folder inside your Google Sketchup application folder).

Now create a new file in your favorite text editor. I am using the free Komodo Edit.

In the first line write the following:

require 'sketchup.rb'

This will always be the first line of any plugin we make for Sketchup. It just ads contents of sketchup.rb file into this file so we can start interacting with Sketchup.

Next we will define a function count_elements, inside this function we will get all elements in our Sketchup model (or entities as they are called in Sketchup), and all selected elements or entities for our model. We will then notify the user about how many entities there are and how many of them are selected.

def count_elements
  model = Sketchup.active_model
  ent = model.entities
  selection = model.selection
  UI.messagebox "Number of elements in this model: #{ent.length} \nNumber of selected elements: #{selection.length}"


Let’s go through this code line by line…since it only has 4 lines of code it will be easy 🙂

First we save active model (that is the model that we currently have on screen) into a variable called model. Now we can store all entities of active model into an array ent (with model.entities). Next we store the selected entities into variable selection.

At the end we want to present a pop up window that will state how many elements there are in our model and how many of them are selected.

Pop ups can be created by UI.messagebox “message we want to show in our pop up”.

Since we are using double quotes and not single ones we can include variables by using construct #{variable here}. We can add new line with \n.

Now save this script with rb extension. You can save it anywhere, since we will activate it with Web Console plugin.

Open Sketchup, either load any 3D model you have or just add some shapes, lines and such and select couple of them. Now open Web Console, click load button at the top of the plugin window, load script you just saved. Go to the script and add one line calling our count_elements function and press Eval. Your Web Console should look like the one below:

The UI message box shown on my loaded model can be seen below:


, , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: