Open headless Sketchup instance / process from extension code?

Hello,

I have understood from reading the forum that it is possible to open through code an arbitrary file (maybe some component copy or temp file) with another Sketchup instance. But is it possible to process this file with another Sketchup instance without it using the application GUI? What it could be called a headless state?

I have also understood that it is possible to save the current file state, and open a different one in the instance where the extension is loaded.

Thanks

Although a SketchUp Ruby script can perform a series of actions without user interaction, I don’t think there is any way to launch SketchUp without its GUI nor to open a SketchUp file without it becoming the active model. By issuing a system command via Ruby you can launch a second instance of SketchUp with a specified model open, but it will still have the GUI.

1 Like

FYI, you can actually do this in the same instance, … but using the C API as part of a Ruby extension.

It is true (using Ruby) that you could launch another SketchUp instance passing a name of a Ruby script to run which does the processing, saves the file and quits, … but SketchUp (on Windows at least,) grabs the focus when it loads. I am not sure if the previous SketchUp instance would regain the focus afterward.

So I don’t really know what it would gain you. You might just as well (on Windows) close the current model, process the temporary one, and then reopen the previous model.

On Mac, it has a multi-document interface, so the other model model can be opened alongside the current one, and afterward closed making the previous model active again.

1 Like

Thank you.
Yes, I imagined that you could close the model and work on the second one, and then go back to the previous one while staying in the same instance.
What I was not sure about is whether the C API (or the Ruby API) allowed to copy entities to another Sketchup file without actually visualizing them and work on this copy while still showing the original model unchanged. It seems that this is possible (provided that one has the will to work with the Sketchup C API).

Oh … copying entities deserves it’s own topic thread(s) … which there are quite a few:

https://forums.sketchup.com/search?q=copy%20%23developers:ruby-api

The easiest means using Ruby (click to expand) ...

The easiest means (using Ruby) would be to copy the entities to the clipboard, open the new file, and paste from the clipboard. So basically …

def copy_to(filepath)
  model = Sketchup.active_model
  ents  = model.selection.to_a
  if ents.empty?
    UI.messagebox("Nothing selected!")
    return
  end
  Sketchup.send_action("copy:")
  Sketchup.open_file(filepath)
  Sketchup.send_action("paste:")
end

The trick might be in determining when the new model is ready for pasting via an AppObserver, if the open_file method returns before the target entities collection is ready for pasting.

I myself do not at all care for C. Too much typing and low level housekeeping needed.

What I’m getting at is the APIs do not have a ready-made function for copying between model files.

It can be done (in SU2022+) by creating a temporary group (within an undo operation,) and then writing a copy of the group’s definition to a file. The temp grouping can then be undone. After opening the target model file, the copy definition can be loaded and an instance of it placed at some transform. The instance can be exploded and the definition removed from the model.

But yes, with SU2022 and higher, the C API can accept references of Ruby objects.
See: SketchUp C API: SketchUpAPI/application/ruby_api.h File Reference

But these may not be assignable to another model object. Ie, it’s likely the code would need to recreate a new C object from the original’s vertices and copy any other extraneous properties (like attribute dictionaries and behaviors.)