Friday, May 29, 2026

Fine-Tuning Selection Set Filtering with Callbacks in PyRx

As of version 2.2.58, most selection set actions in PyRx have received a powerful overload that allows developers to attach a

When executing a selection command, the callback function automatically intercepts a Db.ObjectIdArray containing the candidate objects. Your logic then evaluates these objects and returns a list of integers representing the indexes of the IDs that should be discarded from the final selection.
Key Benefits of Selection Callbacks
  • Advanced Criteria: Filter by complex properties (like geometric limits or dynamic states) that standard DXF group codes cannot check.
  • Streamlined Workflow: Eliminate the need to manually iterate over and clean up selection sets after user input.
  • Real-time Precision: Ensure your commands only process exactly what they are intended to handle.
The Complete Python Code
Here is how to implement a dynamic block filter using the new callback overload.
 
import traceback
from pyrx import Ap, Db, Ed, Ge, Rx

def dyn_filter(ids: Db.ObjectIdArray) -> list[int]:
    """
    Callback filter that evaluates a list of selected Object IDs.
    Returns a list of list indexes pointing to objects that should be removed.
    """
    itemsToRemove = []
   
    # Enumerate through the selection array to track both index and object ID
    for idx, id in enumerate(ids):
        # Open the ID as a BlockReference to inspect its properties
        ref = Db.BlockReference(id)
       
        # Check if the block reference is NOT a dynamic block
        if not ref.isDynamicBlock():
            # Flag the index for removal if it fails the condition
            itemsToRemove.append(idx)
           
    return itemsToRemove

@Ap.Command()
def pyselectfcb():
    """
    Custom PyRx command to prompt user selection using the custom filter.
    """
    try:
        # Prompt selection filtering for 'INSERT' (Block References)
        # Pass the 'dyn_filter' function as the second argument callback
        ps, ss = Ed.Editor.select([(0, "INSERT")], dyn_filter)
       
        # If the selection prompt was successful, print the result status and size
        if ps == Ed.PromptStatus.eOk:
            print(f"Selection Status: {ps}, Filtered Count: {ss.size()}")
           
    except Exception:
        # Catch and print any runtime exceptions for debugging
        print(traceback.format_exc())


 
Code Walkthrough
  1. Ed.Editor.select Overload: We pass dyn_filter as a functional parameter directly into our selection method.
  2. Indexing Strategy: The callback tracks idx. If a block fails the isDynamicBlock() check, its exact list index is recorded.
  3. Array Cleanup: PyRx reads the returned itemsToRemove list and instantly strips those matching positions out of your final selection set ss.
 
 
Only the Dynamic Blocks are selected, yay!
 

Fine-Tuning Selection Set Filtering with Callbacks in PyRx

As of version 2.2.58, most selection set actions in PyRx have received a powerful overload that allows developers to attach a When executing...