sketchucation logo sketchucation
    • Login
    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!
    🫛 Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    [Q] Setting DC attributes randomly from ruby script

    Scheduled Pinned Locked Moved Dynamic Components
    sketchup
    3 Posts 1 Posters 6.1k Views 1 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.
    • PixeroP Offline
      Pixero
      last edited by

      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
      • PixeroP Offline
        Pixero
        last edited by

        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
        • PixeroP Offline
          Pixero
          last edited by

          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
          • First post
            Last post
          Buy SketchPlus
          Buy SUbD
          Buy WrapR
          Buy eBook
          Buy Modelur
          Buy Vertex Tools
          Buy SketchCuisine
          Buy FormFonts

          Advertisement