Metadata and Renaming
These functions are attributes of the rpts module. They read and write the metadata of scene nodes, and they rename nodes. In external Python, call them as rpts.get_metadata() after import rpts. In the RPTS console, rpts is already available.
Data model
A node's metadata is a list of attributes (also called groups). Each attribute has a title and holds entries. Each entry has a title (its key), a value and a storage type, which is one of "uint", "float" or "string". Entries inside an attribute are listed with the unsigned integers first, then the floats, then the strings.
Flat properties, as used by CAD and PLM data, are stored as an attribute whose title is the property name and that holds exactly one entry with an empty key (""). For example, a flat property ACTUAL_WEIGHT is read back as {'ACTUAL_WEIGHT': {'': '4.5'}}. See the flat properties functions.
Most functions here accept nodes as a path string (str), an rpd.Node object, or an iterable of either. Titles and keys are compared exactly and case-sensitively unless a function says otherwise.
Reading metadata
get_metadata(node)
Returns the metadata of one node. If an attribute title or an entry title occurs more than once, the later one wins. For an ordered form that keeps duplicates, use get_metadata_definitions().
node(str or Node): a single path orrpd.Node.
Returns: a dict in the form {attribute_title: {entry_title: value}}. The dict is empty if the node has no metadata or cannot be resolved.
get_metadata_definitions(nodes, as_paths=True)
Returns one metadata record per node, in input order. Nodes that cannot be resolved are skipped. The output is the form that set_metadata_definitions() accepts.
nodes(str, Node, or iterable of either): the nodes to read.as_paths(bool):Truereturns plain, JSON-friendly records.Falsealso adds a"node"key with therpd.Nodeof each record. Do not keep those objects across asset changes. Default:True.
Returns: a list of records:
[
{
"path": "group/part_003",
"name": "part_003",
"attributes": [
{
"title": "Custom",
"entries": [{"key": "Weight", "value": 4.5, "type": "float"}],
},
],
},
]
get_metadata_value(node, key, attribute=None, default=None)
Returns the value of the first entry titled key.
node(str or Node): a single path orrpd.Node.key(str): the entry title. Pass""to read the untitled entry of a flat property, for exampleget_metadata_value(n, "", attribute="ACTUAL_WEIGHT").attribute(str): restricts the search to attributes with this title. Default:None, which searches every attribute.default: the value to return if no entry is found. Default:None.
Returns: the entry value, or default.
get_metadata_type(node, key, attribute=None, default=None)
Returns the storage type of the first entry titled key.
node(str or Node): a single path orrpd.Node.key(str): the entry title.attribute(str): restricts the search to attributes with this title. Default:None.default: the value to return if no entry is found. Default:None.
Returns: "uint", "float" or "string", or default.
get_metadata_values(nodes, key, attribute=None, default=None)
Reads one entry from many nodes. This is the batch form of get_metadata_value().
nodes(str, Node, or iterable of either): the nodes to read.key(str): the entry title.attribute(str): restricts the search to attributes with this title. Default:None.default: the value for nodes that have no such entry. Default:None. Pass a sentinel to tell a missing entry from a real value.
Returns: a dict {node_path: value} with one item per resolved node, in input order. Duplicate and unresolved nodes are skipped.
has_metadata(node, key=None, attribute=None)
Returns whether a node carries metadata.
node(str or Node): a single path orrpd.Node.key(str):Nonemeans any entry. A string requires an entry with that title.""requires an untitled entry, which is a flat property. Default:None.attribute(str): restricts the check to attributes with this title. Used alone, it tests whether such an attribute exists. Default:None.
Returns: True if the node has metadata that satisfies the given criteria, otherwise False. With no criteria, it is True if the node has any attribute.
list_metadata_attributes(nodes=None)
Lists the attribute titles that occur on the given nodes.
nodes(str, Node, or iterable of either, orNone): the nodes to inspect.Nonemeans every node in the scene. Descendants of the given nodes are not included. Default:None.
Returns: a sorted list of unique attribute titles. The list is empty if no model is loaded or nothing has metadata.
list_metadata_keys(nodes=None, attribute=None)
Lists the entry keys that occur on the given nodes. Unlike rpts.rules.metadata_keys(), this function can be limited to some nodes and is not capped.
nodes(str, Node, or iterable of either, orNone): the nodes to inspect.Nonemeans every node in the scene. Descendants of the given nodes are not included. Default:None.attribute(str): only attributes with this exact title. Default:None, which includes all attributes.
Returns: a dict {attribute_title: sorted list of entry keys}. The dict is empty if no model is loaded or nothing matches.
Finding nodes by metadata
find_nodes_by_metadata(key=None, value=None, attribute=None, match_type="exact", case_sensitive=False, roots=None, as_paths=False)
Finds nodes whose metadata matches. A node matches if one attribute and entry pair satisfies every criterion you give. attribute is matched against attribute titles, key against entry titles, and value against the text form of the entry value. All three use the same match_type. With no criteria, every node that carries any metadata matches.
key(str): the entry title to match.Nonemeans any key.""matches only entries with an empty key, which are flat properties. This is always an exact match, whatevermatch_typeis. Default:None.value(str): the value to match. Default:None.attribute(str): the attribute title to match. Default:None.match_type(str orrpd.FilterMatchType): one of"exact","contains","starts_with","ends_with","regex"or"wildcard". The wildcard form supports*and?and must match the whole value. Default:"exact".case_sensitive(bool): whether matching considers letter case. Default:False.roots(str, Node, or iterable of either): limits the search to these subtrees. Default:None, which searches the whole scene.as_paths(bool):Truereturns path strings instead ofNodeobjects. Default:False.
Returns: a list of Node objects, or a list of str paths if as_paths=True. Each node appears once. The function never raises. It returns an empty list if no model is loaded, or if match_type or a regular expression is invalid.
# Flat property: every node whose BASE_MODEL property equals "EX500"
nodes = rpts.find_nodes_by_metadata(attribute="BASE_MODEL", value="EX500")
select_nodes_by_metadata(key=None, value=None, attribute=None, match_type="exact", case_sensitive=False, roots=None, add=False)
Selects the nodes that find_nodes_by_metadata() finds. The parameters key, value, attribute, match_type, case_sensitive and roots have the same meaning as in that function.
add(bool):Trueadds the matches to the current selection.Falsereplaces the selection. Default:False.
Returns: an int:
-1if no model is loaded.0if nothing matched or changed. This also happens for an invalidmatch_typeor regular expression.- A number greater than
0: the number of nodes that were selected or changed.
Writing metadata
Every write function makes exactly one undoable history step and refreshes the metadata panel. Writing through the SDK directly, for example with node.metadata.add(...), also works, but it is not undoable and does not refresh the panel.
Every function on this page that writes returns an int and never raises:
-1if no node could be resolved, or if metadata editing is not available in this build.0if nothing changed or the arguments were invalid. The reason is printed to the log.- A number greater than
0: the number of nodes that changed.
Every write function also takes an optional label (str), the text that appears for the step in the history. Default: None, which generates a label.
METADATA_DEFAULT_ATTRIBUTE
The attribute (group) title that write functions use when you do not give one. Its value is "Custom".
set_metadata(nodes, key, value, attribute="Custom", type=None, label=None)
Creates or updates the entry key with value in the first attribute titled attribute on every node. The attribute is created if it is missing.
nodes(str, Node, or iterable of either): the nodes to change.key(str): the entry title. It may be"", which is the untitled entry.Noneis invalid. To set a flat property, useset_metadata(nodes, "", value, attribute="ACTUAL_WEIGHT").value(int, float or str): the value to store.attribute(str): the attribute title. Default:"Custom".type(str):Noneinfers the type: an int from 0 to 4294967295 becomes"uint", any other number becomes"float", and anything else becomes"string". An explicit"uint","float"or"string"converts the value. A value that cannot be converted returns0. Default:None.
Returns: an int, as described above.
set_metadata_block(nodes, keys, values, attribute="Custom", types=None, label=None)
Like set_metadata(), for several entries in one undo step. Entry keys[i] is set to values[i].
nodes(str, Node, or iterable of either): the nodes to change.keys(sequence of str): the entry titles.values(sequence of int, float or str): the values, in the same order askeys.attribute(str): the attribute title. Default:"Custom".types(str, or sequence of str orNone): one type for every entry, or one type per entry. Default:None, which infers each type asset_metadata()does.
Returns: an int, as described above.
remove_metadata(nodes, key, attribute=None, label=None)
Removes the entry key from attributes titled attribute, or from every attribute if attribute is None. The attribute stays, possibly empty. To drop a flat property completely, use remove_metadata_attribute().
nodes(str, Node, or iterable of either): the nodes to change.key(str): the entry title.""is the untitled entry.Noneis invalid.attribute(str): restricts the removal to attributes with this title. Default:None.
Returns: an int, as described above.
remove_metadata_attribute(nodes, attribute, label=None)
Removes the whole attribute (group) titled attribute.
nodes(str, Node, or iterable of either): the nodes to change.attribute(str): the attribute title.
Returns: an int, as described above.
clear_metadata(nodes, label=None)
Removes all metadata from every given node.
nodes(str, Node, or iterable of either): the nodes to change.
Returns: an int, as described above.
rename_metadata(nodes, old_key, new_key, attribute=None, label=None)
Renames the entry old_key to new_key, keeping its value and type. The call is refused, returns 0, and changes nothing on any node if, in an attribute where the rename would apply, an entry titled new_key already exists.
nodes(str, Node, or iterable of either): the nodes to change.old_key(str): the current entry title.""is the untitled entry.Noneis invalid.new_key(str): the new entry title.""is the untitled entry.Noneis invalid.attribute(str): restricts the rename to attributes with this title. Default:None, which applies to every attribute.
Returns: an int, as described above.
rename_metadata_attribute(nodes, old_attribute, new_attribute, label=None)
Renames an attribute (group), keeping its entries. The call is refused, returns 0, and changes nothing if a node that has old_attribute already has an attribute titled new_attribute.
nodes(str, Node, or iterable of either): the nodes to change.old_attribute(str): the current attribute title.new_attribute(str): the new attribute title.
Returns: an int, as described above.
copy_metadata(source, targets, attributes=None, mode="merge", label=None)
Copies the metadata of one source node onto the target nodes.
source(str or Node): a single node. Only the first is used.targets(str, Node, or iterable of either): the nodes to change. The source is skipped if it is among them.attributes(str or sequence of str): copy only the attributes with these titles. Default:None, which copies all attributes.mode(str):"merge"sets the copied entries on each target and keeps its other metadata."replace"makes each target's metadata exactly the copied attributes, so a source without metadata clears the targets. Anattributesfilter that matches nothing is refused and returns0. Default:"merge".
Returns: an int, as described above.
set_metadata_definitions(definitions, label=None, mode="replace")
Restores metadata in bulk from the output of get_metadata_definitions(). Entries with an empty key are accepted.
definitions(list of dict, or a single dict): the records to apply.mode(str):"replace"replaces the metadata of each record's node with the record's attributes."merge"sets only the record's entries and keeps other metadata. Default:"replace".
Returns: an int, as described above.
apply_metadata_edits(batch_json)
The low-level function behind the write functions above. Prefer those functions.
batch_json(str): a JSON object{"label": str (optional), "ops": [...]}. Each op is one ofset,remove,remove_attribute,rename,rename_attribute,clearorreplace.
Returns: a JSON string {"changed": int, "errors": [str]}. The function never raises. An invalid batch gives changed equal to 0 and a non-empty errors list.
Flat properties
Flat properties are the CAD and PLM form of metadata: an attribute titled with the property name that holds one untitled entry. These functions are shortcuts for the general functions above.
set_property(nodes, name, value, type=None, label=None)
Sets the flat property name. This is the same as set_metadata(nodes, "", value, attribute=name).
nodes(str, Node, or iterable of either): the nodes to change.name(str): the property name.value(int, float or str): the value to store.type(str):None,"uint","float"or"string", as inset_metadata(). Default:None.
Returns: an int: -1, 0 or the number of changed nodes, as for the write functions.
get_property(node, name, default=None)
Returns the value of the flat property name. It reads the untitled entry of the first attribute titled name. If that attribute has exactly one entry, titled or not, the function returns its value. Otherwise it returns default.
node(str or Node): a single path orrpd.Node.name(str): the property name.default: the value to return if the property is not found. Default:None.
Returns: the property value, or default.
remove_property(nodes, name, label=None)
Removes the flat property name. This is the same as remove_metadata_attribute().
nodes(str, Node, or iterable of either): the nodes to change.name(str): the property name.
Returns: an int, as for the write functions.
Renaming nodes
Each rename call is one undoable step. Names are stored exactly as given. The SDK keeps a node's path stable on rename: the path keeps the name the node had at import time, while the scene tree shows the new name. For that reason, read names from node.name, never from the last path segment.
Each function returns an int:
-1if no node could be resolved, or if renaming is not available.0if nothing was renamed.- A number greater than
0: the number of nodes that were renamed.
An entry is skipped, with a message, if its name is blank, contains /, or equals the raw name of a sibling node. The other entries still apply. Renaming a node to its current name does nothing. The scene root cannot be renamed.
The duplicates parameter controls siblings that would end up with the same name:
"skip": only the first sibling is renamed and the others are refused. This is the default for these functions."allow": siblings may share the name. This follows the CAD convention. The SDK keeps paths unique."number": the first sibling keeps the name and later ones get_2,_3and so on. Names already used by other siblings are avoided.
allow_duplicates=True is an alias for duplicates="allow".
rename_node(node, new_name, label=None, allow_duplicates=False, duplicates=None)
Renames one node.
node(str or Node): a single path orrpd.Node.new_name(str): the new name.label(str): the history text. Default:None.allow_duplicates(bool): alias forduplicates="allow". Default:False.duplicates(str):"allow","number"or"skip". Default:None, which means"skip".
Returns: -1, 0 or 1.
rename_nodes_map(mapping, label=None, allow_duplicates=False, duplicates=None)
Renames many nodes in one undo step. A parent and its child can be renamed together. Give the paths as they were before the call.
mapping(dict or sequence of tuples): either{path: new_name}or[(node_or_path, new_name), ...].label(str): the history text. Default:None.allow_duplicates(bool): alias forduplicates="allow". Default:False.duplicates(str):"allow","number"or"skip". Default:None, which means"skip".
Returns: an int, as described above.
rename_nodes_template(nodes, template, label=None, duplicates=None, allow_duplicates=False)
Renames nodes from a template. The template can use these tokens:
{name}: the node's current name.{parent}: the name of the node's parent.{index}or{index:03}: the 1-based position of the node innodes, optionally zero-padded.{meta:Attribute/Key}: the value of the entryKeyin the attributeAttribute.{meta:Attribute}: the value of the flat propertyAttribute.{{and}}: literal braces.
A node with a metadata token that has no value is skipped. A / in a substituted value becomes -. A / typed in the template itself is an error.
nodes(str, Node, or iterable of either): the nodes to rename.template(str): the name template.label(str): the history text. Default:None.duplicates(str):"allow","number"or"skip". Default:None, which means"skip".allow_duplicates(bool): alias forduplicates="allow". Default:False.
Returns: an int, as described above.
rpts.rename_nodes_template(rpts.get_selection(), "{parent}_{index:03}", duplicates="number")
rename_nodes_replace(nodes, find, replace="", regex=False, case_sensitive=True, label=None, duplicates=None, allow_duplicates=False)
Replaces find with replace in the name of each node. Nodes whose name does not change are skipped. A result that contains / is refused.
nodes(str, Node, or iterable of either): the nodes to rename.find(str): the text to search for. It is a regular expression ifregex=True.replace(str): the replacement text. Withregex=True, back-references are supported. Default:"".regex(bool): treatfindas a regular expression. Default:False.case_sensitive(bool): whether matching considers letter case. Default:True.label(str): the history text. Default:None.duplicates(str):"allow","number"or"skip". Default:None, which means"skip".allow_duplicates(bool): alias forduplicates="allow". Default:False.
Returns: an int, as described above.
last_rename_paths()
Returns the paths read back from the scene after the most recent rename call. The SDK keeps paths stable on rename, so this usually maps a path to itself, but read it after a call to be sure. Descendants are not listed, because their paths change by the same prefix.
Returns: a dict {old_path: new_path} for every renamed node and for any node whose own path segment changed. The dict is empty if nothing was renamed.
rename_nodes(batch_json)
The low-level function behind the rename functions above. Prefer rename_node() and rename_nodes_map().
batch_json(str): a JSON object{"label": str (optional), "allow_duplicates": bool (optional), "renames": [{"path": str, "name": str}, ...]}.
Returns: a JSON string {"changed": int, "errors": [str], "paths": {old: new}}. The function never raises.