sketchucation logo sketchucation
    • 登入
    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!
    ⚠️ Important | Libfredo 15.6b introduces important bugfixes for Fredo's Extensions Update

    Looking to make a script, but dunno how.

    已排程 已置頂 已鎖定 已移動 Developers' Forum
    34 貼文 7 Posters 1.8k 瀏覽 7 Watching
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • A 離線
      agamemnus
      最後由 編輯

      I want to make two scripts. In one script you would be able to select a series of components and add it to the memory of the script. Then you would be able to select another bunch of components and the script would convert each component to the ones in the memory, randomly and evenly distributed per component-in-memory.

      The second script would be similar except I would be adding to memory colors and randomly replacing colors (of a set of groups or components) instead of replacing component geometry.

      I have no idea what the commands for this would be.

      I'm looking for commands that would let me do pretty much all of this... I looked at
      http://code.google.com/apis/sketchup/docs/index.html and really I'm quite lost as to which commands to use.

      1 條回覆 最後回覆 回覆 引用 0
      • Chris FullmerC 離線
        Chris Fullmer
        最後由 編輯

        Have you succesfully made any little scripts or anything yet? Do you have Jim Ruby Web Console installed?

        For the first script, the easiest way to implement it would be to make it have 2 parts. The user selects base components and activates the first plugin - remember base components. The end. Then the user selects target components and activates the 2nd part of the plugin.

        This was you can avoid having to learn how to write a selector tool, which is a major pain.

        The second plugin would work similarly. Have you looked at my Extrapolate Colrs plugin? It does almost what you want, but it does not work on groups/components because that takes time to program. But you can go into a group or component and run the script and it works inside the group that way. Check it out, the source is open so you can look at what it does. I was also about to do a video tutorial on how to write that exact script. So I'll post a link when I do.

        Also, I have 2 videos currently on my YouTube channel if you wan to watch about beginning ruby.

        Chris

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

        1 條回覆 最後回覆 回覆 引用 0
        • GaieusG 離線
          Gaieus
          最後由 編輯

          OK Chris, I now know why I don't ask questions like agamemnus 😄

          (sorry agamemnus - no offense meant to you but just realising my limits)

          Gai...

          1 條回覆 最後回覆 回覆 引用 0
          • Chris FullmerC 離線
            Chris Fullmer
            最後由 編輯

            oh Gaieus, you can do it! Its not too late to build some way cool Gaieus Toolset for SU 😄

            Then you can present it at the next basecamp!

            Chris

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

            1 條回覆 最後回覆 回覆 引用 0
            • GaieusG 離線
              Gaieus
              最後由 編輯

              No way Chris, that would be waste of resources. Like if (instead of working hard on your next, cool plugin) you dealt with stupid spammers and kept banning them.

              everyone does his own job. Mine in this subforum is to be amazed and enjoy every new gem.
              😉

              Gai...

              1 條回覆 最後回覆 回覆 引用 0
              • C 離線
                cjthompson
                最後由 編輯

                are you looking for a script to do what you ask for, or are you asking for a tutorial on how to do it?

                1 條回覆 最後回覆 回覆 引用 0
                • A 離線
                  agamemnus
                  最後由 編輯

                  Either. Anyway, here's what I have now. I already have a problem I can't figure out-- I can't get both IF statements to work in the second def. It will skip the second if statement as of now... dunno why.

                  
                  require 'sketchup.rb'
                  
                  module Convert_components_randomly
                  
                   def Convert_components_randomly.choosebase
                    base_entities = Sketchup.active_model.selection
                  
                    if base_entities.empty?
                     results = UI.messagebox "Please select base groups and/or components first."
                     Sketchup.send_action "selectSelectionTool;"
                     return
                    end
                  
                   end
                  
                  
                   def Convert_components_randomly.choosetarget
                    target_entities = Sketchup.active_model.selection
                  
                    if target_entities.empty?
                     results = UI.messagebox "Please select target groups and/or components first."
                     Sketchup.send_action "selectSelectionTool;"
                     return
                    end
                  
                    if Convert_components_randomly.choosebase.base_entities.empty?
                     results = UI.messagebox "Please select base groups and/or components first."
                     Sketchup.send_action "selectSelectionTool;"
                     return
                    end
                  
                   end
                  
                  end #Convert_components_randomly module
                  
                  
                  convert_components_randomly_menu = UI.menu("Plugins").add_submenu("Convert Components Randomly")
                  convert_components_randomly_menu.add_item("Select base components") {Convert_components_randomly.choosebase}
                  convert_components_randomly_menu.add_item("Select components to convert") {Convert_components_randomly.choosetarget}
                  
                  
                  1 條回覆 最後回覆 回覆 引用 0
                  • Chris FullmerC 離線
                    Chris Fullmer
                    最後由 編輯

                    The if statement is invalid:

                    if Convert_components_randomly.choosebase.base_entities.empty?

                    That is calling a method (which does not exist). It needs to looking at an array. So in your choosebase method, you should make an array that holds all the selected components.

                    Then in this method, you can determine if that array is empty. You might need to make your component selection be a global variables. So change base_entities to $base_entities in the first method. Then change the if statement to:

                    if $base_entities.empty?

                    That might begin to get you somewhere. But I have a feeling you are going to run into some more problems with the selection set. base_entities is currently pointing at the selection object - it is not pointing to an array of objects that are selected. Which is different. So maybe change line 6 to read:

                    $base_entities = Sketchup.active_model.selection.to_a

                    Now your base_entities is an array of SketchUp entities. And hopefully now when you get to the "if" statement, it will recognize your if statement as a valid statement, and parse it correctly.

                    But I have not tested any of this, so it might need tweaks still.

                    Chris

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

                    1 條回覆 最後回覆 回覆 引用 0
                    • thomthomT 離線
                      thomthom
                      最後由 編輯

                      global variable? I'd recommend an module variable instead.

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

                      1 條回覆 最後回覆 回覆 引用 0
                      • Chris FullmerC 離線
                        Chris Fullmer
                        最後由 編輯

                        I never played with the @ and @@ variables inside of modules. Do they work as expected? Will his script remember the value over multiple script sessions?

                        Chris

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

                        1 條回覆 最後回覆 回覆 引用 0
                        • thomthomT 離線
                          thomthom
                          最後由 編輯

                          Use @ variable within a module and it's global within that module and it doesn't interfere with any other scripts.

                          
                          module MyModule
                            @module my_var = 'Hello World'
                          
                            def self.foo
                              return @my_var
                            end
                          end
                          
                          

                          For classes - have a look at this: http://sporkmonger.com/2007/2/19/instance-variables-class-variables-and-inheritance-in-ruby

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

                          1 條回覆 最後回覆 回覆 引用 0
                          • M 離線
                            MartinRinehart
                            最後由 編輯

                            Agamemnus,

                            Having Chris Fullmer help you with Ruby programming is like having Roger Federer help you with your tennis. Beyond what money could buy.

                            Sorry about the off topic stuff, but here's some more:

                            @chris fullmer said:

                            oh Gaieus, you can do it! Its not too late to build some way cool Gaieus Toolset for SU 😄

                            Gaieus, Chris is so right! I'm working on my tutorial's Ruby half (yes, half!). See if you can read this script:

                            
                            # /t/stairs1.rb - a stairway in SketchUp Ruby Talk (aka SketchTalk)
                            
                            =begin
                            The following bit of SketchTalk builds a 36"-wide stairway. Rise is 7 inches; run is 9. Built from 1.5" thick lumber. It has 15 steps. It has a 1.5 inch square banister on the left. The banister is supported by 1x1 balusters.
                            =end
                            
                            require '/t/sketch_talk'
                            
                            n # my keyboard shortcut for File/New
                            
                            box [0,0,0], [36,1.5,0], 5.5 # riser
                            box [0,0,5.5], [36,10.5,5.5], 1.5 # tread
                            
                            box [1.25,1,7], [2.25,2,7], 38 # ballusters
                            box [1.25,5.5,7], [2.25,6.5,7], 38+(7.0/2)
                            
                            all # select all 
                            step = g 'step' # turn selection into a component named 'step'
                            
                            mc step, [0,9,7], 14 # Move/Copy, 14x
                            
                            box [1,0,36+7], [2.5,15*9,36+7+15*7], 1.5 # bannister
                            
                            

                            I'll bet you can read that script. I'll bet you can't build the model half that fast any other way!

                            Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

                            1 條回覆 最後回覆 回覆 引用 0
                            • A 離線
                              agamemnus
                              最後由 編輯

                              The first script is almost done. I have a few problems --

                              1. I could not find a way to convert groups to components so I changed them to unique components and then changed the definition of THOSE to the randomly selected base component. Isn't there a more efficient way?

                              2. I can't seem to convert the target groups to components fast enough -- I used 'if (current_entity.typename == "Group") // current_entity.to_component // end', but that only applies at the end of the script so I have to run it twice.. dunno how to make it convert instantly. Tried shoving "Sketchup.active_model.commit_operation" after and then converting the unique components to base components but it won't budge.

                              3. When I do "@target_entities = target_entities", and I already had a non-empty @target_entities, am I leaking?

                              4. (mini-gripe) The rand function is glitchy. Sometimes it gives several (3-4) repeats in a 90-size or so list, which has an incredibly low chance of occurring.

                              
                              require 'sketchup.rb'
                              
                              module Convert_objects_randomly
                              
                               @base_entities = []
                               @target_entities = []
                              
                               def Convert_objects_randomly.choosebase
                                @base_entities = Sketchup.active_model.selection.to_a
                                if @base_entities.empty?
                                 results = UI.messagebox "Please select base groups and/or components first."
                                 Sketchup.send_action "selectSelectionTool;"
                                 return
                                end
                               end
                              
                              
                               def Convert_objects_randomly.choosetarget
                                target_entities = Sketchup.active_model.selection.to_a
                                if @base_entities.empty?
                                 results = UI.messagebox "Please select base groups and/or components first."
                                 Sketchup.send_action "selectSelectionTool;"
                                 return
                                end
                              
                                if target_entities.empty?
                                 if @target_entities.empty?
                                 results = UI.messagebox "Please select target groups and/or components first."
                                 Sketchup.send_action "selectSelectionTool;"
                                 return
                                 end
                                 target_entities = @target_entities
                                end
                              
                                target_entities.each do |current_entity|
                                 if (current_entity.typename == "Group")
                                  current_entity.to_component
                                 end
                                end
                              
                                Sketchup.active_model.commit_operation
                                 
                                target_entities.each do |current_entity|
                                 if (current_entity.typename == "Group") or (current_entity.typename == "ComponentInstance")
                                  current_entity.make_unique
                                  current_entity.definition = @base_entities[rand(@base_entities.length)].definition
                                 end 
                                end
                              
                                @target_entities = target_entities
                                Sketchup.active_model.commit_operation
                              
                               end
                              
                              end #Convert_objects_randomly module
                              
                              
                              Convert_objects_randomly_menu = UI.menu("Plugins").add_submenu("Convert Objects Randomly")
                              Convert_objects_randomly_menu.add_item("Select base objects") {Convert_objects_randomly.choosebase}
                              Convert_objects_randomly_menu.add_item("Convert selected to random base objects") {Convert_objects_randomly.choosetarget}
                              
                              
                              file_loaded(__FILE__)
                              
                              
                              1 條回覆 最後回覆 回覆 引用 0
                              • A 離線
                                agamemnus
                                最後由 編輯

                                Thanks. I couldn't do it with "@module base_entities = '' "-- that gave me an error. I used " @base_entities = '' ". And looks like I need that " @base_entities ='' " or else something weird happens (maybe undefineds everywhere?) and the if statement for base_entities did not work. (it does now, of course)

                                So now I have to figure out how to iterate through target_entities and convert each to a random base_entities ..

                                There isn't a native function I could use?

                                Edit: Figured it out mostly... See post 1 below.

                                1 條回覆 最後回覆 回覆 引用 0
                                • Chris FullmerC 離線
                                  Chris Fullmer
                                  最後由 編輯

                                  #1 groups have the .to_component method that will turn them into a component. But it makes each group a unique component, because there is no built in way to determine if a group is an identical copy of another group. There is some work arounds that can be done on certain groups that are exact copies of another group, but generally speaking, groups can't be turned easily into a single component and instances.

                                  #2 - I'll have to look at later

                                  #3 - The @target_entities will hold its value until SketchUp is closed, after it is given a value once. So you should clear it after your script is run each time. Add a @taget_entities = [] to the end of your script. That is my gess as to what is happening.

                                  #4 - Yeah, that happens. Adam Billyard once took some time to explain about how random functions work in programming. It was eye opening....or rather, my eyes popped out from all the terms he used that didn't make any sense to me. But essentially random is a tricky thing to nail perfectly I guess.

                                  Sounds like its coming along well!

                                  Chris

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

                                  1 條回覆 最後回覆 回覆 引用 0
                                  • A 離線
                                    agamemnus
                                    最後由 編輯

                                    @chris fullmer said:

                                    #3 - The @target_entities will hold its value until SketchUp is closed, after it is given a value once. So you should clear it after your script is run each time. Add a @taget_entities = [] to the end of your script. That is my gess as to what is happening.

                                    Well, I know that it holds its value -- that was a way to re-randomize if the user wanted to without selecting those same groups again. But, if I set @target_entities to something else and it (@target_entities) is already set to something, that won't cause a leak, right? (the array it was previously set to will be cleared from memory?)

                                    1 條回覆 最後回覆 回覆 引用 0
                                    • Chris FullmerC 離線
                                      Chris Fullmer
                                      最後由 編輯

                                      Ahh, correct. If you set it to seomthing new, then it clears its old value.

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

                                      1 條回覆 最後回覆 回覆 引用 0
                                      • thomthomT 離線
                                        thomthom
                                        最後由 編輯

                                        Ruby uses garbage collection - so it keeps track of all the junk we throw about and sweeps it all away! 😄

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

                                        1 條回覆 最後回覆 回覆 引用 0
                                        • C 離線
                                          cjthompson
                                          最後由 編輯

                                          @agamemnus said:

                                          1. I can't seem to convert the target groups to components fast enough -- I used 'if (current_entity.typename == "Group") // current_entity.to_component // end', but that only applies at the end of the script so I have to run it twice.. dunno how to make it convert instantly. Tried shoving "Sketchup.active_model.commit_operation" after and then converting the unique components to base components but it won't budge.

                                          the reason why is because .to_component creates a new entity.
                                          Try current_entity = current_entity.to_component.

                                          1 條回覆 最後回覆 回覆 引用 0
                                          • thomthomT 離線
                                            thomthom
                                            最後由 編輯

                                            Also .typename is very slow.
                                            Instead of current_entity.typename == "Group" use current_entity.is_a?(Sketchup::Group).
                                            http://forums.sketchucation.com/viewtopic.php?f=180&t=19576&start=0#p162235

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

                                            1 條回覆 最後回覆 回覆 引用 0
                                            • 1
                                            • 2
                                            • 1 / 2
                                            • 第一個貼文
                                              最後的貼文
                                            Buy SketchPlus
                                            Buy SUbD
                                            Buy WrapR
                                            Buy eBook
                                            Buy Modelur
                                            Buy Vertex Tools
                                            Buy SketchCuisine
                                            Buy FormFonts

                                            Advertisement