Possible to call call existing Sketchup functions from a Script

Hello Everyone!

Do you know if there’s a way to call an existing Sketchup function from a Ruby Script? For example, if you context-click a Component that’s been scaled and rotated, one of the commands is “Scale Definition”, or if you select a Face, one of the context menu commands is Select -> All with same Material.

As far as I can tell, the Ruby API doesn’t have methods that can do these from a script, so I find myself having to ‘reverse engineer’ through Ruby a process that’s readily available in Sketchup… but I feel it’s silly for a novice like myself to clunkily reproduce what Sketchup does so effectively (that is, if I can even get that far). Am I missing something?

The SketchUp Ruby APi doesn’t have a 1:1 exact mapping of UI actions to API actions. This is because the API is more generic and flexible.

The API can do both of the things you describe, but not as a single line or method call.
Writing a couple of lines for what you describe is normal.

I’m guessing you are looking at automating tasks? And you are looking to take the UI actions you perform and translate to the API?

That’s one use of our API. But people also, and probably more often, want to extend SketchUp with things you cannot do out of the box. That’s why the API is more generic and doesn’t map 1:1 with the UI commands. But you should be able to perform most things.

If you are stuck at a particular operation you can do in the UI, just ask here in the forum and you’ll probably get a very prompt response with proposed solutions.

1 Like

The SketchUp Ruby API was knowingly implemented as a programming facility, not as a macro facility (by which I mean a scripting system to automate sequences of user actions in the GUI). So, as @tt_su wrote, in most situations it provides access to the underlying steps rather than to a complete GUI action or tool. It’s worth noting that the GUI is always “in control” when the user invokes an action, which assures a certain amount of knowledge about the current state of the app. That isn’t always true in the midst of a Ruby program, so invoking a GUI command from Ruby could produce strange results.

2 Likes

Thanks for the quick replies!

Indeed, my scripts are generally used for automating workflow, and sometimes all I want is to duplicate a UI command, but with one extra script-able step (i.e. applying “Scale Definition” iteratively over several objects).

Makes sense when you put it like that.

So far, I’ve been more or less successful in bungling my way through reproducing the UI commands - perhaps a wishlist item (or maybe a side project of my own) could be a reference page or wiki with a number of UI commands, and how they would translate basically into the Ruby API… Food for thought anyways.

Also many of the GUI commands can be initiated via the Sketchup::send_action module function.

There are many other action strings that are not listed on the API doc page.
Many more are posted over at SketchUcation in this thread: Sketchup.send_action() : Arguments to


Taking one of your examples …

The GUI only allows you to select an entity in the current editing context for an “All by Same Material” selection, but via the API, we can write a method that does not have this limitation. We can reference any entity in the model to use it’s material as the filter for the current context.

def select_all_with_same_material(ent)
  # If ent is not in the current active entities context
  # it will not be in the resultant selection set.
  if ent.material
    matl = ent.material
    kind = ent.class
    sel = Sketchup.active_model.selection
    ents = Sketchup.active_model.active_entities
    sel.clear
    sel.add( ents.grep(kind).find_all {|e| e.material == matl } )
  end
end

Also, the example ignores “nil” setting for material like the GUI command does, but you could write it to select all certain entities that do not have a material assigned (ie, they property is nil.)

Personally I’ve been wanting to see a library on top of the API. (Think how Ruby do StdLib on top of their core.) For my own extensions I had my TT-Library to re-use common snippets I kept using: https://bitbucket.org/thomthom/tt-library-2

The big drawback with tt-lib is that it needs to be installed separately and SketchUp and Extension Warehouse doesn’t support such bundling.

Recently I’ve been playing with a light-weight dependency manager (personal project) that can pull in external libraries into the extension’s namespace and bundle with the extension. With such a platform it would be feasible to start some kind of “standard SU library” - open sourced ideally.

1 Like

Neat… I was totally unaware of that option. Conceptually, that’s probably as close as it gets to what I was thinking of in the initial post.

Two more thoughts, …

You can assign a shortcut to context menu items, by putting SketchUp into that editing context (ex, select a face so that the context menu is loaded) and then go to Preferences > Shortcuts and you’ll see the context items populated under “Edit”.

The other is that Thomas has already written a very nifty Selection Toys extension that has all the selection filters you’d ever want. And you should be able to assign shortcuts as mentioned above.

I don’t if he’s coded a public interface for other extension authors into it. @tt_su ?

Regarding your library ThomThom, I’m quite familiar with it, seeing as how I use your extensions to make my life easier on a daily basis (my company really should be sending you cases of cookies with the number of manhours we’re saving).

Methinks the dependency manager project you’re working on has potential, being able to pull in specific parts of an extension. Point in case, I’ve tried to develop a script that would draw simple siding trim on the corners of a house, with a thickness to the material. The basic push/pull wasn’t enough for the beveling and other complex geometry, so I looked at your “Shell” extension to try and come up with a solution.

When push-pull does not meet the need, perhaps try followme along a path.


I think Profile Builder was created to do trim originally.

https://extensions.sketchup.com/en/content/profile-builder-2

No - not for Selection Toys. But most of my extensions should have an MIT license so it’s fine to grab and use the source. (If I haven’t added it to the repo, just ping me and I’ll add it.

1 Like

Yea, this keeps coming up more and more as devs try to leverage what each other have made available. A delivery system is needed to make this efficient.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.