|
SketchUp Ruby API Reference |
The Material class is used to access to the materials used in a model. It is most often applied to Faces.
You can pass any object that can be used as a material to a method that requires a material. Objects include actual materials, color, and classes that can be converted to a color.
The following are valid (assuming the existence of a Material mat1.)
face.material = mat1
face.material = "red"
face.material = 0xff0000
Parent:Entity
Methods: <=>, ==, alpha, alpha=, color, color=, display_name, materialType, name, texture, texture=, use_alpha?
Example Code: materialtests.rb
The <=> method is used to compare two materials based on name.
status = material1 <=> material2
material1 - a Material object
material2 - a Material object
status - 0 if they are equal, 1 if material 1 > material 2, -1 if material 1 < material 2
model = Sketchup.active_model
materials = model.materials
# Adds a material to the "in-use" material pallet.
m = materials.add "Joe"
m2 = materials.add "Fred"
# 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"
m2.texture="c:\\Program Files\\@Last Software\\SketchUp 4\\Materials\\BlueTile.jpg"
status = m <=> m2
# Yields a 1
if (status)
UI.messagebox status
else
UI.messagebox "Failure"
end
# Yields a -1
status = m2 <=> m
if (status)
UI.messagebox status
else
UI.messagebox "Failure"
end
The == method is used to test if two materials are the same.
status = material1 == material2
material1 - a Material object
material 2 - a Material object
status - true if the Materials are the same, false if they are different
model = Sketchup.active_model
materials = model.materials
# Adds a material to the "in-use" material pallet.
m = materials.add "Joe"
m2 = materials.add "Fred"
# 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"
m2.texture="c:\\Program Files\\@Last Software\\SketchUp 4\\Materials\\BlueTile.jpg"
status = m == m2
# Yields a False
if (status)
UI.messagebox status
else
UI.messagebox "The Materials are not Equal"
end
The alpha method is used to retrieve the opacity of the material.
alpha = material.alpha
alpha - the currently opacity value for the material
model = Sketchup.active_model
materials = model.materials
# Adds a material to the "in-use" material pallet.
m = materials.add "Joe"
m2 = materials.add "Fred"
# 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"
m2.texture="c:\\Program Files\\@Last Software\\SketchUp 4\\Materials\\BlueTile.jpg"
alpha = m.alpha
if (alpha)
UI.messagebox alpha
else
UI.messagebox "Failure"
end
The alpha= material is used to set the opacity of the material.
status = material.alpha = alpha
alpha - an opacity value
status - the newly set opacity value
The value must be between 0 and 1. A value of 0 means that the material is completely transparent. A value of 1 means that the Material is completely opaque.
model = Sketchup.active_model
materials = model.materials
# Adds a material to the "in-use" material pallet.
m = materials.add "Joe"
m2 = materials.add "Fred"
# 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"
m2.texture="c:\\Program Files\\@Last Software\\SketchUp 4\\Materials\\BlueTile.jpg"
alpha = m.alpha=0
if (alpha)
UI.messagebox alpha
else
UI.messagebox "Failure"
end
The color method is used to retrieve the color of the material.
color = material.color
color - a Color object
If it uses a Texture, this will return the average color.
model = Sketchup.active_model
materials = model.materials
# Adds a material to the "in-use" material pallet.
m = materials.add "Joe"
m2 = materials.add "Fred"
# 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"
m2.texture="c:\\Program Files\\@Last Software\\SketchUp 4\\Materials\\BlueTile.jpg"color = m.colorif (color)UI.messagebox colorelseUI.messagebox "Failure"end
The color= method is used to set the color of the material.
color = material.color = color
color - a Color object
color - the newly set Color object's name
If the Material has a Texture, then this turns it into a colorized Texture.
To reset the color of a Material with a Texture, set the color to nil.
model = Sketchup.active_model
materials = model.materials
# Adds a material to the "in-use" material pallet.
m = materials.add "Joe"
m2 = materials.add "Fred"
# 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"
m2.texture="c:\\Program Files\\@Last Software\\SketchUp 4\\Materials\\BlueTile.jpg"
color = m.color ="Blue"
if (color)
UI.messagebox color
else
UI.messagebox "Failure"
end
The display_name method retrieves the name that is displayed within SketchUp for the material.
name = material.display_name
name - the display name for the material
This should be used in most cases rather than using the name method.
model = Sketchup.active_model
materials = model.materials
# Adds a material to the "in-use" material pallet.
m = materials.add "Joe"
m2 = materials.add "Fred"
# 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"
m2.texture="c:\\Program Files\\@Last Software\\SketchUp 4\\Materials\\BlueTile.jpg"name = m.display_nameif (name)UI.messagebox nameelseUI.messagebox "Failure"end
The materialType method retrieves the type of the material.
type = material.materialType
type - the material type for the Material object
model = Sketchup.active_model
materials = model.materials
# Adds a material to the "in-use" material pallet.
m = materials.add "Joe"
m2 = materials.add "Fred"
# 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"
m2.texture="c:\\Program Files\\@Last Software\\SketchUp 4\\Materials\\BlueTile.jpg"
type = m.materialTypeif (type)UI.messagebox typeelseUI.messagebox "Failure"
The name method retrieves the name of the material.
name = material.name
name - the name of the Material object
model = Sketchup.active_model
materials = model.materials
# Adds a material to the "in-use" material pallet.
m = materials.add "Joe"
m2 = materials.add "Fred"
# 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"
m2.texture="c:\\Program Files\\@Last Software\\SketchUp 4\\Materials\\BlueTile.jpg"
name = m.nameif (name)UI.messagebox nameelseUI.messagebox "Failure"end
The texture method retrieves the texture of the material.
texture = material.texture
texture - the Texture object within the Material. Returns nil if the Material does not have a Texture.
model = Sketchup.active_model
materials = model.materials
# Adds a material to the "in-use" material pallet.
m = materials.add "Joe"
m2 = materials.add "Fred"
# 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"
m2.texture="c:\\Program Files\\@Last Software\\SketchUp 4\\Materials\\BlueTile.jpg"
texture = m.textureif (texture)UI.messagebox textureelseUI.messagebox "Failure"end
The texture= method sets the texture for the material.
texture = material.texture = texture
texture - the Texture object to apply to the material
texture - the newly set Texture object
Setting the Texture to nil will turn it into a solid color
model = Sketchup.active_model
materials = model.materials
# Adds a material to the "in-use" material pallet.
m = materials.add "Joe"
m2 = materials.add "Fred"
# 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"
m2.texture="c:\\Program Files\\@Last Software\\SketchUp 4\\Materials\\BlueTile.jpg"
texture = m.texture="c:\\Program Files\\@Last Software\\SketchUp 4\\Materials\\BlueTile.jpg"if (texture)UI.messagebox textureelseUI.messagebox "Failure"end
The use_alpha? material is used to determine if the material uses transparency.
status = material.use_alpha?
status - true if transparency is used, false if transparency is not used
model = Sketchup.active_model
materials = model.materials
# Adds a material to the "in-use" material pallet.
m = materials.add "Joe"
m2 = materials.add "Fred"
# 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"
m2.texture="c:\\Program Files\\@Last Software\\SketchUp 4\\Materials\\BlueTile.jpg"
texture = m.texture="c:\\Program Files\\@Last Software\\SketchUp 4\\Materials\\BlueTile.jpg"
status = m.use_alpha?if (status)UI.messagebox statuselseUI.messagebox "Failure"endt
|
SketchUp Ruby API Reference: Material |
© Google Inc. 2007 sketchup.google.com |