Run SketchUp with command arguments RubyStartup but it not executed util window focused

Hi there. I want to use SketchUp with command arguments RubyStartup to automate something, like exporting a skp to gltf (glb) file. (ref: SketchUp Command Arguments – Procrastinators Revolt!)

I tried the following command in macOS terminal (I’m using macOS 15.1 and SketchUp 2024):
/Applications/SketchUp.app/Contents/MacOS/SketchUp -RubyStartup ~/exporter.rb ~/test.skp
where the exporter.rb has only three lines, which exports the current model to glb and exit:

model = Sketchup.active_model
status = model.export('/Users/ZhouYucheng/Downloads/output.glb')
Sketchup.quit()

However, my problem is: when I run the above command in terminal, the ruby script will not run automatically (maybe because the Sketchup’s window is not focused automatically). So I have to manually click the Sketchup’s window to make it focused (to be the frontmost window), and then the ruby exporter script will be executed.

How to address this issue?
I just want to use command line to let SketchUp export a glb file from a skp file. I think there should be a robust way to automate it.

P.S. I searched about SketchUp C API (SDK). But it seems that I cannot use API to directly export a ready-made format (ref: How convert skp to obj in the c sdk - #2 by DanRathbun).
P.S. I noticed that one can use AppleScript like osascript -e 'activate application "SketchUp"' to focus a window. But this solution is not robust. Actually, I want my command to run even I locked the screen (so it can be configured as a server).

:thinking: Perhaps you can use the focus method to focus the active model window.

1 Like

I believe that on Mac before the actual command line arguments you need one that says --args. Another UNIX convention (like the ~ in backup file names instead of skb).

BTW please correct your forum profile. There is no Free Plan of SketchUp 2024. Are you using a cracked version?

So building upon what Steve said, --args was added to the open command in MacOS 10.6.

You might try something like:

open -n -a /Applications/SketchUp.app/Contents/MacOS/SketchUp ~/test.skp --args -RubyStartup ~/exporter.rb 

My only questions would be, isn’t the app bundle versioned?

1 Like

It doesn’t work :joy: The focus method inside ruby will not be executed as long as the whole ruby script is not executed.

Thank you for your reply!
Using open command with --args works! I also found the trailing “/Contents/MacOS/SketchUp” is not necessary in open, and I can just using open -n -a /Applications/SketchUp.app ~/test.skp --args -RubyStartup ~/exporter.rb. This command will make the opened window focused.

As for your question. Do you mean the app bundle “SketchUp.app” should be like “SketchUp 2024.app”? It’s not versioned on my mac. I forget the detail when I installing SketchUp, and maybe I renamed the app bundle name in /Application folder :joy:

However, my problem is not fully addressed. I want to config this command to run automatically in headless mode (e.g., if I locked the screen, like a LaunchDaemon). But I found the open command also doesn’t work in that case: the application it opened will not be auto focused (the opened window’s top-left red/yellow/green dot is gray), and thus the ruby script is not executed. Maybe it’s related to the macOS system.

So, is there any other way to access SketchUp 2024’s built-in export gltf method from command line?
I want this because the built-in export gltf method is added in SketchUp 2024 (I doesn’t found it in SketchUp 2023), and the third-party’s gltf export extension is not as good as the built-in’s.

SketchUp is a GUI application. It does not have a “headless mode.”

The C SDK can operate on model files in “headless mode”, however the exporters are not included in the C API. It is possible that perhaps you could use a 3rd party C library to export to glB format.

Thank you for your explanation.

I just realized that maybe I could write my code fully in ruby and let it auto scan a folder and convert all skp files to glb. So I can open SketchUp app at the beginning and let the ruby script to run forever.
That is: use ruby API to open a skp file once it found, convert it to glb, and close the window.

I’m not familiar with ruby. I read some basic docs about the SketchUp Ruby API (Class: Layout::SketchUpModel — SketchUp Ruby API Documentation).
I found there is a open_file method (Module: Sketchup — SketchUp Ruby API Documentation), but I don’t found a close_file method?
Instead I found a quit method, but it is used to quit the app entirely. Can I just close one window? Or, can I use one window to load a model, convert it, and clear/empty it for re-using ?

Yes, three is a similar plugin by TIG for exporting to DAE
ExportBatchDAE SketchUp Plugins | PluginStore | SketchUcation

(However it seams to me that glb export does not exposed to the File: Exporter Options — SketchUp Ruby API Documentation but, perhaps still will work as your written in a first post??.. )

The #close method is used to close this model. On Mac OS, only the active model can be closed. On Windows, since there can be only one document open, this method will perform a File/New operation.

That is a LayOut Ruby API. This is about an instance of a SketchUp Model that is inserted into a .layout file. Not that much to do with the exporters…

2 Likes

You may be interested in FredoBatch, which can process tasks over a set of files. In the video, below, I show how to export an open set of files to DXF/DXG.

In you case, you would create a simple Custom task, with the following Ruby code:

module MyModule

def MyModule.export_to_glb(skp_file, task_context)
	#Path to the GLB file (same basename as the skp file, with glb extension)
	file_glb = skp_file.sub(/skp\Z/i, 'glb')
	
	#Export to GLB
	Sketchup.active_model.export(file_glb , false)
	
	#Log the information
	task_context.log("Exported to GLB: <b><!blue>#{File.basename(file_glb)}<!></b>")
end

end	#module MyModule

You have some details on how to create Tasks in the full documentation of FredoBatch.

Note that MyModule is just for namespacing. You can choose the name you want. Likewise, you can puts this code in a rb file of your choice (below baba.rb). The task will look like:

To run the task over a set of files, just create one or several File Sets, with the choice of one or several directories and a wildcard filter (like *):

Then create a job that associates the task and the File Set(s).

After run, you get the Results

Exported files are created:

HOWEVER, currently, it is not available in headless mode. You have to launch the job to execute it. But I may add it later on.

1 Like

Oh! The Model#close API it what I really need now! I’ll try that, thank you for pointing this!

Yes, Sketchup.active_model.export(‘xxx.glb’) works on my SketchUp 2024 :joy: I also don’t find any document about it. I just give it a try and find it works. Maybe the Docs should be updated :joy:

@tt_su Docs for Model#export do not yet mention .glB, .stl, .trb or .usdz formats.

Except for .stl, there are not any published options for these.