A Toggle Viewer

Having used Sketchup as a hobbyist for a number of years I finally decided I needed to get to grips with Ruby and the API. I learn best by setting myself a challenging but do-able task so I decided to try and build a plugin to toggle between design options.

Sure you can do this with Layers but you have to turn the current layer visibility off then turn the new layer on for each change of design. You’ll also need to add a prefix to the layer name to keep the options grouped in the list. You may also have other items on one of the layers which you don’t want to hide. You can do it with Scenes but Scenes are primarily about storing views and you’ll either have to forego this facility or have the same set of scenes for each view option. With, say, five different scene views and four different designs you have twenty scene tabs to manage and sequence. I chose to add the designs to a common group and toggle one of this group to be visible at any one time.

To use the plugin:

  • select the different design options and add them as groups (or components) to the Toggle Group using the Plus button in the menu bar
  • use the First, Previous, Next, or Last buttons to shift between options. Next and Previous wrap around like a slide carousel (ask your dad)

When you’re done:

  • click on the Minus button to explode the Toggle Group back to its component groups or components

That’s it.

Here’s a video showing the idea.


… and here’s the extension (version 0.2)

ptb_toggler.rbz (146.0 KB)

I would welcome any comments on whether it is felt useful, any ideas for improvement, or my coding.

[Confession time: Coming from a Swift/C# background I thought Ruby would be a doddle. I initially went down the route of turning a selection of the checkboxes in the Layers tray into pseudo radiobuttons. Layers were added to the selection by manually adding a prefix to the layer name. It worked OK and I learnt a lot. Then I had the idea of adding a dialog to allow the user to select toggle layers from a multiselect-dropdown. This led me down this track: Sketchup API => Ruby => HTML => Bootstrap => Javascript => jQuery then a blizzard of things with odd names like Skippy and Grunt. Not much fun. Finally I hit on the obvious idea of using a group as the container and had something up and running within a couple of hours. So long Skippy; goodbye Grunt.]


Diving directly into HTML+CSS+JS if you are all new to this can be daunting when you only want a simple dialog.

Did you have a look at the HtmlDialog Inputbox example?

That lets you create a simple dialog box using only a Ruby interface if you are ok with what that example already offer.

I did look at the HtmlDialog Inputbox but what I really wanted was a multi-select dropdown and chose the one by David Stutz. My interface as a result looked like this (I never got round to prettying the layout).


I exaggerated the pain somewhat in my post as I like learning new stuff. HTML and Bootstrap is really one topic and Javascript isn’t that far removed from C#. What troubled me most was when I hit an install problem with plugin code and the answer was nearly always ‘open a terminal window and type sudo followed by an alphabet soup of unix commands’.

The good thing about following this path was it forced me in the end to choose a much simpler solution.

This is a really cool extension! The principle is simple, but the best realization is the challenge!

I know a lot of users (@g.h.hubers ) who wanted to have something where they switch visibility of objects like the Google Earth terrain/snapshot layer button:
Instead of needing two or more clicks to hide a layer and show another layer, the user could do both simultanously in one step. This is very useful for presentations (toggle between alternative states of a house).

I was struggling with putting this into a minimal UI, because (unlike the Google Earth terrain/snapshot) a user could want an arbitrary number of separate configurations (e.g. roofs, garages) with an arbitrary number of states (roof 1, roof 2, roof 3). This is not easy to realize with SketchUp’s toolbar buttons which have at most two states (enabled/disabled without changeable icons) and one cann not efficiently add/remove buttons (for separate configurations).

I like how your approach solves this with forward/backward navigation through the list of states.

By the way, for SketchUp development you don’t really need to open a terminal (apart from the Ruby Console or Ruby Console+). For the scope of a small, simple dialog (single-page app with a single view) it is absolutely fine to focus on vanilla HTML and JavaScript.

“Modern” JavaScript tools can be a bit intimidating and confusing because of their fancy names and intransparent purpose, often one tool requires (or integrates with) another. CLI tools, automation tools like grunt/gulp, transpilers, GUI toolkits become useful once your plugin grows into a more complex app and you feel you need tools that support you handling complexity and maintainability.


How right you are!
With large chunks of your model grouped and a meaningful layer assigned to “that chunk” group, plus having several of such groups, you could switch rapidly between groups to model in different environments(as in different groups/models/worlds). All you need is one or several group switches as icons, like the “Toggle Terrain” button.

This is quite impressive for being a first extension.

One minor thing, the file that lies directly in the plugin folder (ptb_toggler.rb) is often referred to as the loader file. I’d rename loader.rb to e.g. main.rb to avoid any confusion.

Many thanks to you (and Aerilius) for your kind comments and for putting me right about the loader file. I’ve corrected this as well as removing some debug lines and an obsolete EntitiesObserver class that I hadn’t spotted. Code has been corrected in original post.

Which brings me to this question. If the extension is felt to be useful what is the best way of making it available. I don’t really want to put it on the Extension Warehouse with all the support obligations that that seems to entail. I guess Github is one way to go. Any other suggestions?

This looks great! I’ve wanted a better way to deal with managing design option presentation and thought that an options toggle would be the right approach.

Why not on the Sketchucation Plugin Store?

Thanks for the suggestion. I’ll do just that.

Whoops! My apologies.

In my haste to clean out the debug stuff I managed to leave in a reference to the recently deleted EntitiesObserver. This stopped things working. Code has been corrected in original post.

Actually incorrect. It is the extension registrar file …

http://ruby.sketchup.com/Sketchup/ExtensionsManager.html says …

The ExtensionsManager class provides a way of accessing the SketchupExtensions that have been registered via the Sketchup.register_extension method.

… and by EW rule must have the same name as the author’s extension subfolder.

The loader file is what the SketchUpExtension instance points at (via it’s @path variable) in the extension’s subfolder, and is loaded via the instance’s internal load() method. (Ie, it is the pathname passed as the 2nd argument to the SketchUpExtension constructor method.)

Many author’s such as ThomThom name it some variation ending in “main”.
I myself usually used the “loader” suffix because it basically loads all the other file’s for the extension, and by personal convention I trap any LoadErrors that occur in this file.

If the user has switched off the extension, then the ExtensionManager never loads the loader file, but it always loads the registrar scripts in the “Plugins” folder proper. *

* Unless some other manager like SketchUcation’s PluginStore renames them from .rb to .rb! or whatever so as to hide them from SketchUp.

It is preferable that there be only 1 version available in the initial post (kept up to date) rather than several of differing versions posted throughout the thread. (It’s okay to add an announcement of a new version but users normally go back to the top of the thread to get the latest.)

1 Like

Thanks for the hint Dan. I’ve removed the superfluous links and updated the one in the original post.


Great idea! This is very useful and commonly used functionality

Very handy little piece of programming. Thanks for your good work

I think it’s very useful. Great job. Congrats.

Many thanks for all your encouraging comments.

Soon after I posted Toggler I was using it in earnest and I discovered a major limitation. Sure the ability to Shift-Click or swipe-select to select optional designs and then add them with a single click on the Plus menu button is great but it means that each Group/Component chosen has to be an orphan. If one or more has a parent (and, more importantly, siblings) you can’t multi-select in this way.

So I rewrote the code storing the options in an array rather than a Sketchup Group. All worked well (and the lines of code count dropped significantly) until I then realised that the merit of using a Group was that the toggle selection was stored with the model and hence would persist across sessions.

That led me down the path of using a Model.AttributeDictionary to save the persistent_ids of the selection in a dictionary. That in turn required using an AppObserver to respond when a model was loaded and hence load the dictionary. All good stuff if you’re learning Ruby and the API.

But finally I realised that any model with Scenes would now overwrite the visibility of my toggle ‘group’. I looked for a SceneObserver/PageObserver and read the recent postings on this topic in the forum but nothing seemed to work. Despondent, I tried a FrameChangeObserver and that worked. Initially I was worried about saving my selection with every frame of a Scene change but I discovered that a check for percent_done == 0 meant a single save per Scene change worked.

Updated extension - version 0.2 - is now in the original post.

Some improvements added in passing:

  • sounds now offer some feedback on successfully adding Group/Components to the toggle group (or signify failure on trying to add a duplicate)
  • clicking on the Explode button a second time offers the opportunity to Unexplode a toggle group if the user has hit this button mistakenly

File size has risen (largely due to the sound files) but I guess that’s the trade-off - keep it simple but limited or do it properly and accept the hike in bytes.

maybe you should add a preference to ‘Mute’ the sounds…

they really annoy some people, particularly in office environments…


Ok, I’ll give that some thought. It seems a bit ‘extravagant’ to add a Setup button to the toolbar with just one option! The sounds only occur on initially defining the Toggle group and on subsequently exploding it so during a presentation using the navigation buttons the extension is silent.

On my mac I just hit F10 to mute all sounds.