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:
<snip>
#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 =3D Sketchup.active_model
materials =3D model.materials
# Adds a material to the "in-use" material pallet.
m =3D materials.add "Material Name"
begin
# Returns nil if not successful, path if successful. Should
return a texture object
m.texture=3D"c:\\Program Files\\@Last Software\\SketchUp
4\\Materials\\Carpet.jpg"
rescue
UI.messagebox $!.message
end
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=3D(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 =3D Sketchup.active_model
materials =3D model.materials
material_name =3D File.basename(file_path) # returns Carpet.jpg from
c:/program files/etc/Carpet.jpg
# Adds a material to the "in-use" material pallet.
m =3D materials.add material_name # e.g. Carpet.jpg
begin
# Returns nil if not successful, path if successful. Should return
a texture object
m.texture =3D 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 corr=
ect.
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.