Metadata example¶
VRED 2023.3 introduced support for managing metadata. However, previous versions already extracted metadata during CAD import for supported file types. It was added to the nodes as OpenSG ValuePair attachments.
Also, API v1 allowed adding custom ValuePair attachments to nodes in the Scenegraph with vrNodePtr.setAttribute.
This example demonstrates how to search for ValuePair attachments and how to convert them into metadata using the vrMetadataService
.
metadata/metadata-import.py¶
1# © 2024 Autodesk, Inc. All rights reserved.
2
3# When importing CAD files in VRED version less than 2023.3 some meta data has been attached as a ValuePair.
4# This function shows how to convert these ValuePair attachments in metadata sets.
5def convertValuePairToObjectSet(node):
6 # check if the node has a ValuePair attachement
7 if node.hasAttachment('ValuePair'):
8 attachment = node.getAttachment('ValuePair')
9 keys = vrFieldAccess(attachment).getMString('key')
10 values = vrFieldAccess(attachment).getMString('value')
11 keyCount = len(keys)
12 # a ValuePair attachement has two fields with string arrays
13 # One is for keys one is for values
14 # key[index] and value[index] gives one key/value pair in metadata
15 if keyCount > 0 and len(values) == keyCount:
16 # this gives access to all metadata sets attached to this node
17 metadata = vrMetadataService.getMetadata(vrNodeService.getNodeFromId(node.getID()))
18 # we do not create a new metadata set, we use nodes object set
19 # object set is one metadata set for all key/value pairs attached directly to this node
20 objectSet = metadata.getObjectSet()
21 print("Add metadata entries from " + node.getName())
22 # add all attached key value pairs to metadata object set
23 for i in range(0, keyCount):
24 objectSet.setValue(keys[i],values[i])
25 # descend to all child nodes and look for a value pair attachement.
26 for childIndex in range(0, node.getNChildren()):
27 convertValuePairToObjectSet(node.getChild(childIndex))
28
29# convert all nodes from scene. Start at the top node and descend to the lower nodes.
30convertValuePairToObjectSet(getRootNode())