Get and set values
# The following is an example on how to get and set values of attributes.
import ufe
import lookdevx as lx
if not shadingContainer:
raise Exception('This example requires a shading container i.e., a MaterialX document or a USD '
'material. Check the other examples to see how to create one.')
# Create some nodes.
runTimeId = shadingContainer.runTimeId()
addFloat = ufe.NodeDef.definition(runTimeId, 'ND_add_float').createNode(shadingContainer, ufe.PathComponent('add'))
addColor3 = ufe.NodeDef.definition(runTimeId, 'ND_add_color3').createNode(shadingContainer, ufe.PathComponent('add'))
addVector2 = ufe.NodeDef.definition(runTimeId, 'ND_add_vector2').createNode(shadingContainer, ufe.PathComponent('add'))
# Values can be queried and set using the `ufe.Attribute` interface.
floatAttribute = ufe.Attributes.attributes(addFloat).attribute('inputs:in1')
color3Attribute = ufe.Attributes.attributes(addColor3).attribute('inputs:in1')
vector2Attribute = ufe.Attributes.attributes(addVector2).attribute('inputs:in1')
# The values can be read using the `get()` method.
print('Original values:')
print(f'Float attribute value: {floatAttribute.get()}')
print(f'Color3 attribute value: ({color3Attribute.get().r()},{color3Attribute.get().g()},{color3Attribute.get().b()})')
print(f'Vector2 attribute value: ({vector2Attribute.get().x()},{vector2Attribute.get().y()})')
# The values can be set using the `set()` method.
floatAttribute.set(0.5)
color3Attribute.set(ufe.Color3f(0.1, 0.2, 0.3))
vector2Attribute.set(ufe.Vector2f(0.4, 0.6))
print('Updated values:')
print(f'Float attribute value: {floatAttribute.get()}')
print(f'Color3 attribute value: ({color3Attribute.get().r()},{color3Attribute.get().g()},{color3Attribute.get().b()})')
print(f'Vector2 attribute value: ({vector2Attribute.get().x()},{vector2Attribute.get().y()})')