• Login
sketchucation logo sketchucation
  • Login
🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Variable problem

Scheduled Pinned Locked Moved Developers' Forum
11 Posts 3 Posters 261 Views
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.
  • C Offline
    chrisglasier
    last edited by 26 Jan 2011, 06:29

    Please would someone be kind enough to show me how to sort out this snippet?

    
    def kf(time)
    keyframes = {}
    model = Sketchup.active_model
    d = model.attribute_dictionaries['kframes']
    d.each_pair do | key,value |
    a =  1
    value.each do | v |
    if v.nil?
    v = [[1,0,0],0]
    end
    v << time * a
    a += 1
    end
    keyframes[key] = value
    end
    return keyframes
    end
    
    

    When I puts the v's all are correct, but value returns the previously nil v' s to nil. Just cannot see why!

    Thanks

    With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

    1 Reply Last reply Reply Quote 0
    • T Offline
      thomthom
      last edited by 26 Jan 2011, 07:26

      It doesn't work as you expect because you are not changing the content of the array. You're dealing with a local copy.
      I changed your loop to use .map - that should work. (untested)

      <span class="syntaxdefault"><br />def kf</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">time</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">  keyframes </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{}<br /></span><span class="syntaxdefault">  model </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br />  d </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">attribute_dictionaries</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'kframes'</span><span class="syntaxkeyword">]<br /></span><span class="syntaxdefault">  d</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each_pair do </span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> key</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">value </span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault">    a </span><span class="syntaxkeyword">=</span><span class="syntaxdefault">  1<br />    value</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">map do </span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> v </span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault">      if v</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">nil</span><span class="syntaxkeyword">?<br /></span><span class="syntaxdefault">        v </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">,</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">]<br /></span><span class="syntaxdefault">      end<br />      v </span><span class="syntaxkeyword"><<</span><span class="syntaxdefault"> time </span><span class="syntaxkeyword">*</span><span class="syntaxdefault"> a<br />      a </span><span class="syntaxkeyword">+=</span><span class="syntaxdefault"> 1<br />      v<br />    end<br />    keyframes</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">key</span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> value<br />  end<br />  return keyframes<br />end<br /></span>
      

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

      1 Reply Last reply Reply Quote 0
      • C Offline
        chrisglasier
        last edited by 26 Jan 2011, 07:43

        @thomthom said:

        It doesn't work as you expect because you are not changing the content of the array. ....

        Thanks but I am sorry to say it doesn't work at all.

        Also my original code successfully adds the time to each ( v) array, it only seems the nil block is being ignored by value.

        So I don't know ...

        With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

        1 Reply Last reply Reply Quote 0
        • C Offline
          chrisglasier
          last edited by 26 Jan 2011, 07:58

          .... but map does work if there aren't any nil elements in the value arrays.

          With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

          1 Reply Last reply Reply Quote 0
          • T Offline
            thomthom
            last edited by 26 Jan 2011, 08:24

            @chrisglasier said:

            Thanks but I am sorry to say it doesn't work at all.

            sorry, should be .map!

            @chrisglasier said:

            Also my original code successfully adds the time to each (v) array, it only seems the nil block is being ignored by value.

            Yes, because nil is immutable.

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

            1 Reply Last reply Reply Quote 0
            • T Offline
              TIG Moderator
              last edited by 26 Jan 2011, 09:25

              value.each do | v |
              if v.nil?
                v = [[1,0,0],0]
              end
              v << time * a
              a += 1
              end
              keyframes[key] = value
              end
              

              looks through the contents of 'value' and assigns some values to elements in array 'v', then it sets the keyframes[key] back to be value ???
              Don't you want to modify 'key'?
              Why don't you use keyframes[key] = v ???

              TIG

              1 Reply Last reply Reply Quote 0
              • C Offline
                chrisglasier
                last edited by 26 Jan 2011, 09:54

                @tig said:

                value.each do | v |
                > if v.nil?
                >   v = [[1,0,0],0]
                > end
                > v << time * a
                > a += 1
                > end
                > keyframes[key] = value
                > end
                

                looks through the contents of 'value' and assigns some values to elements in array 'v', then it sets the keyframes[key] back to be value ???

                Don't you want to modify 'key'?
                Why don't you use keyframes[key] = v ???

                Value is an array of arrays (like [[1,0,0],0]). So v is one of those. I am sure it is caused by the nil block? (immutable??). Perhaps I should make a new array from the v's because they are OK.

                I will try that later or tomorrow.

                Thanks for your help.

                With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

                1 Reply Last reply Reply Quote 0
                • T Offline
                  TIG Moderator
                  last edited by 26 Jan 2011, 10:02

                  In which case

                    ...
                    new_value = []
                    value.each do | v |
                      if v ### ??? ### isn't this what you want ?***
                        v << time * a
                      else ### OR it's nil
                        v = [[1,0,0],0]
                      end ### ***if NOT then revert to your format...
                      new_value << v
                      a += 1
                    end
                      keyframes[key] = new_value
                    end
                  
                  

                  TIG

                  1 Reply Last reply Reply Quote 0
                  • C Offline
                    chrisglasier
                    last edited by 26 Jan 2011, 10:22

                    @tig said:

                    In which case

                      ...
                    >   new_value = []
                    >   value.each do | v |
                    >     if v ### ??? ### isn't this what you want ?***
                    >       v << time * a
                    >     else ### OR it's nil
                    >       v = [[1,0,0],0]
                    >     end ### ***if NOT then revert to your format...
                    >     new_value << v
                    >     a += 1
                    >   end
                    >     keyframes[key] = new_value
                    >   end
                    > 
                    

                    Yes that looks good;I guess I can use v.is_a?(Array) for the if bit as the nil candidates are just empty elements.

                    Thanks to you both ...

                    With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

                    1 Reply Last reply Reply Quote 0
                    • T Offline
                      TIG Moderator
                      last edited by 26 Jan 2011, 10:28

                      if v returns true if v is not nil or not []
                      The else then takes it's 'converse' i.e. it is nil OR []
                      I'm not clear about how a good or bad entry for 'value >>> v' might actually 'look' in your code...

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • C Offline
                        chrisglasier
                        last edited by 26 Jan 2011, 11:34

                        @tig said:

                        I'm not clear about how a good or bad entry for 'value >>> v' might actually 'look' in your code...

                        <span class="syntaxdefault">new_value </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[]<br /></span><span class="syntaxdefault">            value</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each do</span><span class="syntaxkeyword">|</span><span class="syntaxdefault"> v </span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault">                if </span><span class="syntaxkeyword">!</span><span class="syntaxdefault">v</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">is_a</span><span class="syntaxkeyword">?(Array)<br /></span><span class="syntaxdefault">                    v </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">[[</span><span class="syntaxdefault">1</span><span class="syntaxkeyword">,</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">]<br /></span><span class="syntaxdefault">                end<br />                v </span><span class="syntaxkeyword"><<</span><span class="syntaxdefault"> time </span><span class="syntaxkeyword">*</span><span class="syntaxdefault"> a<br />                a </span><span class="syntaxkeyword">+=</span><span class="syntaxdefault"> 1<br />                new_value </span><span class="syntaxkeyword"><<</span><span class="syntaxdefault"> v<br />            end<br />            keyframes</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">key</span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> new_value</span>
                        

                        I did it like this in the end as both types of v have time added. Anyway it works which is good! Thanks again.

                        The empty elements currently are needed to delay certain rotations - like a kick at the end of a run. I have a mechanism to modularise the animation. I want to leave that until later.

                        With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

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

                        Advertisement