Code a SketchUp layer/scene script with Ruby!

Updated: Apr 16, 2020

In this tutorial, we're writing a SketchUp script that will automatically create scenes with the names of every layer in your model. The best part is that entities that belong to a specific layer will only be visible in the scene that matches that layer's name.

The instructions on how to run the script with the Ruby Code Editor Plugin is in my first tutorial.

But, If you like LayerScene and want to install it as a SketchUp Extension/Plugin which includes a toolbar button, then you can buy it for a small fee in the link below.

Buy Here

Before explaining what each line of code does, let me recommend the following book so you can better understand the Ruby syntax.

Programming Ruby 1.9 & 2.0

Click the link and get a copy of this book, it will give you the knowledge to start coding with Ruby which is the language that SketchUp uses to create Extensions/Plugins.

Let's Begin!

When you're starting to prototype ideas it is best to start small and at this point, 14 lines of code were all we needed to prove our concept. The following is a breakdown of the logic behind each line of code and the API calls used in the script...Enjoy!

With the first line of code, we gather data from our current SketchUp model and store it as an object in a variable named model. From there we store entities, pages, layers and current layer in their own variables so we can use them later.

API: Model , Entities , Pages , Layes , Model #activelayer

To start making the scenes using the layers name we have to start iterating through each SketchUp layer.

API: Layers #each

We don't have to make another variable to store the current layer being iterated but it reads a little better when using it on line 11 at the end.

API: Layer

Iterate through each page and erase the ones that share the same name as the layers.

API: Pages #each , Pages #erase , Layer #name , Page #name

Add new page with active layer name.

API: Pages #add , Layer #name

Hide all entiies if the layer they belong to is not the active layer.

API: Entities #each , Drawingelement #hidden , Drawingelement #layer

Update the selected page before creating a new one.

API: Pages #selected_page , Page #update

Unhide every entity before repeating the process.

API: Entities #each , Drawingelement #hidden


I hope this tutorial was useful and helped you see that making tools for SketchUp can be easier than you initially thought. There is still more ground to cover so please subscribe to the website to receive email notifications about updates, tutorials, deals and much more. Also, tell me if you have an idea for a script you want me to try to solve together. See you next time for my next blog post tutorial!

294 views0 comments
  • Twitter
  • Facebook
  • Facebook
  • Instagram
  • YouTube