From: Wilson Bilkovich Date: 2005-12-30T08:01:15+09:00 Subject: Re: Directory and file listing On 12/29/05, adam beazley wrote: > Ok i think you lost me im still very new to ruby and programming in > general. I have some experience with C lang but not extensive. So as of > now this is where I am at with this program. > > this first snippet is working: > > > > #at this point the script is working > #the current directory is set to what the user imput! > #also the "images" array has all of the .jpg files inside it. > #------------------------------------------------------------------------------- > > Here is the correct script for adding a material, then naming it and > then adding a texture to it: > ------------------------------------------------------------------------- > def addMaterial > model = Sketchup.active_model > materials = model.materials > # Adds a material to the "in-use" material pallet. > m = materials.add "Material Name" > begin > # Returns nil if not successful, path if successful. Should > return a texture object > m.texture="c:\\Program Files\\@Last Software\\SketchUp > 4\\Materials\\Carpet.jpg" > rescue > UI.messagebox $!.message > end > ------------------------------------------------------------------------- > So how can I automate this script to loop until every jpg in the > directory has been added to the "in-use" material pallet and named the > same as its texture file. > addMaterial needs to take a parameter, so it can be used without knowing what context it's in. Also.. I'm not sure why you have begin/rescue/end in there. How could assigning a string to "m.texture" ever fail? If your texture=(texture_name) method in 'materials' might raise an exception, you might want to consider handling it there, and having it return nil if no texture could be created. Anyway.. def addMaterial(file_path) model = Sketchup.active_model materials = model.materials material_name = File.basename(file_path) # returns Carpet.jpg from c:/program files/etc/Carpet.jpg # Adds a material to the "in-use" material pallet. m = materials.add material_name # e.g. Carpet.jpg begin # Returns nil if not successful, path if successful. Should return a texture object m.texture = file_path rescue UI.messagebox $!.message end end Now you can do: images.each do |image_file| addMaterial(image_file) end The above is a 'block', and the "image_file" variable is only available between the 'do' and 'end' keywords. In your code, you were trying to access it from elsewhere, which isn't correct. Your code comment: # Returns nil if not successful, path if successful. Should return a texture object ..doesn't make too much sense to me. You're setting the path, so why does it get returned? Also, if "UI.messagebox" has a return value, sometimes your addMaterial method won't behave as you expect. I think you should revisit the use of begin/rescue/end in this case. Further, you should put some thought into where addMaterial (in Ruby style, add_material) should live. If you put it in the same class as 'materials', you can avoid the first few lines, and skip straight to 'materials.add' Good luck, --Wilson.