Materials

SketchUp 6.0+

class

Parent: Entity

Introduction

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

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

Methods

Materials.[]SketchUp 6.0+

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

Arguments:

index
A number representing the material's index in an array of Material objects.
name
The name of the material.

Returns:

material
a Material object on success, Nil on failure
 model = Sketchup.active_model
 materials = model.materials
 material = materials[0]

Materials.addSketchUp 6.0+

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 gnerated using the given name as a base.

Arguments:

name
The name of the new material.

Returns:

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

Materials.add_observerSketchUp 6.0+

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

Arguments:

observer
An observer.

Returns:

success
true if successful, false if unsuccessful.
 materials = Sketchup.active_model.materials
 status = materials.add_observer(observer)

Materials.atSketchUp 6.0+

The at method is an alias for []. See [].

Returns:

material
a Material object on success, Nil on failure
 model = Sketchup.active_model
 materials = model.materials
 material = materials.at(0)

Materials.countSketchUp 6.0+

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.

Returns:

integer
number of materials in the collection.
 materials = Sketchup.active_model.materials
 count = materials.count

Materials.currentSketchUp 6.0+

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

Returns:

material
a Material object.
 current = Sketchup.active_model.materials.current

Materials.current=SketchUp 6.0+

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

Arguments:

material
A Material object.

Returns:

true if successful, false if unsuccessful.
 # Make the first material in the model "current"
 materials = Sketchup.active_model.materials
 materials.current = materials[0]

Materials.eachSketchUp 6.0+

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

Arguments:

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

Returns:

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

Materials.lengthSketchUp 6.0+

The number of materials in the collection.

Returns:

integer
number of materials in the collection.
 materials = Sketchup.active_model.materials
 number = materials.length

Materials.purge_unusedSketchUp 6.0+

The purge_unused method is used to remove unused materials.

Returns:

materials
The Materials object.
 materials = Sketchup.active_model.materials
 materials.purge_unused

Materials.removeSketchUp 8.0 M1+

Remove a given material.

Arguments:

material
The material to remove.

Returns:

status
true if successful, false if unsuccessful.
 model = Sketchup.active_model
 materials = model.materials
 material = materials.add('Joe')
 materials.remove(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.

 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

Materials.remove_observerSketchUp 6.0+

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

Arguments:

observer
An observer.

Returns:

success
true if successful, false if unsuccessful.
 materials = Sketchup.active_model.materials
 status = materials.remove_observer(observer)

Materials.sizeSketchUp 2014+

The size method is an alias of length.

Returns:

integer
number of materials in the collection.
 materials = Sketchup.active_model.materials
 number = materials.size

  

Trimble Home
About Trimble - Privacy Policy - Contact Us