[Q] Setting DC attributes randomly from ruby script

[Q] Setting DC attributes randomly from ruby script

Postby Pixero » Fri Mar 01, 2019 2:09 pm

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.

Code: Select all
 
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))
0
User avatar
Pixero 
 

Re: [Q] Setting DC attributes randomly from ruby script

Postby Pixero » Sat Mar 02, 2019 5:56 pm

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?

Code: Select all
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)
0
User avatar
Pixero 
 

Re: [Q] Setting DC attributes randomly from ruby script

Postby Pixero » Sat Mar 02, 2019 7:47 pm

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:

Code: Select all
cdef.entities.first.set_attribute("dynamic_attributes", "A", a_rand)
0
User avatar
Pixero 
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Dynamic Components

Who is online

Users browsing this forum: No registered users and 4 guests

Visit our sponsors: