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
Sketchup::Material::OWNER_MANAGER
Sketchup::Material::OWNER_IMAGE
Sketchup::Material::OWNER_LAYER

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) ⇒ Integer

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

Parameters:

Returns:

  • (Integer)

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

Version:

  • SketchUp 6.0

#==(material2) ⇒ Boolean

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

Parameters:

Returns:

  • (Boolean)

    true if the materials are the same, false if they are different

Version:

  • SketchUp 6.0

#alphaFloat

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:

  • (Float)

    a number between 0 and 1

Version:

  • SketchUp 6.0

#alpha=(alpha) ⇒ Float

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

Parameters:

  • alpha (Float)

    An opacity value.

Returns:

  • (Float)

    the newly set opacity value

Version:

  • SketchUp 6.0

#colorSketchup::Color

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:

Version:

  • SketchUp 6.0

#color=(color) ⇒ Sketchup::Color, ...

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'

Parameters:

Returns:

Version:

  • SketchUp 6.0

#colorize_deltasArray(Float, Float, Float)

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(Float, Float, Float))

    An array of floats representing the HLS delta.

Version:

  • SketchUp 2015

#colorize_typeInteger

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:

  • (Integer)

    the colorize type for the Material object.

Version:

  • SketchUp 2015

#colorize_type=(type) ⇒ Integer

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

Parameters:

  • type (Integer)

    the new colorize type for the Material object.

Returns:

  • (Integer)

    the colorize type for the Material object.

Version:

  • SketchUp 2015

#display_nameString

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:

  • (String)

    the display name for the material

Version:

  • SketchUp 6.0

#materialTypeInteger

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:

  • (Integer)

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

Version:

  • SketchUp 6.0

#nameString

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:

  • (String)

    the name of the Material object

Version:

  • SketchUp 6.0

#name=(str) ⇒ String

Note:

Since SketchUp 2018 this method will raise an `ArgumentError` if the name is not unique.

Note:

SketchUp 2018 would raise an error if you named material the name it already had.

The #name= method sets the name of the material.

Examples:

Safely change name without raising errors

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

Parameters:

  • str (String)

    the new material name

Returns:

  • (String)

    the newly set material name.

Raises:

  • (ArgumentError)

    if the name is not unique to the model. (Added in SU2018)

Version:

  • SketchUp 8.0 M1

#owner_typeInteger

The #owner_type method is used to determine if the material is owned by a Sketchup::Materials.

Returned value is one of:

  • Sketchup::Material::OWNER_MANAGER

  • Sketchup::Material::OWNER_IMAGE

  • Sketchup::Material::OWNER_LAYER

Returns:

  • (Integer)

Version:

  • SketchUp 2019.2

#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

#textureSketchup::Texture?

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:

  • (Sketchup::Texture, nil)

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

Version:

  • SketchUp 6.0

#texture=(filename) ⇒ Object #texture=(properties) ⇒ Object #texture=(image_rep) ⇒ Object

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) ⇒ Object

    Parameters:

    • filename (String)

      The file path to the texture the material should use.

  • #texture=(properties) ⇒ Object

    Parameters:

    • properties (Array(String, Length, Length))

      An array with the texture file path and optionally the width and height.

  • #texture=(image_rep) ⇒ Object

    Parameters:

    • image_rep (Sketchup::ImageRep)

      The pixel data representing the texture. (Added in SketchUp 2018)

Version:

  • SketchUp 6.0

#use_alpha?Boolean

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:

  • (Boolean)

Version:

  • SketchUp 6.0

#write_thumbnail(path, resolution) ⇒ Boolean

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

Parameters:

  • path (String)

    The file path for the thumbnail.

  • resolution (Integer)

    The resolution of the thumbnail.

Returns:

  • (Boolean)

    true if successful, false if unsuccessful.

Version:

  • SketchUp 8.0 M1