Applying new Material to a Selection is extremely slow
-
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 -
It'd be good if you had a test model and some metrics to your existing code.
-
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} -
@fuzzybro
Put your code withinSketchup.active_model.start_operation("actionName", true,false,false)
..... Your code.....
Sketchup.active_model.commit_operationThis 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.
-
Hi all,
Totally forgot about the "start_operation" thing.
It worked out like a charm.Thanks!
Bro
-
good catch
Advertisement