Is there a way to random pick a component from a list, from a folder?

I would like Ruby to randomly pick a component from a list, from a folder.
Such as Door Level 001.skp, (or) Door Level 002.skp, (or) Door Level 003.skp
Is this possible? I know you can depth = rand 50.feet
width = rand 50.feet
When I add the other Door Level’s, it says it can’t produce from nil?

point = Geom::Point3d.new 0,0,0
transform = Geom::Transformation.new point
model = Sketchup.active_model
entities = model.active_entities
path = Sketchup.find_support_file “Door Level 001.skp”,
"Plugins/MG Build Gen/"
definitions = model.definitions
componentdefinition = definitions.load path
instance = entities.add_instance componentdefinition, transform

path = Sketchup.find_support_file(
  "Door Level 001.skp",
  "Plugins/MG Build Gen/"
)

will set path == nil or path.nil? to true, if the file cannot be found in the directory specified (2nd argument.)

So often when using Ruby, it is normal to test the resultant reference boolean-wise.

path = Sketchup.find_support_file( some_model_file, my_resource_path )
if path
  # use the path reference
else
  puts "(#{Module::nesting[0].name}:#{caller(0,1).first}): "<<
  "Whoops, model file does not exist at path:\n  "<<my_resource_path.inspect
end

I mean that when I put in more than one Door level;
When I put in path = Sketchup.find_support_file rand “Door Level 001.skp”, “Door Level 002.skp”,
"Plugins/MG Build Gen/"
I get bak Run aborted. Error: (eval):6:in ‘rand’: wrong number of arguments(3 for 1)

Try something like this inside your module:

def insert_rand_comp(
  transform = [0,0,0],
  fileglob = "*.skp"
)
  #
  resource_path = "Plugins/#{PLUGIN_DIR}/Resources/Components"
  search_path = Sketchup.find_support_file(resource_path)
  return false if search_path.nil?
  #
  skps = nil
  Dir::chdir( search_path ) {
    skps = Dir[fileglob]
  }
  count = skps.size
  return false if count.zero?
  index = rand(count-1)
  dfile = File.join( search_path, skps[index] )
  #
  model = Sketchup.active_model
  ents  = model.active_entities
  #
  dlist = model.definitions
  cdef  = dlist.load(dfile) rescue nil # nil on IOError
  return false if cdef.nil?
  inst  = entities.add_instance( cdef, transform )
  #
end ###

Well of course you do. The key in that message is:
Error: (eval):6:in ‘rand’: wrong number of arguments(3 for 1)

Looking at the doc for Kernel#rand(), is obvious it only allows 1 numeric argument.

You must follow the docs. Ruby is flexible, but not that flexible. :wink:


See other docs:

class Dir

Ok. Thank you. I’ll try it. I’m still learning. (Obviously) :grinning:

1 Like

Oh I see a mistake in the example method.

Line 17 needs to be:

  dfile = File.join( search_path, skps[index] )

I’ll edit the above post.


Also ref: class File

Thank you. I’m still using Sketchup 7, if that matters.

:arrow_double_down: OFFTOPIC :arrow_double_down:

Yes, it does. It still uses Ruby 1.8.0 for PC and 1.8.5 for Mac “out-of-the-box.” It also has many bugs that were fixed in later releases.

SketchUp 8 MR5 is free so there is no reason not to update. And it updated Ruby to 1.8.6-p287.
You should then refer to the Ruby docs for 1.8.6 (instead of that for 2.0):
http://ruby-doc.org/core-1.8.6/index.html

You can install various SketchUp versions side by side.
Each version uses it’s own folder and registry key.

(I have 5 versions of SketchUp installed on my computer.)


Anyway, SketchUp v2014 and higher switched to Ruby 2.0, which has many improvements, including Unicode string support on PC. This allowed user plugins to move into the user’s %AppData% path. It helps solve many folder permissions issues.

Also the Ruby Standard Library is now distributed with SketchUp 14+.


There are some things in Ruby 2 that do not work in Ruby 1.8. Extra methods added. Some times arguments for methods changed. etc.

:arrow_double_up: OFFTOPIC :arrow_double_up:

5 Versions !! Haha. I still use 7, because a lot of plug-ins don’t work with the newer versions. And I HATE the fact that, with the newer free versions, it has the splash screen, asking me if I really want to open Sketchup. It has been a huge deterrent to me, not to use newer versions. It’s annoying. I made this, with TIG’s help. Maybe it will be usefull to you…

NO, not really. I do not have v7 installed. I’ll never run v7 again. It is 8 years (or more) old. It has too many bugs, that are too hard to deal with (plugin-wise.)

From a programmer’s perspective, I stopped supporting v7 (on PC with Ruby v 1.8.0,) when v8 updated Ruby to 1.8.6-p287. (SketchUp version 7 on PC can be manually updated to Ruby 1.8.6-p287 by copying the DLL from v8.)

When SketchUp v 15 came out, I mostly stopped supporting any SketchUp with Ruby 1.8.x. With the release of SketchUp 2016, I don’t even worry about any of the old bugs, and do not write code for Ruby 1.8.

Heck, even Ruby 2.0.0 is obsolete and no longer maintained!


Re that script you posted. It says quite clearly “(c) TIG 2015” at the top.
This means he should be the only one distributing it.

I also find the single “Convert7” toplevel module name too common. All code should be wrapped within a toplevel author or company module. The plugin or utility would be in a sub-module.

Thank you for your thoughts. I guess I’m just slow to change. I didn’t know what © meant. I thought it was ok to share. Sorry. I have removed it.

© means copyrighted.

You can however, share a link to where TIG posted it.
This way new users always get the latest revision.

Ok, thank you. I didn’t know. Since I got it on this forum, I thought it was ok to share it. Won’t happen again.

1 Like