Path Mapper with Variable Input

Hi,

Is there a possibility to modify branch names of a tree with a viriable input X (so its dynamic)?

Lets say I would like to modify the branch names, by adding a value of X (dependent of some other variable).

so lets say X=2

{0} + X → {2}
{1} + X → {3}
{2} + X → {4}

thank you in advance,

You can use replacePaths:


Ruud.gh (16.9 KB)

ahh thank you!

PathMapper doesn’t accept dynamic variable input. however here I’ve made a little script that can change PathMapper source and target dynamically:

  private void RunScript(string source, string target)
  {
    var pathMappers = FindPathMapperObjectInCurrentGroup();
    if(pathMappers.Count != 1)
    {
      Component.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "This component should be added in a group with exactly one Path Mapper component.");
      return;
    }
    pathMappers[0].Lexers.Clear();
    pathMappers[0].Lexers.Add(new GH_LexerCombo(source, target));
    pathMappers[0].ExpireSolution(true);
  }

  // <Custom additional code> 
  private List<GH_PathMapper> FindPathMapperObjectInCurrentGroup()
  {
    var groups = GrasshopperDocument.Objects.OfType<GH_Group>().Where(gr => gr.ObjectIDs.Contains(Component.InstanceGuid)).ToList();
    var output = groups.Aggregate(new List<GH_PathMapper>(), (list, item) =>
      {
      list.AddRange(
        GrasshopperDocument.Objects.OfType<GH_PathMapper>()
        .Where(obj => item.ObjectIDs.Contains(obj.InstanceGuid))
        );
      return list;
      }).Distinct().ToList();
    return output;
  }

PathMapper.gh (18.0 KB)
You need to add this script component in a group with the PathMapper component you want to edit.
Part of this code is inspired by scriptparasite plugin for grasshopper. by @arendvw.

Thank you for your answer, but I would like to ask how to achieve the same function with ghpython?

How about this:

python_PathMapper.gh (9.0 KB)

It doesn’t control the Path Mapper component, but it performs the same function with python code.

-Kevin

I actually want to know how to write this part in ghpython.

private List<GH_PathMapper> FindPathMapperObjectInCurrentGroup()
{
var groups = GrasshopperDocument.Objects.OfType<GH_Group>().Where(gr => gr.ObjectIDs.Contains(Component.InstanceGuid)).ToList();
var output = groups.Aggregate(new List<GH_PathMapper>(), (list, item) =>
{
list.AddRange(
GrasshopperDocument.Objects.OfType<GH_PathMapper>()
.Where(obj => item.ObjectIDs.Contains(obj.InstanceGuid))
);
return list;
}).Distinct().ToList();
return output;

2021-12-16_9-28-50

import Grasshopper as gh
def findPathMapperObjectInCurrentGroup():
    output = []
    objs = ghenv.Component.OnPingDocument().Objects
    for group in objs:
        if isinstance(group, gh.Kernel.Special.GH_Group) and group.ObjectIDs.Contains(ghenv.Component.InstanceGuid):
            for pathMapper in objs:
                if isinstance(pathMapper, gh.Kernel.Special.GH_PathMapper) and group.ObjectIDs.Contains(pathMapper.InstanceGuid):
                    output.append(pathMapper)
    return output
pathMappers = findPathMapperObjectInCurrentGroup();
if len(pathMappers) != 1:
    ghenv.Component.AddRuntimeMessage(gh.Kernel.GH_RuntimeMessageLevel.Warning, 'This component should be added in a group with exactly one Path Mapper component.')
else:
    pathMappers[0].Lexers.Clear()
    pathMappers[0].Lexers.Add(gh.Kernel.Data.GH_LexerCombo(source, target))
    pathMappers[0].ExpireSolution(True)

PathMapper.gh (21.0 KB)

Thank you very much This is what I was looking for!

I replied in another topic regarding the static nature of Path Mapper with a component that acts somewhat like a dynamic Path Mapper but with slightly fewer bells and whistles. It uses only Grasshopper native components and no scripts, and makes use of the * wildcard to dynamically handle changing path depths. This can be relevant when using ShapeDiver, as scripts are checked before being allowed.

The * wildcard is basically useful whenever the exact depth of the tree is not known/can change, but it is known where the branch depths of interest are relative to the start or end of the depth (can’t really get around that, as the paths have to stay unambiguous).

P.S. It’s not too slow, but the main bottleneck lies in the “Replace Paths” component that it uses. If someone knows a faster way to place items from one branch onto another, I’m keen to find out!

dynamic path swapper example.gh (33.7 KB)

Dynamic Path Swapper.ghuser (21.4 KB)

Sure. You can match by path index. Does Shape Diver support Path Compare? Most of your time is lost in the lexigraphical sorting and merge attempts of Replace Paths

I believe Path Compare doesn’t help in placing items on novel branch paths? When swapping from {a;b} to {b;a}, the original tree may not have already contained {b;a}, so Path Mapper and the Dynamic Path Swapper I sent construct novel paths. I don’t see how Path Compare would prevent the need for Replace Paths, unless I’m missing something?