Tasked with creating 1000+ image from 1000+ simple models. I am new to RUBY script, want to automate the task

Seems simple enough.
Many models, either individual component files, or all stacked in a single spot in one file.
Want to export an image file of each one.
If someone could help point me in the right direction for making a simple ruby script for this, the help would be much appreciated.

You would need first to state which kind of image you want for each file:

  • model thumbnail (normally the view of the model at last save)
  • a specific camera orientation and zoom (or zoom extents). Thus, is it the same for each model?

Also, are all your skp files in the same folder or in a few folders?

From what you are asking, i think it would be easiest to generate the image file from the saved thumbnail.

Due to differences in sizes, perspective, I do not think I can use the same camera angle for each model.

I have not generated the individual skp files yet, which makes generation from the thumbnail ideal, as I can save them with that in mind.

I can save them all to the same folder, as the images will all end up together anyways.

Thank you.

Something I can do a batch at a time would be ideal. As i create a group of models, i can run a script to autogenerate images from thumbnails?
PNG i do believe

I’ve used your plugings before by the way, so thank you again!
I was unaware if they were able to do something like this, so here I am.

I would like to know if you get a script for this task or if you find a plugin that could do it, I have almost all the plug-ins from fredo and none of them can do what you’re asking for, at least the ones I have installed.
Why doy you need 1000 images by the way?

Our company sells garden bed kits, there are many many many combinations of parts, options, sizes, heights, shapes…

Quick and dirty finger exercise…
Tested only once at SU 2021 (Windows). Use at your own risk! No warranties!

Click on a menu in Extensions >>“Write thumbnail png beside skps”.
In a popop window select any skp file in a desired directory (folder) and click Open.

The plugin will go through ALL skp files in that directory (independently which file selected) and write its thumbnail into png file, into that directory.

The file name will be the same just the extension will be png, e.g.: model.skp >> model.png, model1.skp >> model1.png etc… )

(In the ruby console, the name of the file being written will be printed )

You can install as extension (again: it is a “dirty” one):
Dezmo_tumbstopng_a.rbz (1011 Bytes)

_

module Dezmo
module TumbsToPngA
  extend self
  @@loaded = false unless defined?(@@loaded)
  
  def get_skps_directory( model )
    # get the starting directory based on a current file
    path = model.path
    if path.nil? || path.empty?
      start_dir = Dir.getwd
    else
      start_dir = File.dirname(path)
    end
    # give a chance to user to select other directory
    # containing .skp file(s)
    title = "Save thumbnails in a selected directory: Select any SKP File ..."
    user_path = UI.openpanel(title, start_dir, "SKP|*.skp|")
    return unless user_path
    directory_path = File.dirname(user_path)
    # UI::openpanel returns paths with backslashes, change it:
    directory_path.gsub!(/\\\\|\\/,"/")
  end

  def inform(text)
    UI.messagebox(text)
    p "Job done!"
  end

  def create_thumbnails_pngs( model = Sketchup.active_model )
    directory_path = get_skps_directory( model )
    return inform("There is no skp file!") if !directory_path || !File.exist?(directory_path)
    successful = 0
    unsuccessful = 0
    skp_filenames = []
    image_filenames = []
    Dir.entries(directory_path).each{|file_name|
      next unless File.extname(file_name).downcase == ".skp"
      skp_filenames<<File.join(directory_path, file_name)
      base_name = File.basename(file_name, ".*")
      image_filenames<<File.join(directory_path, base_name + ".png")
    }
    skp_filenames.size.times{|i|
      p "Writing: #{image_filenames[i]}"
      # The main task: Write the thumbs into file...
      status = Sketchup.save_thumbnail(skp_filenames[i], image_filenames[i])
      status ? successful += 1 : unsuccessful += 1
    }
    done = " #{successful.to_s} png image(s) created."
    error = "\n #{unsuccessful.to_s} error(s) occurred."
    inform(done + error)
  end
  
  unless @@loaded
    cmd1 = UI::Command.new("Write thumbnail png beside skps"){
      create_thumbnails_pngs()
    }
    menu = UI.menu("Plugins").add_item(cmd1)
    @@loaded = true
  end
  
end
end

References (some):

https://ruby-doc.org/core-2.7.1/Dir.html#method-c-getwd
https://ruby-doc.org/core-2.7.1/File.html#method-c-basename

https://ruby.sketchup.com/UI.html#openpanel-class_method
https://ruby.sketchup.com/Sketchup.html#save_thumbnail-class_method

2 Likes