sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Script returns no errors but has no effect in sketchup

    Scheduled Pinned Locked Moved Developers' Forum
    9 Posts 3 Posters 226 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.
    • E Offline
      Eric_Erb
      last edited by

      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")
      end

      mod.entities.each do |entity|
      entity.material = my_materials[entity.layer.name]
      end

      mod.commit_operation
      end`

      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by

        You didn't call the method.

        Hi

        1 Reply Last reply Reply Quote 0
        • E Offline
          Eric_Erb
          last edited by

          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")
          end

          mod.entities.each do |entity|
          entity.material = my_materials[entity.layer.name]
          end

          mod.commit_operation
          end

          if( !$bs2roofLoaded )
          UI.menu("Plugins").add_item("BS Roof") { bs2roof }
          bs2roofLoaded = true
          end`

          1 Reply Last reply Reply Quote 0
          • J Offline
            Jim
            last edited by

            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.

            Hi

            1 Reply Last reply Reply Quote 0
            • E Offline
              Eric_Erb
              last edited by

              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")
              end

              mod.entities.each do |entity|
                  if entity.is_a? Sketchup::Drawingelement
              	entity.material = my_materials[entity.layer.name]
                  end
              end
              

              mod.commit_operation
              end

              UI.menu("Plugins").add_item("BS Roof") { bs2roof }`
              
              1 Reply Last reply Reply Quote 0
              • E Offline
                Eric_Erb
                last edited by

                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?

                1 Reply Last reply Reply Quote 0
                • Chris FullmerC Offline
                  Chris Fullmer
                  last edited by

                  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.

                  Lately you've been tan, suspicious for the winter.
                  All my Plugins I've written

                  1 Reply Last reply Reply Quote 0
                  • E Offline
                    Eric_Erb
                    last edited by

                    Perfect!!! Thank you Chris

                    1 Reply Last reply Reply Quote 0
                    • J Offline
                      Jim
                      last edited by

                      @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. 😄

                      Hi

                      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