• Login
sketchucation logo sketchucation
  • Login
ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

[Q] Setting DC attributes randomly from ruby script

Scheduled Pinned Locked Moved Dynamic Components
sketchup
3 Posts 1 Posters 6.1k 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.
  • P Offline
    Pixero
    last edited by 1 Mar 2019, 13:09

    I'm trying to randomly within a range set attributes but don't know how to get it to work.
    The attributes A and B should randomly set a value from a dropdown list of strings like a1, a2... b1, b2... in the DC.
    The attibute CWidth should be set randomly between a min and max value. Integers will do fine.

      
    cinst.set_attribute("dynamic_attributes", "A", "a1") #rand(1..11)
    
    cinst.set_attribute("dynamic_attributes", "B", "b1") #rand(1..5)
    
    cinst.set_attribute("dynamic_attributes", "CWidth", rand(300..500))
    
    
    1 Reply Last reply Reply Quote 0
    • P Offline
      Pixero
      last edited by 2 Mar 2019, 16:56

      I've tried with this but it doesn't seem to work either. I'm not getting any errors in the console though.
      Right after this I redraw the DC but it still has the default attributes set.
      Any ideas what to do?

      
      a_array = ["a1", "a2", "a3", "a4", "a5"]
      b_array = ["b1", "b2", "b3", "b4", "b5"]
      a_rand = a_array.sample
      b_rand = b_array.sample
      
      cinst.set_attribute("dynamic_attributes", "A", a_rand)
      cinst.set_attribute("dynamic_attributes", "B", b_rand)
      cinst.set_attribute("dynamic_attributes", "C", rand(300..500))
      		
      #Redraw to update the DC
      dcs = $dc_observers.get_latest_class
      dcs.redraw_with_undo(cinst)
      
      
      1 Reply Last reply Reply Quote 0
      • P Offline
        Pixero
        last edited by 2 Mar 2019, 18:47

        For anyone interested I managed to solve it by myself.
        I had to set the attributes on the definition and transverse down one step like this:

        
        cdef.entities.first.set_attribute("dynamic_attributes", "A", a_rand)
        
        
        1 Reply Last reply Reply Quote 0
        • 1 / 1
        1 / 1
        • First post
          3/3
          Last post
        Buy SketchPlus
        Buy SUbD
        Buy WrapR
        Buy eBook
        Buy Modelur
        Buy Vertex Tools
        Buy SketchCuisine
        Buy FormFonts

        Advertisement