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

    Ruby script for selecting color

    Scheduled Pinned Locked Moved Developers' Forum
    62 Posts 4 Posters 1.9k Views 4 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.
    • nithi09N Offline
      nithi09
      last edited by

      Thanks TIG
      I tried when I use sketchup inputbox popup use defalt nothing happening. when I choose custom it gives mesage "cannot convert Custom to length" could you tell me why?

      <span class="syntaxdefault"></span><span class="syntaxkeyword">require&nbsp;</span><span class="syntaxstring">'Sketchup.rb'<br /><br /></span><span class="syntaxdefault">def&nbsp;testbox1&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ent&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">entities&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxcomment">#ss&nbsp;=&nbsp;model.selection<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#obj&nbsp;=&nbsp;ss[3]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">prompts&nbsp;</span><span class="syntaxkeyword">=&nbsp;[</span><span class="syntaxstring">"Width"</span><span class="syntaxkeyword">,</span><span class="syntaxstring">"Depth"</span><span class="syntaxkeyword">,</span><span class="syntaxstring">"Height"</span><span class="syntaxkeyword">,</span><span class="syntaxstring">"Colour"</span><span class="syntaxkeyword">]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">defaults&nbsp;</span><span class="syntaxkeyword">=&nbsp;[</span><span class="syntaxdefault">6.feet</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">4.feet</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">2.feet</span><span class="syntaxkeyword">,</span><span class="syntaxstring">'Red'</span><span class="syntaxkeyword">]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list&nbsp;=&nbsp;[[</span><span class="syntaxstring">"6'|8'|10'|Custom"</span><span class="syntaxkeyword">],</span><span class="syntaxstring">""</span><span class="syntaxkeyword">,</span><span class="syntaxstring">""</span><span class="syntaxkeyword">,[</span><span class="syntaxstring">"Blue|Red|Green"</span><span class="syntaxkeyword">]]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">results&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">inputbox&nbsp;prompts</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">defaults</span><span class="syntaxkeyword">,&nbsp;list,&nbsp;</span><span class="syntaxstring">"Test_Box"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">return&nbsp;if&nbsp;</span><span class="syntaxdefault">not&nbsp;results<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">results</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;</span><span class="syntaxdefault">x&nbsp;</span><span class="syntaxkeyword">==&nbsp;</span><span class="syntaxstring">"Custom"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">resultsx&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">inputbox&nbsp;</span><span class="syntaxkeyword">([</span><span class="syntaxstring">"Enter&nbsp;Width;"</span><span class="syntaxkeyword">],&nbsp;[</span><span class="syntaxstring">"1'"</span><span class="syntaxkeyword">],&nbsp;</span><span class="syntaxstring">"Custom&nbsp;Size"</span><span class="syntaxkeyword">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span class="syntaxdefault">nil&nbsp;</span><span class="syntaxkeyword">if&nbsp;</span><span class="syntaxdefault">not&nbsp;resultsx<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">resultsx</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">to_l<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">else<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">x&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">x</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">to_l<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wid&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">x<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dep&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">results</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">hei&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">results</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">color&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">results</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">3</span><span class="syntaxkeyword">]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">pts&nbsp;</span><span class="syntaxkeyword">=&nbsp;[[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">],&nbsp;[</span><span class="syntaxdefault">wid</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">],&nbsp;[</span><span class="syntaxdefault">wid</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">dep</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">],&nbsp;[</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">dep</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">]]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">base&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">ent</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_face&nbsp;pts<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hei&nbsp;</span><span class="syntaxkeyword">=&nbsp;-</span><span class="syntaxdefault">hei&nbsp;</span><span class="syntaxkeyword">if&nbsp;(</span><span class="syntaxdefault">base</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">normal</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">dot</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">Z_AXIS</span><span class="syntaxkeyword">)&nbsp;<&nbsp;</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxcomment">#color&nbsp;=&nbsp;nil&nbsp;if&nbsp;color&nbsp;==''<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">base</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">back_material&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">color<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxcomment">#base.reverse!<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">base</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">pushpull&nbsp;hei<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;end<br /><br /><br /><br /></span><span class="syntaxkeyword">if&nbsp;(</span><span class="syntaxdefault">not&nbsp;$Testbox_menu_loaded</span><span class="syntaxkeyword">)<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;</span><span class="syntaxdefault">UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Tools"</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"TEST_BOX"</span><span class="syntaxkeyword">)&nbsp;{</span><span class="syntaxdefault">testbox1</span><span class="syntaxkeyword">}<br />&nbsp;&nbsp;</span><span class="syntaxdefault">$Testbox_menu_loaded&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">true<br />end</span>
      
      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        @nithi09 said:

        I tried when I use sketchup inputbox popup use defalt nothing happening. when I choose custom it gives mesage "cannot convert Custom to length" could you tell me why?

        Because you set the default value of the box to an object of the type Length (6.feet) - this means that the input box will try to convert the return value to be of the same type.

        But "Custom" cannot be converted into a valid Length object.

        So instead you'd need to use strings as the default value and handle the conversion back to Length as needed.

        Btw, it's a lot easier to read your code if you wrap it in [code] tags. (I've done it for you so far.)

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

        1 Reply Last reply Reply Quote 0
        • TIGT Offline
          TIG Moderator
          last edited by

          Well done TT - I didn't spot the mismatch between 6.feet length and "6'" string.
          If you are making a dropdown list it needs to be text, that you convert to a length later...

          TIG

          1 Reply Last reply Reply Quote 0
          • TIGT Offline
            TIG Moderator
            last edited by

            Not
            require 'Sketchup.rb' class Testbox def testbox1
            ...
            Try
            require 'Sketchup.rb' class Testbox def **initialize**()
            ...
            Then use Testbox.new() which will run the initialize...

            TIG

            1 Reply Last reply Reply Quote 0
            • nithi09N Offline
              nithi09
              last edited by

              Thanks you for you help. it's works

              1 Reply Last reply Reply Quote 0
              • nithi09N Offline
                nithi09
                last edited by

                Hi I want to add texture for this box I want to see this box look like glass box any Idea?
                thanks

                1 Reply Last reply Reply Quote 0
                • TIGT Offline
                  TIG Moderator
                  last edited by

                  If you have a glass texture already existing use that by 'Name'.

                  If you have added a 'color' material you can get a reference to that after you have made it/added it to a face: mat=base.material and then use mat.alpha=0.5 to add opacity < 1... aka transparency

                  TIG

                  1 Reply Last reply Reply Quote 0
                  • nithi09N Offline
                    nithi09
                    last edited by

                    Hi TIG it's working. but I want to see both way. I mean from outside to inside the box see through fine but inside to outside not see through how do I can do both way? or where do I can find clear glass texture
                    Thanks

                    1 Reply Last reply Reply Quote 0
                    • TIGT Offline
                      TIG Moderator
                      last edited by

                      When you give the first face ['base' ?] its material you need to also apply it to the back face thus:
                      base.material = 'xxxx' base.**back_material** = 'xxxx'
                      where 'xxxx' is the material...

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • nithi09N Offline
                        nithi09
                        last edited by

                        Hi TIG how do I can add glass texture to only one wall of box out of four walls.above method applying whole box. I like to try only one wall glass.thanks

                        1 Reply Last reply Reply Quote 0
                        • TIGT Offline
                          TIG Moderator
                          last edited by

                          Well... you need to work out how to 'find' the face first, then once you have it give it a [preexisting] material - let's assume it's name is 'Glass'...
                          A face has a 'normal' - which the vector that tells us which way a face is looking...
                          So let's say the collection of faces [along with edges etc] is in ` - then you need to filter that to find just the faces and then from that exact face with a particular 'normal'...
                          Let's also assume it is a box so only one matching face.normal will be found...

                          group.entities.grep(Sketchup;;Face).each{|face|
                            if face.normal==Z_AXIS ### == faces 'UP'... BUT you can use any vector to compare
                              face.material='Glass'
                              face.back_material='Glass'
                              break ### stop looking
                            end
                          }
                          

                          group.entities.grep(Sketchup;;Face).each{|face|
                          if face.normal==Z_AXIS ### == faces 'UP'... BUT you can use any vector to compare
                          face.material='Glass'
                          face.back_material='Glass'
                          break ### stop looking
                          end
                          }[/code:ngl1vctf]

                          TIG

                          1 Reply Last reply Reply Quote 0
                          • nithi09N Offline
                            nithi09
                            last edited by

                            Hi TIG how do I find preexisting material on my box? is not 'Glass' thx

                            1 Reply Last reply Reply Quote 0
                            • TIGT Offline
                              TIG Moderator
                              last edited by

                              Your materials have names.
                              I used 'Glass' as an example.
                              The material must already exist in the model.
                              [I am skipping explaining how to create a material if it doesn't already exist, change its RGB and opacity etc... that wasn't your original question!]

                              So use any existing material you want - just pass its 'name'... 😕

                              TIG

                              1 Reply Last reply Reply Quote 0
                              • nithi09N Offline
                                nithi09
                                last edited by

                                Hi TIG What I am trying to do above creating box one side of box look like see through glass and rest solid coloured any idea???

                                1 Reply Last reply Reply Quote 0
                                • TIGT Offline
                                  TIG Moderator
                                  last edited by

                                  An image of what you expect might help us help you, we are not mind-readers.

                                  I have given you a general framework for code that finds "any face" by its direction and gives it a material.
                                  The example I gave was for face.normal==Z_AXIS
                                  which means it faces directly upwards,
                                  BUT you can compare it to any axis
                                  X_AXIS
                                  Y_AXIS
                                  and for the 'opposite direction'...
                                  X_AXIS.reverse
                                  Y_AXIS.reverse
                                  Z_AXIS.reverse

                                  You need to find and read code examples more....

                                  TIG

                                  1 Reply Last reply Reply Quote 0
                                  • nithi09N Offline
                                    nithi09
                                    last edited by

                                    Hi TIG could you recommend any good book specific for sketchup with code example. I have a book called "Automatic Sketchup" by Matthew Scarpino. It's not enough detail for me.

                                    1 Reply Last reply Reply Quote 0
                                    • TIGT Offline
                                      TIG Moderator
                                      last edited by

                                      @nithi09 said:

                                      Hi TIG could you recommend any good book specific for sketchup with code example. I have a book called "Automatic Sketchup" by Matthew Scarpino. It's not enough detail for me.
                                      That's an excellent start.
                                      Also get examples of others scripts [not too complex to start] and see how others do it...

                                      TIG

                                      1 Reply Last reply Reply Quote 0
                                      • nithi09N Offline
                                        nithi09
                                        last edited by

                                        Hi TIG It's working thanks.
                                        How do I choose specific one face If I have multiple faces in same direction

                                        1 Reply Last reply Reply Quote 0
                                        • TIGT Offline
                                          TIG Moderator
                                          last edited by

                                          Find something unique about that face - like its bounds.min x/y/z ?

                                          TIG

                                          1 Reply Last reply Reply Quote 0
                                          • TIGT Offline
                                            TIG Moderator
                                            last edited by

                                            The sloping, upward looking face will have a normal.z>0 BUT ALSO normal.z<1, it also has a normal.x<0 - so that finds that kind of face ?
                                            The triangular face has normal.x==1, BUT so does the rectangular one next to it.
                                            You can differentiate them using face.vertices.lengthon each, ==3 for the triangle and ==4 for the rectangle ?
                                            Or if the difference is that it is below the red axis, testing face.bounds.min.y<0 will find it...

                                            Tip on bb=object.bounds
                                            bb.width is in X
                                            bb.height is in Y
                                            bb.depth is in Z
                                            this is NOT as you might initially assume - 'height' is NOT the Z - the bounds conventions come from the older CAD concept, where width/height are from the screen, when you typically looked at an object in plan, from above; and the 'depth' is the extent it projected 'out-of-the-screen'. 3D modelers like SketchUp show axes in 3d with Z typically up the screen rather that 'out-of-it'... bb.min, bb.max and bb.center etc should be self-evident ?

                                            TIG

                                            1 Reply Last reply Reply Quote 0
                                            • 1
                                            • 2
                                            • 3
                                            • 4
                                            • 4 / 4
                                            • First post
                                              Last post
                                            Buy SketchPlus
                                            Buy SUbD
                                            Buy WrapR
                                            Buy eBook
                                            Buy Modelur
                                            Buy Vertex Tools
                                            Buy SketchCuisine
                                            Buy FormFonts

                                            Advertisement