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

    Problem with group.copy

    Scheduled Pinned Locked Moved Developers' Forum
    20 Posts 7 Posters 643 Views 7 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

      But simply typing $beam_group OR my_copy=$beam_group.copy will BOTH return nil IF $beam_group is not being predefined ?? 😕
      So the issue is that $beam_group is not defined... NOT that group.copy is broken 😒

      TIG

      1 Reply Last reply Reply Quote 0
      • Al HartA Offline
        Al Hart
        last edited by

        I do not get nil if I type in $xxx.copy where $xxx is not defined:

        @unknownuser said:

        $xxx
        nil
        $xxx.copy
        Error: #<NoMethodError: undefined method `copy' for nil:NilClass>
        (eval):95

        However, I agree that it would have been helpful if he had typed in $beam_group for me.

        Al Hart

        http:wiki.renderplus.comimageseefRender_plus_colored30x30%29.PNG
        IRender nXt from Render Plus

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

          But if $xxx is defined [as something other than nil] but not a group, BUT it's an object that can use a method .copy - then it might return nil ?
          First you need to see if the Sxxx is a 'group'...
          If it's a component-instance then copy it will also work if DC tools are loaded because that adds .copy to the component-instance class too...
          What other 'class' could it be that takes .copy and returns nil?
          Perhaps he has a third party script that ill-advisedly refines the group.copy method and makes it return nil ???
          I have a pretty full house of scripts and Baregrep reveals a few with their own .copy methods including my SKMtools that adds image.copy to mimic group.copy; @Last's ../Ruby_Tests/camera.rb also has a .copy method to copy a camera - as well as ftools, fileutils and SketchyPhysics which also use .copy methods, but these are entirely within their own classes outside of SUp's own ??

          TIG

          1 Reply Last reply Reply Quote 0
          • S Offline
            scottbattersby
            last edited by

            All Hail TIG!

            I have key-shaped bruises on my forehead after spending a morning trying to work out why group.copy was returning nil when applied to a perfectly normal group.

            TIG writes : "Perhaps he has a third party script that ill-advisedly refines the group.copy method and makes it return nil ???"

            So I take out all scripts from my plugins directory and Hey Presto! suddenly group.copy works as advertised.

            Gingerly replacing them and testing, I found the culprit to be Sketchy Physics 3.1

            I take the file sketchyphysics.rb out of my plugins, group copy method works fine

            I put it back in, group copy method returns nil

            I take it out, copy works

            I put it in, copy returns nil

            I feel better.

            So some keywords to help other poor souls :

            group copy method failing returning nil not working stuffed buggered broken

            I have sketchup 8.0.11752 free by the way

            1 Reply Last reply Reply Quote 0
            • M Offline
              mptak
              last edited by

              Enjoyed the thread and look forward to the maintenance revision of Sketchy Physics as I presume it was some of its redefinition/augmentation of classes which led to this snafu. I think removing SP should be part of any troubleshooting until it is made a bit tighter. Great program though.

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

                This could have been my problem back in June as well.

                Can you think of any way to lock a method stoat other rubies cannot override it. (This is mch worse than adding a new method to an existing class.) then SU could just lock aloof its methods.

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

                  The author of SketchyPhysics is aware of the issues and has said these will be addressed in the next update. It doesn't even use the changed group.copy method anyway 😒

                  In the meantime rather than using
                  gcopy = group.copy
                  Which should return a reference to the new instance of the group, but might return 'nil' if SP messes with it... in your code consider something like this instead to side step it:
                  gcopy = group.parent.entities.add_instance(group.entities.parent, group.transformation)
                  This return the instance, placing the copy immediately over the original group, in the same context.

                  Incidentally, you can also use this method to 'duplicate' a group inside another context, like:
                  gcopy = someother_entities.add_instance(group.entities.parent, group.transformation)
                  [Note: you might need to adjust 'transformation' depending on the contexts involved]
                  To 'move the group' add group.erase!; group = gcopy - this will then appear that the group has moved into the new entities [like Edit>Cut...Edit>PasteInPlace].
                  The second step re-references 'group' to point to the new copy, as its 'id' etc will have changed too, this way it keeps an enduring reference, useful if you are to do something with it later on.

                  TIG

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

                    @scottbattersby said:

                    I have key-shaped bruises on my forehead after spending a morning trying to work out why group.copy was returning nil when applied to a perfectly normal group.

                    TIG writes : "Perhaps he has a third party script that ill-advisedly refines the group.copy method and makes it return nil ???"

                    So I take out all scripts from my plugins directory and Hey Presto! suddenly group.copy works as advertised.

                    I was reading this thread and was just about to make a comment about group.copy being modified by some plugin - though I could not remember which one. Glad you pinned it down.

                    As TIG said, SketchyPhysics is being reworked to avoid such clashes.

                    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

                      @render plus said:

                      Can you think of any way to lock a method stoat other rubies cannot override it. (This is mch worse than adding a new method to an existing class.) then SU could just lock aloof its methods.

                      <span class="syntaxdefault"><br />module FooBar</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">hello</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> puts </span><span class="syntaxstring">'Hello world'</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;<br /></span><span class="syntaxcomment">#&nbsp;=>&nbsp;nil<br /><br /></span><span class="syntaxdefault">FooBar</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">freeze<br /></span><span class="syntaxcomment">#&nbsp;=>&nbsp;FooBar<br /><br /></span><span class="syntaxdefault">module FooBar</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">bye</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> puts </span><span class="syntaxstring">'Goodbye world'</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;<br /></span><span class="syntaxcomment">#&nbsp;=>&nbsp;Error; #<TypeError; (eval);512; can't modify frozen object><br />#&nbsp;=>&nbsp;(eval);512<br /><br /></span><span class="syntaxdefault">module FooBar</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">hello</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> puts </span><span class="syntaxstring">'Hello evil world'</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;<br /></span><span class="syntaxcomment">#&nbsp;=>&nbsp;Error; #<TypeError; (eval);512; can't modify frozen object><br />#&nbsp;=>&nbsp;(eval);512<br />&nbsp;</span><span class="syntaxdefault"></span>
                      

                      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

                        hm... I'm tempted of adding a script that loads first that freezes the base ruby and SketchUp classes and modules just to see what happens. Could be useful to catch misbehaving plugins.

                        ...hmm.... not sure what happens if you sub-class a frozen class....

                        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

                          @thomthom said:

                          ...hmm.... not sure what happens if you sub-class a frozen class....

                          <span class="syntaxdefault"><br />class BaseFoo</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> def poke</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> puts </span><span class="syntaxstring">'arrrh!'</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;<br /></span><span class="syntaxcomment">#&nbsp;=>&nbsp;nil<br /><br /></span><span class="syntaxdefault">BaseFoo</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">freeze<br /></span><span class="syntaxcomment">#&nbsp;=>&nbsp;BaseFoo<br /><br /></span><span class="syntaxdefault">class BaseFoo</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> def prod</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> puts </span><span class="syntaxstring">'eeek!'</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;<br /></span><span class="syntaxcomment">#&nbsp;=>&nbsp;Error; #<TypeError; (eval);512; can't modify frozen class><br />#&nbsp;=>&nbsp;(eval);512<br /><br /></span><span class="syntaxdefault">class ChildFoo </span><span class="syntaxkeyword"><</span><span class="syntaxdefault"> BaseFoo</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> def prod</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> puts </span><span class="syntaxstring">'eeek!'</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;<br /></span><span class="syntaxcomment">#&nbsp;=>&nbsp;nil<br /><br /></span><span class="syntaxdefault">x </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> ChildFoo</span><span class="syntaxkeyword">.new<br /></span><span class="syntaxcomment">#&nbsp;=>&nbsp;#<ChildFoo;0x105ddb98><br /><br /></span><span class="syntaxdefault">x</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">prod<br /></span><span class="syntaxcomment">#&nbsp;=>&nbsp;eeek!<br />#&nbsp;=>&nbsp;nil<br /><br /></span><span class="syntaxdefault">x</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">poke<br /></span><span class="syntaxcomment">#&nbsp;=>&nbsp;arrrh!<br />#&nbsp;=>&nbsp;nil<br /><br /></span><span class="syntaxdefault">class ChildFoo</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> def prod</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> puts </span><span class="syntaxstring">'double-eeek!'</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;<br /></span><span class="syntaxcomment">#&nbsp;=>&nbsp;nil<br /><br /></span><span class="syntaxdefault">class ChildFoo</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> def poke</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> puts </span><span class="syntaxstring">'cheeese!'</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;<br /></span><span class="syntaxcomment">#&nbsp;=>&nbsp;nil<br /><br /></span><span class="syntaxdefault">class BaseFoo</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> def poke</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> puts </span><span class="syntaxstring">'moooo'</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;<br /></span><span class="syntaxcomment">#&nbsp;=>&nbsp;Error; #<TypeError; (eval);512; can't modify frozen class><br />#&nbsp;=>&nbsp;(eval);512<br />&nbsp;</span><span class="syntaxdefault"></span>
                          

                          😄 All good!

                          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

                            http://i.nt.ro/freezing-classes-and-the-nearest-thing-to-unfreezing/
                            http://www.oreillynet.com/ruby/blog/2007/04/ruby_code_that_will_swallow_yo.html

                            TIG

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

                              Here's a snippet I tried to lock down the API for debugging:
                              http://sketchucation.com/forums/viewtopic.php?f=180&t=48890#p439296 (new thread)

                              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

                                @dan rathbun said:

                                Problem is that Ruby is designed to allow this by people who know what they are doing.

                                Yes - locking down the API out of the box can probably do more harm than anything.

                                But it does allow us to lock down for debugging. - See previous post.

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

                                1 Reply Last reply Reply Quote 0
                                • Dan RathbunD Offline
                                  Dan Rathbun
                                  last edited by

                                  @thomthom said:

                                  hm... I'm tempted of adding a script that loads first that freezes the base ruby and SketchUp classes and modules just to see what happens. Could be useful to catch misbehaving plugins.

                                  Problem is that Ruby is designed to allow this by people who know what they are doing.

                                  Many of the Extended ruby Library files add needed methods to base classes, or modify some methods (RubyGems is an example that modifies the global require method, although I never understood why it really needed to do it.)

                                  Even though methods are objects, it is difficult to get a reference to an instance method within the class definition (very easy within an instance of the class.) So it is hard to freeze particular instance methods class-wide, without freezing the whole class or module. (Perhaps the gurus on the Ruby Forum know a trick?)

                                  An alternative would possibly be (untested):

                                  class Sketchup;;Group
                                  
                                    # Make a copy of method ;copy
                                    alias_method(;_copy_,;copy)
                                    
                                    def self.method_added(sym)
                                      #
                                      # callback called by Ruby when a new method is defined;
                                      #
                                      alias_method(;copy,;_copy_) if sym == ;copy
                                      #
                                    end
                                    
                                  end
                                  

                                  EDIT: method_added is a class callback, so must be defined using def self.method_added(sym)

                                  I'm not here much anymore.

                                  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