How to interact with default attributes in Dynamic Components with Ruby

Hi, guys!

I created a component with its definition and instance in Ruby. Is it possible to add option lists with Ruby to the component just like creating Dynamic Components to control something like size?

I tried this code:

      # Convert the group to a component and set its name and attributes
      inst = group.to_component
      inst.definition.name = "material"
      inst.name = thickness.to_s
      
      # Set the thickness attribute to the component's LenY
      inst.set_attribute("dynamic_attributes", "LenY", thickness)

but it gave me only “LenY” that is a Custom Attribute
dasda

I know “LenY” here is a string. So is there a way to interact with the native or default attributes in DC or create a custom one connected with the native attributes (This would be better because we can customize the name appearing in the DC options.

Thanks in advance!

Search results for ‘Dynamic Components LenX #developers:ruby-api’ - SketchUp Community

e.g.:
How to access the attributes for the dynamic component? - Developers / Ruby API - SketchUp Community

1 Like

Yes but … the DC extension is a closed source extension that has no public documentation. It is also very complex and has interacting "dynamic_attributes" dictionaries on both the definition and instance (for components) and just the instance for nested dynamic groups.

Much of this has already been discussed in the Dynamic Components subcategory.

2 Likes

Hi Dan, thanks for the hint! I have explored a little bit about “dynamic_attributes” dictionaries. I created a new code and there was a progress actually.

 # Extrude the face to create a wall with the specified thickness (LenY)
      len_y = 100.mm 
      f.pushpull(len_y)

      # Convert the group to a component and set its name and attributes
      inst = group.to_component
      inst.definition.name = "material"
      inst.name = len_y.to_s
      
   # Set the thickness attribute to the component's LenY
      inst.set_attribute("dynamic_attributes", "Thickness", len_y)

      # Set the display name for the "T" attribute
      dict = inst.attribute_dictionary("dynamic_attributes", true)
      
      
      
      # Set the thickness attribute formula based on LenY
      dict["_thickness_formula"] = 'LenY'

      # Set the thickness label and units
      dict["_thickness_label"] = 'Thickness'
      dict["_thickness_units"] = 'STRING'

      # Set the thickness access type
      dict["_thickness_access"] = 'TEXTBOX'

In this case the custom attribute “Thickness” is related to the default value “LenY” (left in image) and also shows in DC options dialog although it is empty (right in image) . Is there a way to make default value “LenY” and len_y(100mm) interact? Like when I change the thickness in DC option input box, the LenY (thickness) of the model also changes.

Thank you very much for any tips!

Hi Dezmo, Thanks for the reference! I found it much helpful there for my case!

1 Like

I don’t think so, as your len_y is simply a local variable in your code, not the DC extension’s code.

These kinds of meta attributes (I think) belong in the definition’s "dynamic_attributes" dictionary, not the instance’s.

Have you installed Aerilius’ Attribute Inspector extension, and examined the sample dynamic components that come with SketchUp ?

The dynamic component information displayed in Attribute Inspector I am afraid is a copy of the internal values that the dynamic components work with.
If the attributes in the dictionaries are modified, the geometries are not updated.
I think there are many extensions that do that job and that are very developed (e.g.) Profile Builder, (I actually draw practically everything with that extension) so I think it is easier to use that tool than to create it from scratch.
This way works but it convert to string
ScreenFlow

This way doesn’t work (also if you change to float):
ScreenFlow2

Hi, Dan. Yes I have tried Attribute Inspector and tried to examined one DC I manually created in Sketchup. It looks possible, with Ruby, to access and manipulate DC’s both custom and default attributes that are attributes starting with underscores, for example _leny that represents LenX (maybe a default attribute for size control?) under Size category when setting attributes. But it looks very complex and time-consuming to create a DC with input or options with Ruby.

Hi Rafa. According to author of DC, attributes that start with underscores, might give access to manipulate DC default and custom attributes in Ruby. Here is link: setting dynamic attributes to expressions? • sketchUcation • 1.

So my vision is to create a DC with Ruby scripts but I have not tested this method and as you said there might be a risk that If the attributes in the dictionaries are modified, the geometries are not updated.

Except I really suggest (again) that you examine one of the DCs that come with SketchUp.

Change options with the DC interface and watch how the dynamic attributes change in both the instance and definition dictionaries. Make sure you test with both a single dynamic instance and multiple instances in the model. (There is differing behavior when there are multiple instances.)

These are the “meta” attributes that the DC extension uses to display the user interface and control the instances of dynamic components.

It’s been quite awhile since I mucked around with DCs, but I think you may need to go the other 'way around. Ie, set the _leny_formula = to the thickness custom attribute.

Then, don’t forget to do a redraw on the instance. See the last line of code in Scott’s example at the SCF post you linked above.

1 Like