My favorites | English | Sign in

Google SketchUp Ruby API

Material

class

Parent: Entity

Introduction

SketchUp 6.0+

The Material class represents a texture or color that can be applied to Drawingelements. 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

Methods

Material.<=>SketchUp 6.0+

The <=> method is used to compare two materials based on name. The number returned relates to the "string distance" between the names.

Arguments:

material1
A Material object.
material2
A Material object.

Returns:

status
0 if they are equal, positive number if material1 > material2, negative if material1 < material2
 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:\\Materials\\Carpet.jpg"              
 m2.texture = "c:\\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

Material.==SketchUp 6.0+

The == method is used to test if two materials are the same.

Arguments:

material1
A Material object.
material
2 A Material object.

Returns:

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:\\Materials\\Carpet.jpg"               
 m2.texture = "c:\\Materials\\BlueTile.jpg"
 status = m == m2

 # Yields a False
 if (status)
   UI.messagebox status
 else
   UI.messagebox "The Materials are not Equal"
 end

Material.alphaSketchUp 6.0+

The alpha method is used to get the opacity of the material.

The value will 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.

Returns:

alpha
a number between 0 and 1
 alpha_value = Sketchup.active_model.materials[0].alpha

Material.alpha=SketchUp 6.0+

The alpha= method is used to set the opacity of the material.

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.

Arguments:

alpha
An opacity value.

Returns:

status
the newly set opacity value
 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:\\Materials\\Carpet.jpg"               
 m2.texture = "c:\\Materials\\BlueTile.jpg"
 alpha = m.alpha=0     
 if (alpha)
   UI.messagebox alpha
 else
   UI.messagebox "Failure"
 end

Material.colorSketchUp 6.0+

The color method is used to retrieve the color of the material.

If it uses a Texture, this will return the average color.

Returns:

color
a Color 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:\\Materials\\Carpet.jpg"               
 m2.texture = "c:\\Materials\\BlueTile.jpg"
 color = m.color     
 if (color)
   UI.messagebox color
 else
   UI.messagebox "Failure"
 end

Material.color=SketchUp 6.0+

The color= method is used to set the color of the material.

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.

Arguments:

color
A Color object.

Returns:

color
the newly set Color object's name
 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:\\Materials\\Carpet.jpg"               
 m2.texture = "c:\\Materials\\BlueTile.jpg"
 color = m.color ="Blue"    
 if (color)
   UI.messagebox color
 else
   UI.messagebox "Failure"
 end

Material.display_nameSketchUp 6.0+

The display_name method retrieves the name that is displayed within SketchUp for the material.

This should be used in most cases rather than using the name method.

Returns:

name
the display name 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:\\Materials\\Carpet.jpg"               
 m2.texture = "c:\\Materials\\BlueTile.jpg"
 name = m.display_name     
 if (name)
   UI.messagebox name
 else
   UI.messagebox "Failure"
 end

Material.materialTypeSketchUp 6.0+

The materialType method retrieves the type of the material. Types include:

  • 0 = solid,
  • 1 = textured,
  • 2 = colorized textured.
  • Returns:

    type
    the material type for the Material object. See summary for details.
     material = Sketchup.active_model.materials[0]
     type = material.materialType

Material.nameSketchUp 6.0+

The name method retrieves the name of the material.

Returns:

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:\\Materials\\Carpet.jpg"               
 m2.texture = "c:\\Materials\\BlueTile.jpg"
 name = m.name     
 if (name)
   UI.messagebox name
 else
   UI.messagebox "Failure"
 end

Material.textureSketchUp 6.0+

The texture method retrieves the texture of the material.

Returns:

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:\\Materials\\Carpet.jpg"               
 m2.texture = "c:\\Materials\\BlueTile.jpg"
 texture = m.texture     
 if (texture)
   UI.messagebox texture
 else
   UI.messagebox "Failure"
 end

Material.texture=SketchUp 6.0+

The texture= method sets the texture for the material.

Setting the Texture to nil will turn it into a solid color

Arguments:

texture
The Texture object to apply to the material.

Returns:

texture
the newly set Texture 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:\\Materials\\Carpet.jpg"               
 m2.texture = "c:\\Materials\\BlueTile.jpg"
 texture = m.texture = "c:\\Materials\\BlueTile.jpg"
 if (texture)
   UI.messagebox texture
 else
   UI.messagebox "Failure"
 end

Material.use_alpha?SketchUp 6.0+

The use_alpha? method tells if the material uses transparency.

Returns:

type
the material type for the Material object. See summary for details.
 material = Sketchup.active_model.materials[0]
 is_alpha = material.use_alpha?