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

    Directly apply a material

    Scheduled Pinned Locked Moved Plugins
    4 Posts 3 Posters 248 Views 3 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
      rossthompson
      last edited by

      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	 
      
      
      
      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        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 use if not]... then it sets it to a new material named 'Window' [ add]...

        TIG

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

          Hi, that worked perfectly

          Thanks TIG

          1 Reply Last reply Reply Quote 0
          • thomthomT Offline
            thomthom
            last edited by

            Can also be written like so:
            m = materials['Door'] || materials.add('Door')

            Thomas Thomassen β€” SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            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