Class: Sketchup::Material

Inherits:
Entity
  • Object
show all
Includes:
Comparable

Overview

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.)

Examples:

face.material = mat1
face.material = "red"
face.material = 0xff0000

Version:

  • SketchUp 6.0

Constant Summary #

Sketchup::Material::MATERIAL_SOLID
Sketchup::Material::MATERIAL_TEXTURED
Sketchup::Material::MATERIAL_COLORIZED_TEXTURED
Sketchup::Material::COLORIZE_SHIFT
Sketchup::Material::COLORIZE_TINT

Instance Method Summary # collapse

Methods inherited from Entity

#add_observer, #attribute_dictionaries, #attribute_dictionary, #delete_attribute, #deleted?, #entityID, #get_attribute, #inspect, #model, #parent, #persistent_id, #remove_observer, #set_attribute, #to_s, #typename, #valid?

Instance Method Details

#<=>(material2) ⇒ Object

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

Examples:

model = Sketchup.active_model
materials = model.materials
m1 = materials.add('Joe')
m2 = materials.add('Fred')
p m1 <=> m2

Returns status - 0 if they are equal, positive number if material1 > material2, negative if material1 < material2

Parameters:

  • material2

    A Material object.

Returns:

  • status - 0 if they are equal, positive number if material1 > material2, negative if material1 < material2

Version:

  • SketchUp 6.0

#==(material2) ⇒ Object

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

Examples:

model = Sketchup.active_model
materials = model.materials
m1 = materials.add('Joe')
m2 = materials.add('Fred')
if (m1 == m2)
  UI.messagebox('The Materials are equal.')
else
  UI.messagebox('The Materials are not equal.')
end

Returns status - true if the materials are the same, false if they are different

Parameters:

  • material2

    A Material object.

Returns:

  • status - true if the materials are the same, false if they are different

Version:

  • SketchUp 6.0

#alphaObject

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

The value will be between 0.0 and 1.0. A value of 0.0 means that the material is completely transparent. A value of 1.0 means that the Material is completely opaque.

Examples:

alpha_value = Sketchup.active_model.materials[0].alpha

Returns:

  • alpha - a number between 0 and 1

Version:

  • SketchUp 6.0

#alpha=(alpha) ⇒ Object

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

The value must be between 0.0 and 1.0. A value of 0.0 means that the material is completely transparent. A value of 1.0 means that the Material is completely opaque.

Examples:

model = Sketchup.active_model
materials = model.materials
material = materials.add('Joe')
material.alpha = 0.5

Returns status - the newly set opacity value

Parameters:

  • alpha

    An opacity value.

Returns:

  • status - the newly set opacity value

Version:

  • SketchUp 6.0

#colorObject

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

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

Examples:

model = Sketchup.active_model
materials = model.materials
material = materials.add('Joe')
material.color = 'red'
color = material.color

Returns:

  • color - a Color object

Version:

  • SketchUp 6.0

#color=(color) ⇒ Object

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.

Examples:

model = Sketchup.active_model
materials = model.materials
material = materials.add('Joe')
material.color = 'red'

Returns color - the newly set Color object's name

Parameters:

  • color

    A Color object.

Returns:

  • color - the newly set Color object's name

Version:

  • SketchUp 6.0

#colorize_deltasObject

The colorize_deltas method retrieves the HLS delta for colorized materials.

Examples:

material = Sketchup.active_model.materials[0]
h, l, s = material.colorize_deltas

Returns:

  • Array - An array of floats representing the HLS delta.

Version:

  • SketchUp 2015

#colorize_typeObject

The colorize_type method retrieves the type of colorization of the material. This value is only relevant when the materialType is set to 2 (colorized textured). Types include:

- 0 = shift (Sketchup::Material::COLORIZE_SHIFT),
- 1 = tint (Sketchup::Material::COLORIZE_TINT),

Examples:

material = Sketchup.active_model.materials[0]
type = material.colorize_type

Returns:

  • type - the colorize type for the Material object.

Version:

  • SketchUp 2015

#colorize_type=(type) ⇒ Object

The colorize_type method set the type of colorization of the material. This value is only relevant when the materialType is set to 2 (colorized textured). Types include:

- 0 = shift (Sketchup::Material::COLORIZE_SHIFT),
- 1 = tint (Sketchup::Material::COLORIZE_TINT),

Examples:

material = Sketchup.active_model.materials[0]
material.colorize_type = Sketchup::Material::COLORIZE_TINT

Returns type - the colorize type for the Material object.

Parameters:

  • type
    • the new colorize type for the Material object.

Returns:

  • type - the colorize type for the Material object.

Version:

  • SketchUp 2015

#display_nameObject

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

This should be used when presenting the name in the UI, but the returned name cannot be used as a key in model.materials.

Examples:

model = Sketchup.active_model
materials = model.materials
material = materials.add('[Joe]')
# Use .name for the internal name of a material
puts material.name # Outputs "[Joe]"
# Use .display_name for presenting the material name
# to the UI like SketchUp does.
puts material.display_name # Outputs "Joe"

Returns:

  • name - the display name for the material

Version:

  • SketchUp 6.0

#materialTypeObject

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

- 0 = solid (Sketchup::Material::MATERIAL_SOLID),
- 1 = textured (Sketchup::Material::MATERIAL_TEXTURED),
- 2 = colorized textured (Sketchup::Material::MATERIAL_COLORIZED_TEXTURED).

The constants where added in SketchUp 2015.

Examples:

material = Sketchup.active_model.materials[0]
type = material.materialType

Returns:

  • type - the material type for the Material object. See summary for details.

Version:

  • SketchUp 6.0

#nameObject

The name method retrieves the name of the material. This is the internal name of the object which should be used for retrieving the material from the model's material list.

Use .display_name to display the name in the UI.

Examples:

model = Sketchup.active_model
materials = model.materials
material = materials.add('[Joe]')
# Use .name for the internal name of a material
puts material.name # Outputs "[Joe]"
# Use .display_name for presenting the material name
# to the UI like SketchUp does.
puts material.display_name # Outputs "Joe"

Returns:

  • name - the name of the Material object

Version:

  • SketchUp 6.0

#name=(str) ⇒ Object

Sets the name of the material.

Examples:

materials = Sketchup.active_model.materials
material = materials.add("Joe")
material.name = 'Jeff'

Returns:

  • name - the newly set material name.

Version:

  • SketchUp 8.0 M1

#save_as(filename) ⇒ Boolean

The #save_as method is used to write a material to a SKM file.

You must remember to append “.skm” to the filename as this will not be done automatically.

Examples:

filename = File.join(ENV['HOME'], 'Desktop', 'su_test.skm')
materials = Sketchup.active_model.materials
material = materials.add("Hello World")
material.color = 'red'
material.save_as(filename)

Parameters:

  • filename (String)

    the path to the SKM file to load.

Returns:

  • (Boolean)

    `true` if successful

Version:

  • SketchUp 2017

#textureObject

The texture method retrieves the texture of the material.

Examples:

model = Sketchup.active_model
materials = model.materials
material = materials.add('Joe')
material.texture = "C:/Materials/Carpet.jpg"
texture = material.texture

Returns:

  • texture - the Texture object within the Material. Returns nil if the Material does not have a texture.

Version:

  • SketchUp 6.0

#texture=(filename) ⇒ Sketchup::Texture #texture=(properties) ⇒ Sketchup::Texture

The texture= method sets the texture for the material.

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

Examples:

model = Sketchup.active_model
materials = model.materials
material = materials.add('Joe')
material.texture = "C:/Materials/Carpet.jpg"

Overloads:

  • #texture=(filename) ⇒ Sketchup::Texture

    Parameters:

    • filename (String)

      The file path to the texture the material should use.

  • #texture=(properties) ⇒ Sketchup::Texture

    Parameters:

    • properties (Array<String, Integer, Integer>)

      An array with the texture filename and optionally the width and height in model units.

Returns:

Version:

  • SketchUp 6.0

#use_alpha?Object

The use_alpha? method tells if the material uses transparency.

Note that this is not affected by the alpha value of the color object. Only the .alpha value and transparent texture will make this method return true.

Examples:

material = Sketchup.active_model.materials[0]
is_alpha = material.use_alpha?

Returns:

  • type - the material type for the Material object. See summary for details.

  • (Boolean)

Version:

  • SketchUp 6.0

#write_thumbnail(filename, resolution) ⇒ Object

The write_thumbnail method writes a bitmap thumbnail to the given file name.

Examples:

model = Sketchup.active_model
model.materials.each { |material|
  thumbnail_file = "C:/tmp/materials/#{material.display_name}.png"
  material.write_thumbnail(thumbnail_file, 128)
}

Returns status - true if successful, false if unsuccessful.

Parameters:

  • filename

    The file name for the thumbnail.

  • resolution

    The resolution of the thumbnail.

Returns:

  • status - true if successful, false if unsuccessful.

Version:

  • SketchUp 8.0 M1