On 12/29/05, adam beazley [email protected] wrote:
#also the "images" array has all of the .jpg files inside it.
begin
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.