sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Transfer 'color by layer' to 'color by material'

    Scheduled Pinned Locked Moved SketchUp Discussions
    sketchup
    6 Posts 2 Posters 1.1k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R Offline
      rv1974
      last edited by

      Have you seen the plugin that does the subject?
      Thanks in advance

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        To clarify...
        Do you mean, to take each layer's color, and use that color for a [new] material [probably named based on the parent-layer's name?], then apply that material to all entities which are assigned that layer.

        If so try this... copy+paste+enter in the Ruby Console.

        mo=Sketchup.active_model
        cs={}
        ns=[]
        ls=mo.layers
        p ls[0].name
        ls.to_a[1..-1].each{|e| #skip Layer0
          cs[e.name]=e.color
          ns<<e.name
        }
        mo.start_operation('lcol>>mcol',true)
        ms=mo.materials
        ns.each{|k|
          m=ms.add(k) unless m=ms[k]
          m.color=cs[k]
        }
        mo.entities.each{|e|
          e.material=ms[e.layer.name] unless e.layer==ls[0] #Layer0
        }
        mo.definitions.each{|d|
          next if d.image?
          if d.group?
            d.entities.each{|e|
              e.material=ms[e.layer.name] unless e.layer==ls[0] #Layer0
            }
          else #compo
            d.entities.each{|e|
              e.material=ms[e.layer.name] unless e.layer==ls[0] #Layer0
            }
            d.instances.each{|e|
              e.material=ms[e.layer.name] unless e.layer==ls[0] #Layer0
            }
          end
        }
        mo.commit_operation
        puts
        
        

        It's one step undoable.

        It currently skips processing 'Layer0' - but to include it change:
        ls.to_a[1..-1].each to read ls.each
        and add a new # in front of the 4 lines **#** unless e.layer==ls[0] #Layer0

        Toggle between Material and Layer view settings to see the effect...

        TIG

        1 Reply Last reply Reply Quote 0
        • R Offline
          rv1974
          last edited by

          Works almost perfect. Please check PM.
          Many thanks!

          1 Reply Last reply Reply Quote 0
          • TIGT Offline
            TIG Moderator
            last edited by

            Transparency [alpha] in colors used 'by layer' is not detectable - I think?
            It always returns 255, even when it displays as less !
            Neither is a layer's 'texture'.

            This version also processes 'back-face color'.

            mo=Sketchup.active_model
            cs={}
            ns=[]
            ls=mo.layers
            p ls[0].name
            ls.to_a[1..-1].each{|e| #skip Layer0
              cs[e.name]=e.color
              ns<<e.name
            }
            mo.start_operation('lcol>>mcol',true)
            ms=mo.materials
            ns.each{|k|
              m=ms.add(k) unless m=ms[k]
              m.color=cs[k]
            }
            mo.entities.each{|e|
              e.material=ms[e.layer.name] unless e.layer==ls[0] #Layer0
              e.back_material=ms[e.layer.name] if e.is_a?(Sketchup;;Face) && e.layer!=ls[0] #Layer0
            }
            mo.definitions.each{|d|
              next if d.image?
              if d.group?
                d.entities.each{|e|
                  e.material=ms[e.layer.name] unless e.layer==ls[0] #Layer0
                  e.back_material=ms[e.layer.name] if e.is_a?(Sketchup;;Face) && e.layer!=ls[0] #Layer0
                }
              else #compo
                d.entities.each{|e|
                  e.material=ms[e.layer.name] unless e.layer==ls[0] #Layer0
                  e.back_material=ms[e.layer.name] if e.is_a?(Sketchup;;Face) && e.layer!=ls[0] #Layer0
                }
                d.instances.each{|e|
                  e.material=ms[e.layer.name] unless e.layer==ls[0] #Layer0
                }
              end
            }
            mo.commit_operation
            puts
            
            

            TIG

            1 Reply Last reply Reply Quote 0
            • R Offline
              rv1974
              last edited by

              Amazing! Many thanks.
              P.S. Please publish it it Store so it won't get buried in forum.

              1 Reply Last reply Reply Quote 0
              • TIGT Offline
                TIG Moderator
                last edited by

                Let's wait a day or two until it gets fine tuned...

                TIG

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Buy SketchPlus
                Buy SUbD
                Buy WrapR
                Buy eBook
                Buy Modelur
                Buy Vertex Tools
                Buy SketchCuisine
                Buy FormFonts

                Advertisement