|
SketchUp Ruby API Reference |
The Texture class contains methods for obtaining information about textures that are part of your materials in your model (within the In-Model section of the Materials Browser). Remember, textures are repeatable images that repeat when painted on a surface.
Parent: Entity
Methods: average_color, filename, height, image_height, image_width, size=, valid?, width
Example Code: texturetests.rb
The average_color method retrieves a color object with the average color found in the texture.
color = texture.average_color
color – a color object (if successful), nil if unsuccessful.
model = Sketchup.active_model
materials=model.materials # Adds a material to the “in-use” material browser tab
m = materials.add “Test Color” begin
# Returns nil if not successful, path if successful
m.texture = “c:\\Program Files\\@Last Software\\SketchUp 4\\Materials\\Carpet.jpg”
rescue
UI.messagebox $!.message
end texture = m.texture
# Returns a color object
color = texture.average_color if (color)
UI.messagebox color
else
UI.messagebox “Failure: No average color”
end
The filename method retrieves the entire path, including the file, for a texture object.
filename = texture.filename
filename – a string representation of the path and filename used for the texture.
model = Sketchup.active_model
materials=model.materials # Adds a material to the “in-use” material browser tab
m = materials.add “Test Color” begin
# Returns nil if not successful, path if successful
m.texture = “c:\\Program Files\\@Last Software\\SketchUp 4\\Materials\\Carpet.jpg”
rescue
UI.messagebox $!.message
end texture = m.texture
# Returns a color object
filename = texture.filename if (filename)
UI.messagebox filename
else
UI.messagebox “Failure”
end
The height method is used to get the height of a repeatable texture image, in inches.
height = texture.height
height – the height, in inches, of the texture pattern
model = Sketchup.active_model
materials=model.materials # Adds a material to the “in-use” material browser tab
m = materials.add “Test Color” begin
# Returns nil if not successful, path if successful
m.texture = “c:\\Program Files\\@Last Software\\SketchUp 4\\Materials\\Carpet.jpg”
rescue
UI.messagebox $!.message
end texture = m.texture
# Returns a color object
height = texture.height if (height)
UI.messagebox height
else
UI.messagebox “Failure”
end
The image_height method retrieves the height of the repeatable texture image, in pixels.
imageheight = texture.image_height
imageheight – the height, in pixels, of the texture pattern
imageheight = texture.height if (imageheight)
UI.messagebox imageheight
else
UI.messagebox “Failure”
end
The image_width method retrieves the width of the repeatable texture image, in pixels.
imagewidth = texture.image_width
imagewidth – the width, in pixels, of the texture pattern
imagewidth = texture.width if (imagewidth)
UI.messagebox imagewidth
else
UI.messagebox “Failure”
end
The size= method allows you to set the size of the repeatable texture image, in inches
size = texture.size=size
size – the size, in inches, of the texture. This number will apply to height and width to keep aspect ratio.
size – the size, in inches, of the texture. This number will apply to height and width to keep aspect ratio.
size = texture.size=60 if (imagewidth)
UI.messagebox imagewidth
else
UI.messagebox “Failure”
end
The valid? method ensures that a texture is valid.
status = texture.valid?
status – true if valid, false if invalid.
status = texture.valid? if (valid)
UI.messagebox valid
else
UI.messagebox valid
end
The width method is used to get the width of a repeatable texture image, in inches.
width = texture.width
width – the width, in inches, of the texture pattern
model = Sketchup.active_model
materials=model.materials # Adds a material to the “in-use” material browser tab
m = materials.add “Test Color” begin
# Returns nil if not successful, path if successful\
m.texture = “c:\\Program Files\\@Last Software\\SketchUp 4\\Materials\\Carpet.jpg
rescue
UI.messagebox $!.message
end texture = m.texture
# Returns a color object
width = texture.width if (width)
UI.messagebox width
else
UI.messagebox “Failure”
end