From: Wilson Bilkovich Date: 2005-12-30T01:59:43+09:00 Subject: Re: Directory and file listing require 'sketchup.rb' # I'm not getting a good picture of where this code fits, and how parameters are fed to it. # So, let's assume that Sketchup has a class method on it that will let us know. # e.g. Sketchup.materials_path contains 'C:/Program Files/@Last Software/Blah Blah/' # Call Materials_menu.initialize_menu somehow, probably not here, since this is the menu # action. Dir.chdir Sketchup.materials_path Dir["*.jpg"].each do |img_name| begin Sketchup.import_material(img_name) rescue Exception => e UI.messagebox(e.message) end end # This needs to be defined before it is called, so put it in one of your classes. # Let's assume it's available as Sketchup.import_material() # I've also made the assumption that 'texture' contains a filename, not the actual file data. # Note; you would put this actual definition in sketchup.rb, not here. def Sketchup.import_material(file_name) materials = Sketchup.active_model.materials # Adds a material to the "in-use" material pallet. m = materials.add "img" m.texture = file_name end # This should be in a better place as well, and you could use a Materials_menu # class variable instead of the file_loaded? thing. def Materials_menu.initialize_menu unless file_loaded?("massmaterialimporter.rb") # This will add a separator to the menu, but only once add_separator_to_menu("Plugins") plugins_menu = UI.menu("Plugins") Materials_menu = plugins_menu.add_submenu("Mass Material Importer") Materials_menu.add_item("Import Materials") { materialimporter } end end On 12/29/05, adam beazley wrote: > Thanks for all of the post so far I really appreciate it. > Well here is the code which I havent got to work just yet so any advice > would be appreciated: > > #----------------------------------------------------------------------------- > > require 'sketchup.rb' > > #----------------------------------------------------------------------------- > > > dir=gets > Dir.chdir dir > #Dir.chdir "c:/Program Files/@Last Software/Sketchup 5/Materials/" > images=Dir["*.jpg"] > images.each do |img| > > end > > def materialimporter > model = Sketchup.active_model > materials = model.materials > > if (img) > # Adds a material to the "in-use" material pallet. > m = materials.add "img" > begin > # Returns nil if not successful, path if successful. > Should return a texture object > m.texture=images > rescue > UI.messagebox $!.message > end > > else > UI.messagebox "Failure" > end > > end > > > > > if( not file_loaded?("massmaterialimporter.rb") ) > > # This will add a separator to the menu, but only once > add_separator_to_menu("Plugins") > > plugins_menu = UI.menu("Plugins") > Materials_menu = plugins_menu.add_submenu("Mass Material Importer") > > Materials_menu.add_item("Import Materials") { materialimporter } > > > end > > #----------------------------------------------------------------------------- > file_loaded("massmaterialimporter.rb") > > > > > Ass you can see this script is meant to be put into a program called > sketchup. Im trying to make sketchup create a new texture for every jpg > in the given directory. Im a little confused as to how to make the > program go down the list of jpg's and make a new texture for each jpg > and naming that texture the same name as the jpg. > > ie > ruby reads c://program...../.../.../sample.jpg > creates a texture and names it "sample" > then goes to the next jpg and does the same thing > > anyway thanks everyone > > -- > Posted via http://www.ruby-forum.com/. > >