Skip to main content

Automating Edits with Rules

Introduction​

CAD and PLM files carry a lot of information about each part besides its geometry: product names, part numbers, materials, weights, revisions and so on. In RapidPipeline Twin Studio, this information is stored as metadata on the nodes of the scene, and it is shown in the METADATA panel.

The RULES panel lets you turn that information into repeatable edits. A rule says:

For every node WHERE these conditions are true, DO these actions.

For example: "For every node whose metadata property PART_NUMBER starts with 4711, assign the material Steel_Brushed and rename the node to {meta:PART_NUMBER}." Rules can be saved to a file, reloaded later, and run again on the same or on another model, which makes them a good fit for recurring clean-up and preparation steps.

info

The RULES panel is available in the Model tab only.

Opening the Rules Panel​

Use View Menu > Rules to show or hide the RULES panel. It is hidden by default.

The RULES panel is shown in the same column as the MODEL ACTIONS panel, and the two panels share the same space. While RULES is open, MODEL ACTIONS is hidden and its entry in the View menu is greyed out. Close RULES to get MODEL ACTIONS back.

The RULES panel with a rule set open: toolbar, rule list, and the selected rule's filters (WHERE) and actions (DO)

The panel is made of these areas, from top to bottom:

  • The title line with the name of the current rule set. A dot after the name ("Rules •") means that there are unsaved changes. Hover the title to see the file the rule set was loaded from or saved to ("Not saved yet" otherwise).
  • The toolbar: New, Open…, Save, Save As…, Export as Python…, Run All ▶ and Preview.
  • The rule list, with the + Add Rule button below it.
  • The rule editor, with the WHERE (filters) and DO (actions) sections of the selected rule.
  • A footer that shows the state of the preview, for example "3 nodes match".

Creating a Rule​

There are two ways to create a rule:

  1. Click + Add Rule in the RULES panel. A new, empty rule called "New Rule" is added to the list and selected.
  2. In the METADATA panel, right-click a property and choose Create Rule from This Property… or Create Rule from This Value…. The RULES panel opens and a new rule is created that already contains a metadata filter for that property (and, for "from This Value", for that exact value), together with a Select action. The second entry is greyed out if the property has different values on the selected nodes ("mixed").

The METADATA panel's right-click menu on a property, with "Create Rule from This Property…" and "Create Rule from This Value…"

Every rule has:

  • a name (and an optional description, shown as a tooltip in the list),
  • a checkbox in the list to enable or disable the whole rule,
  • a list of filters (the WHERE part),
  • a list of actions (the DO part).

Click a rule in the list to edit it. Changes are applied immediately; they are only written to disk when you save the rule set.

Filters (WHERE)​

Each filter checks one property of a node. All enabled filters must match for a node to be selected by the rule (filters are combined with AND). A rule without any enabled filters matches every node, and the footer then shows "no enabled filters, so every node matches".

Click + Add Filter to add a filter, and ✕ to remove it. The checkbox in front of a filter disables it without deleting it.

Each filter has the following fields:

FieldDescription
TargetWhat to check: Metadata, Node name, Node path, Material name or Mesh name.
Attribute and PropertyOnly for the Metadata target. Attribute is the metadata group title (shown in bold in the METADATA panel), Property is the key of the property inside it. Leave a field empty to mean "any". The drop-down lists are filled from the metadata that exists in the loaded model, but you can also type your own text.
MatchHow the Value is compared (see below).
ValueThe text or number to compare with. For the Metadata target an empty value means "the property exists, whatever its value".

The Match drop-down offers these text comparisons: Exact, Contains, Starts with, Ends with, Regex and Wildcard (* ?). For the Metadata target, the following number comparisons are offered as well: Equals (=), Not equal (!=), Less than (<), Less or equal (<=), Greater than (>), Greater or equal (>=) and Range (a-b).

Three small toggle buttons sit next to the value:

  • Aa - Case sensitive.
  • ≠ - Invert: match the nodes that do not satisfy this filter.
  • ⊞ - Include descendants of matching nodes: also take in everything below a matching node. This is useful because metadata often sits on a part node while the geometry is in its child nodes.

At the right end of each filter row, a counter shows how many nodes remain after this filter (filters are counted one after the other). The counters are filled in while Preview is on, otherwise they show "—".

Actions (DO)​

Click + Add Action to add an action, choose its type in the drop-down, and fill in its settings. Click ✕ to remove it. As for filters, the checkbox in front of an action disables it without deleting it. A rule can contain several actions; they run from top to bottom, and the filters are evaluated again before each action, so a rule that renames nodes and then hides them still finds the right nodes.

ActionWhat it does
Select (replace selection)Replaces the current selection with the matching nodes.
Add to selectionAdds the matching nodes to the current selection.
Hide / ShowHides or shows the matching nodes.
IsolateThe matching nodes stay visible, together with everything below them and the parents that contain them. All other nodes are hidden.
Assign materialAssigns a material to the matching nodes (and to the meshes below them). Pick the material from the Material drop-down, which lists the materials of the current scene, or type its name.
Run processing actionRuns one of the processing actions of the MODEL ACTIONS panel (for example a mesh clean-up or a decimation) on the matching nodes only. See below.
Log matchesWrites the number of matches and the first matching node paths to the LOG panel. Nothing in the scene changes.
Set metadataSets a metadata property on every matching node, creating it if needed. Fields: Attribute, Key, Value and Type (auto, uint, float or string; default auto). If you give a Key but no Attribute, the attribute Custom is used. If you leave the Key empty and give an Attribute, a flat property (a property without a group) is created.
Remove metadataRemoves a property (fill in Key) or a whole attribute (leave Key empty) from the matching nodes. Leave Attribute empty to match any attribute.
RenameRenames the matching nodes. See below.

Running processing actions from a rule​

When you choose Run processing action, pick the action in the Action drop-down. The rule then stores a snapshot of the settings of that action, taken from the MODEL ACTIONS (or OPTIMIZE) panel at that moment. If that panel is not open, the default settings of the action are used. The line Config: N settings shows that a snapshot is stored inside the rule.

  • Edit… opens the stored settings as JSON. The dialog validates your changes and offers Copy and Load current settings.
  • Reset replaces the stored settings with the current settings of the action.

If the stored settings are empty ("read when run"), the current settings of the panel are read each time the rule runs.

The action is applied to the matching nodes that contain meshes (groups are expanded to the meshes below them). If none of the matches contains a mesh, the action is skipped.

tip

Set up the action in the MODEL ACTIONS panel first (for example Decimate Mesh with the face percentage you want), and only then add it to the rule. The snapshot then already contains the settings you expect.

Renaming nodes​

The Rename action has two modes, selected with Mode:

  • Template: you write the new name, and text in curly braces is filled in for each node.
  • Find / replace: replaces text in the existing name. Use the Find and Replace fields, and the Regex and Match case check boxes. With Regex, the Python regular-expression syntax is used and \1-style back-references work in Replace.

In Template mode, the following tokens are available. The button next to the template inserts them for you, including the metadata keys found in the scene:

TokenBecomes
{name}the current name of the node
{parent}the name of the parent node
{index}, {index:03}the position of the node among the matches (1, 2, 3 ... or 001, 002, 003 ...)
{meta:Attr/Key}a metadata value (attribute and property key)
{meta:Attr}the value of a flat property
{{ and }}literal curly braces

For example, {parent}_{name}_{index:03} or {meta:PART_NUMBER}_{meta:REVISION}.

Duplicate names decides what happens when sibling nodes (nodes with the same parent) would get the same new name:

  • Allow (default): they may share the name, as is common in CAD, for example for four placements of the same screw.
  • Number (_2, _3): the first node keeps the name, the following ones become Name_2, Name_3 and so on.
  • Skip: only the first node is renamed, the others are left unchanged.

A node is skipped, with a message in the log, when a metadata value used in the template is missing, when the new name would be empty, or when Skip applies. A / that comes from a substituted value is replaced by -; a / typed into the template itself, or produced by a find / replace, is not allowed.

Reordering, Duplicating and Deleting Rules​

Rules run in the order of the list. To change the order, drag a rule to a new position, or right-click it and choose Move Up or Move Down.

Right-click a rule in the list to open its context menu:

EntryDescription
Run This RuleRuns only this rule. A disabled rule is skipped, also when run this way.
Select MatchesSelects all nodes matched by the rule in the scene.
Copy Rule as PythonCopies the rule as a Python script to the clipboard (see Export as Python).
DuplicateCreates a copy of the rule, named "... copy", right below it.
Rename…Changes the name of the rule.
DeleteDeletes the rule after a confirmation ("Delete rule '...'?").
Move Up / Move DownMoves the rule one position up or down.

Use the check box in front of a rule to disable it temporarily. Disabled rules are skipped by Run All.

The right-click menu of a rule in the rule list

Previewing a Rule​

Switch Preview on to check what a rule matches without changing anything in the scene. While Preview is on:

  • the nodes matched by the selected rule are selected and highlighted in the 3D view,
  • the footer shows how many nodes match (for example "12 nodes match"),
  • every rule in the list shows its match count as a small badge, and every filter shows its running count,
  • errors in the filters (for example an invalid regular expression) are shown in the footer as "Preview error: ...".

The preview only changes the selection, never the model. When Preview is off, the footer reads "Preview off — turn on to select matches and see live counts". For very large matches, the preview selects at most the first 1000 nodes; use Select Matches from the context menu to select all of them.

The RULES panel with Preview on: match counts next to the rule and its filter, and the total at the bottom of the panel

Running Rules​

  • Run All ▶ runs every enabled rule, in list order.
  • Run This Rule (context menu) runs a single rule.

Before running, the rule set is checked. If something is not valid, a dialog "The rule set is not valid" lists the problems and nothing is executed. If there is no enabled rule, you are told "There are no enabled rules to run."

While the rules run, the panel is disabled. When more than one rule runs, a progress dialog Running rules shows "Running rule 2/5: rule name". Closing this dialog stops the run after the rule that is currently executing; the rules that already ran are not reverted (use Undo for that). If a rule fails, the remaining rules still run.

When the run is finished:

  • each rule in the list shows a status dot and a tooltip Last run, with the number of matched nodes and, per action, how many nodes were changed, skipped, or failed,
  • details are written to the LOG panel with the prefix [Rules].

What happens to the scene, Undo and History​

  • Rules change the real scene: selection, visibility, materials, metadata, node names or meshes (for processing actions).
  • Each action is its own undo step, and the steps appear in the HISTORY panel and in Edit Menu > Undo. Metadata and rename actions are labeled with the rule name, for example "Rule 'Hide bolts': ...". Undo and Redo (Ctrl+Z and Ctrl+Y) work as for any other command.
  • The Isolate action can take two undo steps when some of the matching nodes were hidden before.
  • Selection changes made by the rules are not history entries.
caution

Processing actions can merge or rebuild meshes, and metadata of merged nodes may not survive. If you use rules that depend on metadata, run them before heavy processing steps, or put them in an earlier rule in the list.

Saving and Loading Rule Sets​

All rules of the RULES panel form one rule set. The toolbar offers:

ButtonDescription
NewStarts a new rule set, containing one empty rule. If there are unsaved changes, you are asked whether to Save, Discard or Cancel.
Open…Loads a rule set from a file.
SaveSaves to the current file. If the rule set has not been saved yet, it is saved next to the loaded model, as <model name>.rprules.json; if that location is not known, you are asked for a file name.
Save As…Saves to a file of your choice. The file name always ends with .rprules.json, and the name of the rule set is taken from the file name.

Rule sets are stored as readable JSON files with the extension .rprules.json. This makes it easy to keep them with your project, share them with colleagues, or keep them under version control.

Automatic loading: when you import or open a model, Twin Studio looks for a rule set with the same name next to the model file (for example gearbox.rprules.json next to gearbox.step) and loads it automatically. If there are unsaved changes in the panel when you open another model, the changes are kept and nothing is loaded automatically; Save then writes to the file belonging to the new model.

Export as Python​

Export as Python… writes the rule set as a standalone, readable Python script (default name <model name>.rules.py). Disabled items are included as comments. You can run the script in the Script Editor or the Python console, use it as a starting point for your own scripts, or add it to an automation pipeline. The context menu entry Copy Rule as Python does the same for a single rule and copies the script to the clipboard.

The .rprules.json file remains the editable source: Twin Studio cannot import a Python script back into rules.

For the Python functions behind the rules, see the Python API documentation.

Limitations​

  • The RULES panel is only available in the Model tab.
  • Filters always look at the whole scene: a rule cannot be limited to the current selection.
  • Paths of nodes keep the name they had at import time. If a rule renames a node, the new name is shown in the scene tree, but the internal path stays the same.
  • Several placements of the same CAD part often have identical names. A rename rule therefore gives them identical new names too, unless you choose Number (_2, _3) under Duplicate names.
  • The text fields of Set metadata and Remove metadata are plain text fields; the value is converted according to Type (auto detects whole numbers, decimals and text).
  • Starting a rule while a processing action of the MODEL ACTIONS panel is still running is not guarded; wait for the processing action to finish first.