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

    [Code] material.delete

    Scheduled Pinned Locked Moved Developers' Forum
    17 Posts 3 Posters 3.2k 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.
    • TIGT Offline
      TIG Moderator
      last edited by TIG

      This adds a new Method to Sketchup's Material Class - 'delete'
      Usage: material.delete
      It changes any entities use that material to the default-material, and then removes that material from the model and the materials-browser.
      It has been streamlined as much as possible but will search potentially thousands of entities to remove that material...
      Edit:
      v1.1 has simplified defn changing and respond_to? material test - thanks to thomthom.
      v1.2 materials made into array to remove self.
      v1.3 empty temp groups used without detriment.
      v1.4 operation removed as it should be used with another one.
      material-delete.rb

      TIG

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

        If read the code right, you iterate all the entities of each component instance and group?
        A quicker way would be to just run over the entities in the model.definitions collection and then the model.entities.

        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

          It has to deal with nested groups and instances etc... It processes every instance as each could have different materials, if it hasn't processed an instance's definition (i.e. it's not in @done) it does that too, otherwise it skips it... Not sure if the other way'd be quicker ?

          TIG

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

            You don't need to dig into the nesting.

            Simplified example:

            
            model.entities.each { |e| e.material = nil if e.respond_to? ;material }
            model.definitions.each { |d|
                d.entities.each { |e| e.material = nil if e.respond_to? ;material }
            }
            
            

            This will strip the material of ALL entities that has a material property. Including the materials applied to ComponentInstances and Groups. No need to keep track of which definitions has already been processed. (of course, back_material will have to be accounted for.

            Other notes: won't your version choke if it runs into entities that doesn't have material property?

            And would it be quicker if the dummy geometry was a single edge instead of a face (four edges and a face)? Or even just empty dummy groups?

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

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

              Just to illustrate with some high-tech ASCII art:

              If you have a model with a entity tree like this: (Excluding edges)

              
              + Model
              + model.entities
              | Face1
              | Face2
              | Face3
              | Group1 [Def1]
              +--+ Face4
              |  | Face5
              |  | Instance3 [Def3]
              |  +--+ Face7
              |     | Face8
              |     | Face9
              | Group2 [Def1]
              +--+ Face4
              |  | Face5
              |  | Instance3 [Def3]
              |  +--+ Face7
              |     | Face8
              |     | Face9
              | Group3 [Def2]
              +--| Face6
              | Instance1 [Def3]
              +--| Face7
              |  | Face8
              |  | Face9
              | Instance2 [Def3]
              +--| Face7
                 | Face8
                 | Face9
              
              

              The you can see how you get all entities when you loop over model.entities. and model.definitions... entities:

              
              + model.entities
              | Face1
              | Face2
              | Face3
              | Group1 [Def1]
              | Group2 [Def1]
              | Group3 [Def2]
              | Instance1 [Def3]
              | Instance2 [Def3]
              
              + model.definitions
              +--+ Def1.entities
              |  | Face4
              |  | Face5
              |  | Instance3 [Def3]
              |
              +--+ Def2.entities
              |  | Face6
              |
              +--+ Def3.entities
                 | Face7
                 | Face8
                 | Face9
              
              

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

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

                Where does the face.copy method derive from? I can't find it in the API docs.

                The self_to_nil method, is that embedded in .delete? Local to that method only? Never seen that method nesting before. Didn't even know it was possible. Feels very much like Javascript that way.

                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

                  @thomthom said:

                  Where does the face.copy method derive from? I can't find it in the API docs.
                  The self_to_nil method, is that embedded in .delete? Local to that method only? Never seen that method nesting before. Didn't even know it was possible. Feels very much like Javascript that way.

                  The 'face.copy' is simply 'group.copy' - the group was called 'face' !!!
                  You can embed a def locally within another def - useful if you want to 'loop until' etc.

                  Having taken on board your comments I have tweaked the method... here's the updated one http://forums.sketchucation.com/viewtopic.php?p=166978#p166978

                  TIG

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

                    @tig said:

                    The 'face.copy' is simply 'group.copy' - the group was called 'face' !!!

                    Ah. didn't see that. Thought it maybe wes undocumented.

                    @tig said:

                    You can embed a def locally within another def - useful if you want to 'loop until' etc.

                    That sure is. Will have to remember that.

                    I think this method, and the layer version would fit well into the Skx library. Would you mind if this was included? I can add it to the repository for you if you don't want to bother with setting up Mercurial.

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

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

                      Just had a though. What happens if this method is called from another method that already has done a model.start_operation?

                      can operations be nested? Or will it interrupt the previous? Maybe one of the transparency flags (SU7 only) can be used?

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

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

                        That's a good point, Thom. I'm not sure start_operation belongs in specific methods.

                        I asked somewhere here if nesting start_operations was allowed, or what happens if they are nested. It's possible to write some "guards" for start_operation that prevent nesting - which may be interesting to do just to see what is really happening with all the scripts you have already installed.

                        Hi

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

                          I'm wondering if trans or prev_trans flag can be used to inject these types of methods. But I don't fully understand the difference between the two. And I'm also unsure of the warning for trans apply to prev_trans.

                          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

                            @unknownuser said:

                            ...
                            I think this method, and the layer version would fit well into the Skx library. Would you mind if this was included? I can add it to the repository for you if you don't want to bother with setting up Mercurial.

                            Please add them for me...

                            TIG

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

                              As Jim says, perhaps best to remove the start/commit_operations unless 'we' can work out how best to 'transparencify' them... Advice welcome...

                              TIG

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

                                Ok. I'll add it to the Skx. Thanks for sharing the snippet.

                                After this week I'll have some time to do some experiment with start operation. It'd be interesting if we could find a best practice.

                                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

                                  v1.3 now uses empty temp groups without detriment and so must be a little quicker too ... http://forums.sketchucation.com/viewtopic.php?p=166985#p166985

                                  TIG

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

                                    v1.4 - now has operation properly removed ! ... viewtopic.php?p=166985#p166985

                                    TIG

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

                                      Use v1.2: fixed an array bug.
                                      http://forums.sketchucation.com/viewtopic.php?p=166978#p166978

                                      TIG

                                      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