Assigning material from in model to identical group (same process for multiple materials and groups) NEED YOUR KIND HELP .

I regularly go through this forum, I found it very helpful and knowledgeable too.
I need your help regarding following things. I’ve created the code but unable to assign the materials from IN MODEL to identical groups on view port. so i request you to please help me to solve this issue.

        n = count
        #r = n / 4
        s = 24
        w = 12

        (0..n-1).each { |i|
       
        points = [
		  Geom::Point3d.new( 0,  0, 0),
		  Geom::Point3d.new(12,  0, 0),
		  Geom::Point3d.new(12, 12, 0),
		  Geom::Point3d.new( 0, 12, 0),
		]
        
        group = entities.add_group
        face = group.entities.add_face(points).reverse! #.pushpull 12

       # Applying Material
#       group.material= "red"


        new_transformation = Geom::Transformation.new([i*s,0,0])
        group.transformation = new_transformation

        # Select
        selection.add(group)
        
        }

Waiting for your reply . . . .
Thank you,

You have a statement that is commented out:

       group.material= "red"

Note that this is creating a new material object from what SketchUp knows to be a color name.
It is shorthand for and equivalent to:

model = group.model
materials = model.materials
matl = materials.add('red')
matl.color= "Red"
group.material = matl

… except that (in the first snippet) you won’t have a reference to the group’s material object unless you do …

matl = group.material= "red"

Why is not working for you? It works fine for me.

Are you sure that you have the correct rendering options set to show materials ?

You must have rendering set to “Shaded” or “Shaded (with Textures)” to see see the colors of materials. (The latter if your materials are using image textures.)

Check your Face Style setting beneath the View menu.

1 Like

Just for info, Integer#times can be used without needing to use a literal Range object …

count.times { |i|
  # code
}

… calls the block count times, being for i in 0..count-1.

Which is the same as:

for i in 0..count-1 {
  # code
}
1 Like

Ok check revised script now “RED” material are assign thanks

        model=Sketchup.active_model
        materials = Sketchup.active_model.materials
        materials.purge_unused  # First purge material in model

##  Material count and display material name in console :
# def
        count = materials.count
        number_materials = materials.length
        UI.messagebox("Your model has " + number_materials.to_s + " materials.")
        model.materials.each { |m|puts m.display_name}
        
# end # Material count


##  Create geometry :
#def
		model = Sketchup.active_model
		entities = model.active_entities
		selection = model.selection

# Method-1


        n = count
        #r = n / 4
        s = 24
        w = 12

        count.times { |i|
        points = [
		  Geom::Point3d.new( 0,  0, 0),
		  Geom::Point3d.new(12,  0, 0),
		  Geom::Point3d.new(12, 12, 0),
		  Geom::Point3d.new( 0, 12, 0),
		]
        
        group = entities.add_group
        face = group.entities.add_face(points).reverse!

       # Applying Material
       group.material= "red"

        new_transformation = Geom::Transformation.new([i*s,0,0])
        group.transformation = new_transformation

        # Select
        selection.add(group)
        
        }

#end #end def

But how to apply material from in active model means material is already apply some objects in scene like

Now actually create square face in group and apply these materials individually group.
but i don’t want to apply UV mapping. just we regular apply paint bucket tool.

Actually why i want this script when i was cross 50 materials in this scene and when applying manually created group and then 5-4 material was miss out.

simply understand see

  1. material tray
  2. material in model tiles

i want material in model tiles
in scene origin like

group.material= materials[i]
1 Like

Great thank you DAN

its save my time to find which material miss out . . . .

1 Like