Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 47

Path Mapper: Advanced Data Wrangling

Recommended resources

Hello! In our last session, you learned how to select and filter data from trees using components like List Item, Tree Branch, and Split Tree. These tools are excellent for extracting information, but they don't alter the fundamental structure of the data tree itself.

Today, we delve into one of the most powerful—and initially, one of the most intimidating—components in Grasshopper: the Path Mapper. While our previous tools allowed you to select data based on its address, the Path Mapper lets you rewrite those addresses entirely. This is the key to advanced data wrangling, enabling you to completely reorganize a data tree to suit any purpose. This lesson will demystify its syntax and show you how to perform sophisticated structural transformations on your data.

1. The Core Concept: Lexical Mapping

The Path Mapper operates on a principle called lexical mapping. This is a formal way of saying it uses a text-based rule to transform an input path structure into a new output path structure. Given your background in ICT, you can think of this as applying a find-and-replace operation or a regular expression, but specifically for Grasshopper's data paths.

When you double-click the Path Mapper component, you open its editor.

The Path Mapper component and its editor. The left side (Source) defines the structure of the incoming data, while the right side (Target) defines the desired output structure.

The editor has two main parts:

  • Source: A textual representation of the incoming data tree's paths. For example, a tree with two levels of branches might be represented as {A;B}. Here, A and B are placeholders for the path indices at each level.
  • Target: A rule that defines the new path for the data. You write this rule using the placeholders from the source path.

The most fundamental concept is the syntax for describing a data element's full address: {A;B;...}(i).

  • The curly braces {...} contain the path.
  • The letters A, B, C, etc., represent the integer indices at each "level" of the path.
  • The parentheses (...) contain the item's index, represented by i.

The Path Mapper also provides a few built-in variables, or constants, that you can use in your target mapping. These are essential for creating dynamic and parametric rules.

Constant Description
path_index The index of the current branch in the flattened list of all branches.
path_count The total number of branches in the tree.
item_count The number of items in the current branch.

Let's begin by reading the official documentation to get a formal introduction to the component and its syntax.

Chapter 3: Advanced Data Structures - Rhino developer

This section from the Rhino developer guide introduces the Path Mapper, its editor, and the core syntax. It's the best starting point for understanding the formal logic.

Please read the section titled 3.6.3 Path mapper. Focus on understanding the relationship between the Source and Target paths and the definitions of the three constants (item_count, path_count, path_index). Also, look at the images showing the built-in mappings like Flatten, Graft, and Reverse; we will explore these next.


2. Replicating Basic Operations

The best way to understand the Path Mapper's power is to see how it can easily replicate the functions of several other components you already know, like Flatten and Graft. This shows its versatility and provides a gentle introduction to writing mapping rules.

The following video from BIM Corner provides a clear, step-by-step walkthrough of these fundamental operations.

Grasshopper Path Mapper is a BADASS

This video offers a practical demonstration of using the Path Mapper for common data reorganization tasks.

First, watch the introduction to understand the component's role. Then, follow these key examples: Flattening: See how the rule {A;B}(i) -> {0} collapses an entire tree into a single branch. Grafting: Observe how the rule {A}(i) -> {A;i} moves each item into its own new sub-branch. Trimming: This is a very common and powerful operation. Pay close attention to how the rule {A;B}(i) -> {A} merges branches by removing the last level of the path hierarchy.

The "Trim" operation is particularly useful. Imagine you have a grid of points organized into branches representing rows, where each row is further subdivided. If you only care about the rows, you can use trimming to merge all the subdivisions.

A visual example of the "Trim" operation. A complex tree with 12 branches, defined by the source path `{A;B}`, is mapped to a simpler tree with 4 branches by using the target path `{A}`. All data is preserved but restructured.

3. Advanced and Parametric Mapping

Simple replication is useful, but the true strength of the Path Mapper lies in creating custom, complex, and parametric data transformations. This is where your mathematical background becomes a significant advantage. You can use mathematical expressions directly within the target path definition.

A common technique involves using the floor (rounding down) and modulo (remainder of a division) operations to reorganize a flat list of items into a multi-dimensional structure. For example, if you have 100 items and you want to organize them into 10 branches of 10 items, you can use rules based on floor(i / 10) and i % 10.

This next video provides an excellent deep dive into this technique, creating a 3D grid of points and then using the Path Mapper to draw lines through the grid in different directions by restructuring the point data.

Grasshopper - Intro to the Path Mapper

This video by Daniel Christev is a fantastic exploration of the mathematical and parametric capabilities of the Path Mapper.

Watch from this segment. The presenter first creates a 3D grid of points (10x10x10 = 1000 points in one list). He then masterfully uses modulo and floor operations within the Path Mapper to regroup these points, allowing him to create polylines along the X, Y, and Z axes of the grid. This is a perfect example of algorithmic thinking applied to geometry. Next, watch the segment comparing the Path Mapper to the Flip Matrix component, from this point. Note why Flip Matrix fails on a 3D data structure and how the Path Mapper handles it effortlessly by simply swapping the placeholders (e.g., {A;B;C} -> {A;C;B}). Finally, pay attention to the brief but crucial point about making the definition parametric from this timestamp. The presenter shows that hard-coding numbers (like 10) in the mapping rule is fragile. Using constants like item_count makes the definition robust and adaptable to changes in the input.

A Word of Caution

As powerful as the Path Mapper is, it has one significant weakness: its mapping rules are specific to the structure of the source tree. If you define a mapping {A;B} -> {B;A} and you later change the input data so it only has one path level ({A}), the Path Mapper will fail because the source rule no longer matches. You must always be mindful of the exact structure of the data you are feeding into it.


Conclusion

You have now explored the most advanced data manipulation tool in the standard Grasshopper library. By mastering the Path Mapper, you transition from simply using data to truly commanding it. The ability to fundamentally restructure data trees on the fly is what separates basic scripting from the creation of highly complex and intelligent parametric systems.

Key Takeaways:

  • The Path Mapper reorganizes data trees by applying a text-based (lexical) rule that maps source paths to target paths.
  • It can replicate the functionality of simpler components like Flatten, Graft, and Flip Matrix but provides far greater flexibility.
  • The mapping syntax {A;B}(i) uses placeholders for path and item indices, which can be rearranged in the target rule.
  • You can use mathematical functions (floor, modulo) and constants (path_index, item_count) in your target rules to create powerful, parametric transformations.
  • The Path Mapper is powerful but "brittle"—the source mapping must exactly match the incoming data structure.

In our next lesson, we will step back from individual components and look at the bigger picture. With the ability to create complex data flows, keeping your definition organized becomes paramount. We will cover techniques for managing complexity using groups, relays, and other tools to ensure your scripts remain clear, readable, and maintainable.