Scripting Sketchup from Ruby environment?

The Sketchup Ruby API can run Ruby scripts from Sketchup Ruby console. Is there a way to invoke Sketchup from within a Ruby console (not the Ruby console in Sketchup) and return a pointer to the Sketchup instance object?

I don’t think so. Sketchup runs it own copy of ruby inside SketchUp.

As Guy said, SketchUp runs it’s own Ruby interpreter and there is no functionality to remote control the API.

You could try with a socket connection, forward commands from the standalone Ruby console to SketchUp. This all require SketchUp running - I’m not sure if you wanted that.

What is it that you are trying to do?

If it’s to use debugging tools for Ruby then we have a solution for that in SU2014:

I am trying to find a script-able 3D modeling engine which serves as the pre-processor for my engineering application software. The 3D modeling is only the first step of the work flow, after the 3D model is built, it will to be processed by other engines, such as meshing, geometrical analysis, electrical analysis, data processing, etc.

To automate the above work flow, it is desirable to have the 3D modeling engine scripted from the Ruby environment. If this is not possible, then Sketchup has to be invoked from command line to run a pre-programmed script, such as,
sketchup.exe -RubyStartup C:\path\to\script.rb

there are some drawbacks when Sketchup is invoked from command line, for what I am trying to achieve; when Sketchup is invoked from command line, it is a process running on its own, without any communication with the invoking Ruby process. This will cause some issues with error processing. I think it is better to have all Ruby scripts running from the same process.

I was able to do something like this in VBScript with another modeling engine. In that case, the VBS program invoked the modeling engine and received a handle to it, then the VBScript built the entire model remotely by using the object handle of the modeling engine, everything was done in the same VBScript process.

If everything you want to do can be called from ruby you can run all tasks from the sketchup ruby. It’s a normal ruby 2.0 with standard library which you can call external programs without a problem, but it just run inside SketchUp.

Some people have been doing things outside of SU - those links are listed under Interesting Examples in this section Resources.

SketchUp Bridge will let you communicate with stuff outside of the SU Ruby environment, an example https://sites.google.com/site/sagesuwiki/tutorials/ruby/dialog-boxes-and-win32api-advanced. Last I tested, you may need to use SU13 to test SU Bridge as the plugin did a good job crashing SU14 at startup.

There is no way to use the Ruby API outside of the SketchUp process. Using a socket for communication or similar would be the only way if SketchUp is only a part of your whole process.

Only other alternative we have is the C API, but, then it’s not scripting any more. Not sure how much you need to do in SketchUp, but you could write a Ruby C extension for your standalone Ruby script that creates the model using the C API. Then you don’t need to start SketchUp.exe or anything.

http://www.sketchup.com/intl/en/developer/sdk_start.html

Other analytical efforts using SU are listed here Resources.

when communicating/controlling SKetchup from externally, it doesn’t have to be Ruby. Socket appears to be a useful way to communicate with Sketchup from other standalone applications, provided it works reliably. I appreciate the information.

There are quite a number of online posts on Sketchup socket programming, with mixed results. The 2012 thesis by Mitchell Hedges provides detailed description on this topic. The thesis title is"Controlling distributed audio from a 3D Graphical User Interface (GUI)"

It is very encouraging to see that others are trying to do the same as I do, using Sketchup as the front-end 3D modeling tool for engineering simulations. Thanks for the useful pointers and it is definitely very interesting to follow some of these links.

Before SketchUp 2014 SketchUp shipped only with Ruby Core - not the StdLib.
However, I’ve not personally tried the socket class, but we have a Ruby C Extension example set up with Visual Studio and Xcode projects and the Ruby libs and headers we use in SketchUp:

That’s a nice boiler plate to hook up any C/C++ libs you might need.

A search in my Sketchup 2014 installation directory and plugin directory for ruby.h returned nothing. Found some of your old posts on this topic dating back to 2011. Is there any documentation on Ruby C Extension, is it something “officially” supported by the development team?

SketchUp doesn’t ship with the headers out of the box.
The Ruby C extensions are a feature of Ruby itself - not something SketchUp has made. We packaged that GitHub repo to provide build setups that match how we build and ship Ruby.

There are some serious limitations to run everything from Sketchup embedded Ruby API for this type of applications. I wonder what would it take to expose the Sketchup Ruby API to external processes. That would make a huge difference for this type of applications using Sketchup as the 3D modeler.

There are a fair numbers of posts online discussing the “external API” for Sketchup, possible solutions including sockets, bridges. But they all appear to be some sort of workarounds rather than robust solutions with supporting docs.

That’s new to me. Missed that completely. If you have links I can file a new feature request, but having concrete examples of use-cases would help here. The more the merrier.

Do you have some references to how other software packages dot this?

some links to “external API” related posts:
(1) Redirecting to Google Groups
(2) IPC with Ruby 1.8.5 - Stack Overflow
(3) Redirecting to Google Groups
(4) Redirecting to Google Groups
(5) Redirecting to Google Groups
(6) Redirecting to Google Groups

given the popularity of Sketchup, the demand for a robust and well-documented external API is definitely out there.

Some info on use case:
There are a large number of engineering applications that use the following 3-step workflow to solve engineering problems.
(1) step 1 (pre-processing): create a project, containing 3D models, data files, requirements, documents, etc. Then build the 3D model in a GUI environment, save the model in a file
(2) step 2 (simulation), configure the simulation engine with parameters, etc. and run the simulation
(3) step 3 (post-processing) process the results

In this process, Sketchup can play an important role in building the 3D model (step 1). It is very time-consuming to build a 3D GUI interface for modeling of engineering problems. Sketchup is a great platform to achieve that goal.

However, it is neither desirable nor necessary to expect the entire process being done in Sketchup. What needs to be done, is to “bind” Sketchup with the main application through a robust, well-documented external API. It could be C++ or Ruby or any other suitable language (many engineering applications use Python, VBScript, VBA, etc.)

There are a multitude of benefits for providing such an “external API”:
(1) open the doors for Sketchup to become the preferred 3D modeling engine for a large number of engineering applications, avoid re-inventing the wheels for each application
(2) GUI consistency across multiple programs, users do not have to re-train and re-learn the 3D GUI of every application
(3) reduce the cost and development time of engineering tools
(4) end users of engineering applications are likely to have the budget and willingness to pay for tools they use to do their jobs; increased revenue potential for Sketchup

NREL OpenStudio https://openstudio.nrel.gov/ is an example to use Sketchup as the front-end 3D modeler for building energy consumption analysis.

adding a note to this, Ruby C extension is not an “external API” of Sketchup, it is an extension to the embedded (internal) Ruby environment.

The Sketchup SDK is an external C API, but it mainly works with file import and export without access to Sketchup classes and objects.

Without access to Sketchup classes and objects? What do you mean by that? It exposes wrappers around our internal C++ objects and data structures. What additions would make it fit for your need?

You are right. The Sketchup SDK does indeed expose internal objects and data structures. That is what developers need to integrate with other applications.

Now the potential feature enhancement is to expose the Ruby API externally, so that end users can access Sketchup from their external Ruby console or script, rather than running embedded Ruby from Sketchup.