Class: Sketchup::Materials

Inherits:
Entity
  • Object
show all
Includes:
Enumerable
Defined in:
SketchUp/Sketchup/Materials.rb

Overview

A collection of Materials objects. Each model contains a Materials collection that can be accessed via Model.materials.

Examples:

# Get a handle to all the materials in the current model.
model = Sketchup.active_model
materials = model.materials

Version:

  • SketchUp 6.0

Instance Method Summary collapse

Methods inherited from Entity

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

Instance Method Details

#[](index) ⇒ Sketchup::Material? #[](name) ⇒ Sketchup::Material?

The #[] method is used to retrieve a material by index or name.

The #at method is an alias of #[]

Examples:

model = Sketchup.active_model
materials = model.materials
material = materials[0]

Overloads:

  • #[](index) ⇒ Sketchup::Material?

    Returns a Material object on success, Nil on failure.

    Parameters:

    • index (Integer)

      A number representing the material’s index in an array of Material objects.

    Returns:

  • #[](name) ⇒ Sketchup::Material?

    Returns a Material object on success, Nil on failure.

    Parameters:

    • name (String)

      The name of the material.

    Returns:

Version:

  • SketchUp 6.0



42
43
# File 'SketchUp/Sketchup/Materials.rb', line 42

def [](arg)
end

#add(name) ⇒ Sketchup::Material

Add a new Material. When called with no arguments, this will generate a new unique name for the new Material. If a name is given, it will check to see if there is already a material with that name. If there is already a material with the given name, then a new unique name is generated using the given name as a base.

Examples:

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

Parameters:

  • name (String)

    The name of the new material.

Returns:

Version:

  • SketchUp 6.0



62
63
# File 'SketchUp/Sketchup/Materials.rb', line 62

def add(name)
end

#add_observer(observer) ⇒ Boolean

The add_observer method is used to add an observer to the materials collection.

Examples:

materials = Sketchup.active_model.materials
status = materials.add_observer(observer)

Parameters:

  • observer (Object)

    An observer.

Returns:

  • (Boolean)

    true if successful, false if unsuccessful.

Version:

  • SketchUp 6.0



78
79
# File 'SketchUp/Sketchup/Materials.rb', line 78

def add_observer(observer)
end

#[](index) ⇒ Sketchup::Material? #[](name) ⇒ Sketchup::Material?

The #[] method is used to retrieve a material by index or name.

The #at method is an alias of #[]

Examples:

model = Sketchup.active_model
materials = model.materials
material = materials[0]

Overloads:

  • #[](index) ⇒ Sketchup::Material?

    Returns a Material object on success, Nil on failure.

    Parameters:

    • index (Integer)

      A number representing the material’s index in an array of Material objects.

    Returns:

  • #[](name) ⇒ Sketchup::Material?

    Returns a Material object on success, Nil on failure.

    Parameters:

    • name (String)

      The name of the material.

    Returns:

Version:

  • SketchUp 6.0



102
103
# File 'SketchUp/Sketchup/Materials.rb', line 102

def at(arg)
end

#countInteger

Note:

Since SketchUp 2014 the count method is inherited from Ruby’s Enumable mix-in module. Prior to that the #count method is an alias for #length.

Examples:

materials = Sketchup.active_model.materials
count = materials.count

Returns:

  • (Integer)

See Also:

Version:

  • SketchUp 6.0



119
120
# File 'SketchUp/Sketchup/Materials.rb', line 119

def count
end

#currentSketchup::Material

The current method is used to get the current material, i.e. the material that the user has selected in the Materials dialog.

Examples:

current = Sketchup.active_model.materials.current

Returns:

Version:

  • SketchUp 6.0



131
132
# File 'SketchUp/Sketchup/Materials.rb', line 131

def current
end

#current=(material) ⇒ Sketchup::Material

The current= method is used to set the current material.

Examples:

# Make the first material in the model "current"
materials = Sketchup.active_model.materials
materials.current = materials[0]

Parameters:

Returns:

Version:

  • SketchUp 6.0



147
148
# File 'SketchUp/Sketchup/Materials.rb', line 147

def current=(material)
end

#each {|Sketchup::Material| ... } ⇒ nil

The each method is used to iterate through all of the materials.

Examples:

model = Sketchup.active_model
model.materials.each { |material|
  puts material.display_name
}

Yields:

  • (Sketchup::Material)

    A variable that will hold each Material object as they are found.

Returns:

  • (nil)

Version:

  • SketchUp 6.0



164
165
# File 'SketchUp/Sketchup/Materials.rb', line 164

def each
end

#lengthInteger

Note:

The returned number includes Image materials as well. It will not reflect the number of materials yielded by #each. To get the number of non-image materials use #count or materials.to_a.size.

The number of materials in the collection.

Examples:

materials = Sketchup.active_model.materials
number = materials.length

Returns:

  • (Integer)

See Also:

Version:

  • SketchUp 6.0



182
183
# File 'SketchUp/Sketchup/Materials.rb', line 182

def length
end

#load(filename) ⇒ Sketchup::Material

The #load method is used to load a material from file into the model.

If a matching material exist in the model that will be returned instead.

Examples:

# Load a material from the shipped SketchUp library. (SketchUp 2016)
filename = 'Materials/Brick, Cladding and Siding/Cinder Block.skm'
path = Sketchup.find_support_file(filename)
materials = Sketchup.active_model.materials
material = materials.load(path)

Parameters:

  • filename (String)

    the path to the SKM file to load.

Returns:

Raises:

  • (RuntimeError)

    if the material failed to load.

Version:

  • SketchUp 2017



204
205
# File 'SketchUp/Sketchup/Materials.rb', line 204

def load(filename)
end

#purge_unusedSketchup::Materials

The purge_unused method is used to remove unused materials.

Examples:

materials = Sketchup.active_model.materials
materials.purge_unused

Returns:

Version:

  • SketchUp 6.0



216
217
# File 'SketchUp/Sketchup/Materials.rb', line 216

def purge_unused
end

#remove(material) ⇒ Boolean

Remove a given material.

NOTE: On SketchUp versions prior to 2014 there is a bug in this method that could potentially lead to file corruption. If you call Materials.remove on a material that is painted onto any entity in the active model (e.g. faces, edges, groups, …), then calling this method will not successfully unpaint the entity and remove the material from the model. You must first unpaint all of the entities that respond to .material and .back_material before calling Materials.remove.

Examples:

if entity.respond_to?(:material) do
  if entity.material.equal?(material_to_remove) do
    entity.material = nil
  end
end
# for entities that have a back material
if entity.respond_to?(:back_material) do
  if entity.back_material.equal?(material_to_remove) do
    entity.back_material = nil
  end
end
model = Sketchup.active_model
materials = model.materials
material = materials.add('Joe')
materials.remove(material)

Parameters:

Returns:

  • (Boolean)

    true if successful, false if unsuccessful.

Version:

  • SketchUp 8.0 M1



254
255
# File 'SketchUp/Sketchup/Materials.rb', line 254

def remove(material)
end

#remove_observer(observer) ⇒ Boolean

The remove_observer method is used to remove an observer from the materials collection.

Examples:

materials = Sketchup.active_model.materials
status = materials.remove_observer(observer)

Parameters:

  • observer (Object)

    An observer.

Returns:

  • (Boolean)

    true if successful, false if unsuccessful.

Version:

  • SketchUp 6.0



270
271
# File 'SketchUp/Sketchup/Materials.rb', line 270

def remove_observer(observer)
end

#sizeInteger

Note:

The returned number includes Image materials as well. It will not reflect the number of materials yielded by #each. To get the number of non-image materials use #count or materials.to_a.size.

The number of materials in the collection.

The #size method is an alias for #length.

Examples:

materials = Sketchup.active_model.materials
number = materials.size

Returns:

  • (Integer)

See Also:

Version:

  • SketchUp 2014



290
291
# File 'SketchUp/Sketchup/Materials.rb', line 290

def size
end

#unique_name(name) ⇒ String

The #unique_name method is used to retrieve a unique name from the materials collection that is based on the provided one. If provided name is unique it will be returned, otherwise any trailing indices will be replaced by a new index.

Examples:

materials = Sketchup.active_model.materials
unique_name = materials.unique_name("test_name")

Parameters:

  • name (String)

    the suggested name.

Returns:

Version:

  • SketchUp 2018



308
309
# File 'SketchUp/Sketchup/Materials.rb', line 308

def unique_name(name)
end