Directly apply a material
-
Hi,
I am working on a script that activates the paint bucket tool with a predefined material of a certain colour and transparency. The code I have written works fine except on every use it creates a copy of the material eg. 'window1', 'window2' etc. which makes a bit of a mess of the materials palette.I think it just needs a simple if statement to avoid creating the material if it already exists. Does anyone know how to do this?
Thanks Ross
toolbar = UI;;Toolbar.new "Window" cmd = UI;;Command.new("Test") { window } cmd.small_icon = "CBG/window_sm.png" cmd.large_icon = "CBG/window.png" cmd.tooltip = "Paint Window" toolbar = toolbar.add_item cmd toolbar.show def window model = Sketchup.active_model materials = model.materials m = materials.add "Window" # Adds a material to the "in-use" material pallet. m.color = Sketchup;;Color.new(100, 150, 240) # Specify the colour color = m.color alpha = m.alpha=0.5 # Set transparency materials.current = materials["Window"] # Set to the current material Sketchup.send_action "selectPaintTool;" # Start the paint tool end
-
Try something like this:
m=materials.add("Window") unless m=materials["Window"]
Which sets 'm
' to be an existing material named 'Window', but if it fails [unless
or should you prefer it useif not
]... then it sets it to a new material named 'Window' [add
]... -
Hi, that worked perfectly
Thanks TIG
-
Can also be written like so:
m = materials['Door'] || materials.add('Door')
Advertisement