Script returns no errors but has no effect in sketchup
-
My boss pointed out that there is an exception to the "materials are always the same" rule that you guys helped write a code for. I thought I'd just make a button to make the material change if it is needed. So I wrote the code below but when I run it in sketch up it says complete and returns no errors but has no effect on my model. Where did I screw it up?
` require 'sketchup'
def BS2Roof
mod = Sketchup.active_model
su_materials = mod.materials
my_materials = Hash.new
my_materials["Roof_Surface"] = su_materials["BS"]begin
mod.start_operation("Change Roof", true)
rescue ArgumentError
mod.start_operation("Change Roof")
endmod.entities.each do |entity|
entity.material = my_materials[entity.layer.name]
endmod.commit_operation
end` -
You didn't call the method.
-
You know when someone points something like that out you can't help but think "I'm....Dumb" I actually cut it out to get rid of the add to plugin menu thing. The new problem is it turns the outside of my model white even thought the material is Burnished Slate (BS) in color. (The inside of my model retains it's materials"
` require 'sketchup'
def bs2roof
mod = Sketchup.active_model
su_materials = mod.materials
my_materials = Hash.new
my_materials["Roof_Surface"] = su_materials["BS"]begin
mod.start_operation("Change Roof", true)
rescue ArgumentError
mod.start_operation("Change Roof")
endmod.entities.each do |entity|
entity.material = my_materials[entity.layer.name]
endmod.commit_operation
endif( !$bs2roofLoaded )
UI.menu("Plugins").add_item("BS Roof") { bs2roof }
bs2roofLoaded = true
end` -
I can only go by what information I have. As a professional trouble-shooter, my first question when something doesn't work is typically "Did you plugin it? Did you turn it on?" And from my own experience, I am the first to forget them.
-
Well it shows up just as it should in my pluggins menu. I have my material called BS loaded in the template so it's in the active model materials. I'm sure there is a big hint in your reply to my post but I just don't know enough to discern what you're telling me. For crying out loud I wrote a script with a method that I never called on and wondered why it didn't do anything.
I'm getting closer. The new code changes the roof to the BS material, but is still turning the other stuff white.
` require 'sketchup'
def bs2roof
mod = Sketchup.active_model
su_materials = mod.materials
my_materials = Hash.new
my_materials["Roof_surface"] = su_materials["BS"]begin
mod.start_operation("Change Roof", true)
rescue ArgumentError
mod.start_operation("Change Roof")
endmod.entities.each do |entity| if entity.is_a? Sketchup::Drawingelement entity.material = my_materials[entity.layer.name] end end
mod.commit_operation
endUI.menu("Plugins").add_item("BS Roof") { bs2roof }`
-
I think the problem with my code is that I'm still looking at all the layer names and applying materials to the components within those layers according to the layers name. Therefore when It finds a layer that doesn't have a material associated with it it applies white. So what I need to do is either write a script that specifically looks for a layer named Roof_surface and apply materials to its components or add a few lines to my script that says "if there is no material associated with this layer name do nothing" Anybody know how to do that?
-
Try changing the line:
entity.material = my_materials[entity.layer.name]
to
entity.material = my_materials[entity.layer.name] if my_materials[entity.layer.name]
That should make it so that it only applies the material if the material is not "nil".
Chris
EDIT: Note that the amended code I posted should all be on one line - its a single line of code.
-
Perfect!!! Thank you Chris
-
@eric_erb said:
I'm sure there is a big hint in your reply to my post but I just don't know enough to discern what you're telling me.
No, I did not mean to say that. I replied to the wrong message - I should have tried more to answer the question.
Advertisement