sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🫛 Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    Applying new Material to a Selection is extremely slow

    Scheduled Pinned Locked Moved Developers' Forum
    6 Posts 4 Posters 414 Views 4 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.
    • fuzzybroF Offline
      fuzzybro
      last edited by

      Hi all,

      Could you please suggest how to apply a newly created material to a whole bunch of faces in the selection?

      Right now I'm doing it by looping through all the faces/instances in the selection and assigning new material:

      
        materials = Sketchup.active_model.materials
        mat = materials.add "My New Material"
      
        entities = Sketchup.active_model.selection
        entities.each do |e|
          is_instance = is_instance?(e)
          is_face = e.is_a?(Sketchup;;Face)
          next if !is_instance && !is_face
          e.material = mat
          e.back_material = mat if e.is_a?(Sketchup;;Face)
        end
      
      

      But it turns out to be extremely slow and doing the same thing via Paint Bucket Tool is blazing fast in comparison with assigning material via API.

      Is there a fast way of doing it via API?

      Cheers,
      Bro

      [OctaneRender for SketchUp Overview and Demo Version(http://sketchucation.com/click.php?url)]

      Latest updates, tutorials and artwork
      facebook.com/octane4sketchup

      1 Reply Last reply Reply Quote 0
      • tt_suT Offline
        tt_su
        last edited by

        It'd be good if you had a test model and some metrics to your existing code.

        1 Reply Last reply Reply Quote 0
        • dkendigD Offline
          dkendig
          last edited by

          materials = Sketchup.active_model.materials
          mat = materials.add "My New Material"

          #component instances
          Sketchup.active_model.selection.grep(Sketchup::ComponentInstance){|inst| inst.material = mat}
          #groups
          Sketchup.active_model.selection.grep(Sketchup::Group){|grp| grp.material = mat}
          #faces
          Sketchup.active_model.selection.grep(Sketchup::Face){|f| f.material = mat;f.back_material = mat}

          Devin Kendig
          Developer

          1 Reply Last reply Reply Quote 0
          • P Offline
            pgarmyn
            last edited by

            @fuzzybro
            Put your code within

            Sketchup.active_model.start_operation("actionName", true,false,false)
            ..... Your code.....
            Sketchup.active_model.commit_operation

            This wil enable 'undo' for 'actionName' and the second parameter (false) wil disable screenupdate after each change of the model, until 'commit_operation'. API documentation says This can result in much faster Ruby code execution if the operation involves updating the model in any way.

            1 Reply Last reply Reply Quote 0
            • fuzzybroF Offline
              fuzzybro
              last edited by

              Hi all,

              Totally forgot about the "start_operation" thing.
              It worked out like a charm.

              Thanks!

              Bro

              [OctaneRender for SketchUp Overview and Demo Version(http://sketchucation.com/click.php?url)]

              Latest updates, tutorials and artwork
              facebook.com/octane4sketchup

              1 Reply Last reply Reply Quote 0
              • dkendigD Offline
                dkendig
                last edited by

                good catch

                Devin Kendig
                Developer

                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