Attribute Builder (Python, experimental)

An extension to the Python API is available for editing attribute components from a dictionary-style specificiation. The API is implemented as a single Python class smtk.attribute_builder.AttributeBuilder. The initial use for this feature is for replaying Python operation traces. To support this, the following method is provided:

smtk.attribute_builder.AttributeBuilder.build_attribute(self, att: smtk.attribute.Attribute, spec: dict:[str, list], resource_dict:[str, smtk.resource.Resource]=None) None

This method modifies the contents of the input attribute based on the input specification. In addition to the attribute and specification arguments, the builder_attribute() method includes an optional resource_dict argument for specifying SMTK resource instances that can be referenced in the specification dictionary.

Specification

The input specification is a Python dictionary object with two fields for specifying a list of associations and a list of items. Here is an example of the specification format:

  {
    'associations': [{'resource': 'model'}],
    'items': [
        {'path': 'string-item', 'value': 'xyzzy'},
        {'path': '/double-item', 'value': [3.14159, None, 2.71828]},
        {'path': 'int-item', 'enabled': True, 'value': 2},
        {'path': 'int-item/conditional-double', 'value': 42.42},
        {'path': 'comp-item', 'value': [
            {'resource': 'model', 'component': 'casting'},
            {'resource': 'model', 'component': 'symmetry'},
        ]},
        {'path': '/group-item/subgroup-double', 'value': 73.73},
    ]
}

Note

The current implementation will raises a Python exception if it encounters a syntax error in the specification or if some editing step fails.

Associations List

Attribute associations are specified by a list of dictionary items. Each item includes a resource key with its value set to one of the keys in the resource dictionary (passed in to the build_attribute() method). This is sufficient to specify a resource association. To specify a component belonging to a resource, a component field is added to specify the name of the component.

Items List

Item modifications are specified by a list of dictionary items. The fields for each dictionary item can include path (required), enabled, and value.

  • The path field specifies the path from the attribute to the item being specified. It uses the same syntax as the smtk.attribute.Attribute.itemAtPath() method, with forward slash as the separator and the :py:attr:activeOnly argument set to :py:const:false. (Starting the path with a forward slash is optional.)

  • The value field is for setting the item value. This can be a single value or a list of values. The type (string, int, float) and number of values must be consistent with the item and its corresponding item-definition, of course. For ReferenceItem, ComponentItem, and ResourceItem, the value is specified using the same syntax used in the associations list. If the value, or any of the values in a list, are None the corresponding item value will be unset.

  • The enabled field is for setting the item’s enabled state.

  • Extensible group items may have several sub-groups. The count field for a group item specifies the number of sub-groups. Items in a sub-group use integer index N in their path to specify which sub group they belong to. For example:


{
‘items’: [

{‘path’: ‘/group-item’, ‘count’: 2}, {‘path’: ‘/group-item/0/subgroup-double’, ‘value’: 73.73}, {‘path’: ‘/group-item/1/subgroup-double’, ‘value’: 83.83},

]

}

  • If an item belonging to a group needs to have a name that is an integer, it should always be preceded by the sub-group index.