accessAttachments

snippets/accessAttachments.py
 1# © 2024 Autodesk, Inc. All rights reserved.
 2
 3# example for handling attachments
 4
 5# create a name attachment
 6nameAttachment = createAttachment("Name")
 7# set the name value of the attachment
 8vrFieldAccess(nameAttachment).setString('name', "MyAttachmentNodeName")
 9# get the selected node and add the attachment.If a name attachment is already present, it will be replaced
10node = getSelectedNode()
11node.addAttachment(nameAttachment)
12# check if the attachment exists and print the name
13if node.hasAttachment('Name'):
14    attachment = node.getAttachment('Name')
15    print(vrFieldAccess(attachment).getString('name'))
16
17# access core attachments
18if node.fields().hasField('material'):
19    mat = vrFieldAccess(node.fields().getFieldContainer("material"))
20    if mat.hasAttachment('Name'):
21        matNameAttachment = mat.getAttachment('Name')
22        print(vrFieldAccess(matNameAttachment).getString('name'))
23
24
25# delete a touch sensor attachment
26if node.hasAttachment("TouchSensorAttachment"):
27    att = node.getAttachment("TouchSensorAttachment")
28    node.subAttachment(att)