Rules and Node Search
A rule set is a list of rules. Each rule has filters that select nodes and actions that run on the nodes it selects. The same rule sets can be edited in the Rules panel of the application and saved as .rprules.json files. This page documents the two ways to use them from Python:
rpts.find_nodes()evaluates filters and returns the matching nodes.- The
rpts.rulesnamespace loads, builds, validates, previews, runs and exports rule sets.
In external Python, call these as rpts.find_nodes() and rpts.rules.run() after import rpts. In the RPTS console, rpts is already available.
Data structures
Rule sets are plain dicts, so they can be saved as JSON.
Filter
A filter dict has these keys. Missing keys take the default shown.
enabled(bool):True.target(str): what to examine. One of"metadata","node_name","node_path","material_name"or"mesh_name".attribute(str orNone): for themetadatatarget, the attribute title.Nonemeans any.key(str orNone): for themetadatatarget, the entry title.Nonemeans any.value(str orNone): the value to match. For targets other thanmetadata, this is the pattern.Nonemeans any, for themetadatatarget.match(str): the comparison. Default:"contains". Text matches are"exact","contains","starts_with","ends_with","regex"and"wildcard". Numeric matches are"eq","ne","lt","le","gt","ge"and"range". A range is written"a-b"or"a..b".case_sensitive(bool):False.invert(bool):False.include_descendants(bool):False.
For the metadata target, text matches apply to the attribute title, the entry key and the text form of the value. Numeric matches compare the entry value as a number, and key and attribute then match exactly. Numeric matches are not valid for the other targets.
Action
An action dict has the keys enabled (bool, default True), type (str) and params (dict). The action types and their params are:
"select": selects the matched nodes, replacing the selection."add_to_selection": adds the matched nodes to the selection."hide"and"show": hides or shows the matched nodes."isolate": keeps the matched nodes visible with their whole subtree, and hides every node that is neither under a match nor an ancestor of a match. If a matched node is hidden, it is shown first, which makes a second undo step."assign_material": params{"material": str}."process": params{"action": str, "config": dict or None}. It runs a processing action on the matched nodes only, as one undo step. A non-emptyconfigwins. Otherwise the action's settings come from the application panel if it is open, or from the action's default settings. If nothing matched, the action is skipped. The asset is replaced, so later actions re-evaluate on the new asset. Tessellation happens at import in RPTS, so there is no per-subset tessellation unless a processing action provides it."log": writes the matched nodes to the log."set_metadata": paramsattribute(default"Custom"),key,value(text) andtype("auto","uint","float"or"string", default"auto"). With"auto", the text is parsed:"12"becomesuint,"1.5"becomesfloat, anything else becomesstring."remove_metadata": paramsattribute(empty means any) andkey(empty means remove the whole attribute). At least one is required."rename": paramsmode("template", the default, or"replace") andduplicates("allow", the default,"number"or"skip"). The template mode takes a non-emptytemplate, with the tokens described forrename_nodes_template()on the Metadata and Renaming page. The replace mode takes a non-emptyfind,replace,regex(defaultFalse) andcase_sensitive(defaultTrue). Nodes that cannot be renamed are skipped and counted inskipped_nodes.
Rule and RuleSet
A rule dict has the keys id (str), name (str), description (str), enabled (bool), filters (list of Filter) and actions (list of Action). Rules have no scope: their filters always evaluate over the whole scene.
A rule set dict has the keys schema (str, "rpts.rules/1"), name (str) and rules (list of Rule).
Reports
run() returns one report per considered rule. A rule report has id, name, matched (int), actions (a list of action reports) and, on failure, error. A disabled rule is reported as {"id", "name", "skipped": True}. An action report has type and changed (int), and may have error. A skipped action is {"type", "skipped": True}. A rename action report also has skipped_nodes, the number of nodes that were not renamed.
Finding nodes
find_nodes(filters, logic="all", scope=None, as_paths=False)
Finds nodes with rule-style filters. Use this when you want the matches in a script without building a whole rule set.
filters(list of dict): Filter dicts. They are combined with AND.logic(str): only"all"is supported. Any other value logs a warning and returns an empty list. Default:"all".scope(str, Node, or iterable of either): limits the search to these nodes and their descendants. 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, in scene order. The function never raises. It returns an empty list if no model is loaded or a filter is invalid, and the reason is logged.
filters = [
rpts.rules.new_filter(target="metadata", attribute="BASE_MODEL", value="EX500", match="exact"),
rpts.rules.new_filter(target="node_name", value="bolt", match="contains"),
]
paths = rpts.find_nodes(filters, as_paths=True)
The rpts.rules namespace
Access these as rpts.rules.<name>().
load(path)
Loads a rule set from a .rprules.json file.
path(str): the file to read.
Returns: the rule set dict, or None if loading failed. The reason is logged.
save(rule_set, path=None)
Saves a rule set as indented UTF-8 JSON. The schema key is set to "rpts.rules/1".
rule_set(dict): the rule set to save.path(str): the destination file. Default:None, which usesdefault_path().
Returns: the path that was written, or None on failure. The reason is logged.
default_path()
Returns the default rule file path, <model_dir>/<model_stem>.rprules.json, next to the loaded model.
Returns: a str, or None if the model file path is not known.
new_rule_set(name="Rules")
Creates an empty rule set.
name(str): the rule set name. Default:"Rules".
Returns: a RuleSet dict.
new_rule(name="New rule")
Creates an enabled rule with no filters and no actions, and a fresh unique id.
name(str): the rule name. Default:"New rule".
Returns: a Rule dict.
new_filter(**kwargs)
Creates a complete Filter dict with the default values, overridden by the keyword arguments.
Returns: a Filter dict.
new_action(type, **params)
Creates an Action dict.
type(str): the action type.**params: the action's params, for examplenew_action("assign_material", material="Steel").
Returns: an Action dict.
validate(rule_set)
Checks a rule set for problems.
rule_set(dict): the rule set to check.
Returns: a list of problem strings. The list is empty if there are none. Messages that start with "Warning:" do not block a run.
preview(rule_or_set)
Evaluates the filters only. Nothing in the scene is changed.
rule_or_set(dict): a Rule or a RuleSet.
Returns: a dict {rule_id: [matched paths]}. Each list is capped at 1000 paths.
match(rule)
Returns the nodes that a rule's filters select over the whole scene.
rule(dict): a Rule.
Returns: a list of path strings in scene order, without a cap. The list is empty on error, and the error is logged.
run(rule_set_or_path, rule_ids=None, stop_on_error=False, dry_run=False, progress=None)
Runs the enabled rules of a rule set. For each rule, the filters are evaluated, then re-evaluated before every action, because an earlier action may have changed the scene. Each action is its own undo step. The function never raises.
rule_set_or_path(dict or str): a RuleSet, a single Rule, or the path of a.rprules.jsonfile.rule_ids(list of str): run only the rules with these ids. Default:None, which runs all enabled rules.stop_on_error(bool): stop the whole run at the first rule or action that fails. Default:False.dry_run(bool): evaluate the filters only and run no action. Default:False.progress(callable): called asprogress(index, total, rule_name)before each enabled rule, whereindexstarts at 0, and asprogress(total, total, "")at the end. If it returnsFalse, the run stops before the next rule and is logged as cancelled. Exceptions that the callback raises are ignored. Default:None.
Returns: a list of rule reports, as described under Reports. The list is empty if the rule set is invalid.
rule_set = rpts.rules.load("C:/models/engine.rprules.json")
reports = rpts.rules.run(rule_set, dry_run=True)
for report in reports:
print(report["name"], report.get("matched"))
run_rule(rule, rule_ids=None, stop_on_error=False, dry_run=False, progress=None)
Runs a single Rule dict. The parameters and the return value are the same as for run().
to_python(rule_set_or_path, rule_ids=None)
Generates a standalone, editable Python script that reproduces run() using only the public rpts API. The helper code is inlined. Disabled rules, actions and filters are written as commented-out code. This is an export only: a generated script cannot be imported back into a rule set.
rule_set_or_path(dict or str): a RuleSet, a single Rule, or the path of a.rprules.jsonfile.rule_ids(list of str): export only the rules with these ids. Default:None.
Returns: the script as a str, or an empty string on error. The error is logged.
processing_actions()
Lists the processing actions that a process action can use.
Returns: a list of dicts, each with name, description, config (the default config) and, for some actions, category.
metadata_keys()
Lists the metadata keys in the whole scene, for example to offer completions. The result is capped at 2000 keys. For a scoped, uncapped list, use list_metadata_keys().
Returns: a dict {attribute_title: sorted list of entry keys}. The dict is empty if no model is loaded.