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,

image

You can use replacePaths:


Ruud.gh (16.9 KB)

ahh thank you!

You can also use “Path Mapper”.

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.

3 Likes

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

2 Likes

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)

1 Like

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