Creating branches from Grasshopper

Hi

I’m just wondering if it’s possible to create a new branch from either Grasshopper or via python.

1 Like

Hi @dwastberg! Welcome to the forum (feel free to introduce yourself if you want to).

Short answer: yes, it’s possible from python! Check the examples out: Examples | Speckle Docs


from specklepy.api.client import SpeckleClient
from specklepy.api.credentials import get_default_account, get_local_accounts

all_accounts = get_local_accounts() # get back a list
account = get_default_account()

client = SpeckleClient(host="your-server.com")

# create a branch
branch_id = client.branch.create("stream_id", "branch name", "a description of the branch")

I’ve literally just copy pasted this code from the docs, so it’s not working, ping our resident py-master, @izzylys.

Regarding grasshopper: we’re planning to deprecate most of the stream management components in favour of the web app, where it’s simpler and easier to implement this functionality.

3 Likes

Thanks I’ll give the python API a try.

The reason I want to create branches automatically from grasshopper is to automatically create branches derived from layer names and then push different geometry to different branches. Although perhaps this isn’t really the intended use for branches.

2 Likes

Just to complete Dim’s answer. If you’re looking to run this inside a Python script component in Grasshopper, I’m not sure it will work… but you can find a way to get the Python script + Speckle working here :point_down:t3:
https://speckle.guide/user/grasshopper.html#using-the-c-python-script-nodes

3 Likes

@dwastberg depending on what you’re trying to achieve. You could also try, instead of using branches, to create Custom Speckle Objects.
With a CSO you can have as many property as you want, and they can also be dynamically generated to match you layers, see:

https://speckle.guide/user/grasshopper.html#creating-custom-objects

1 Like

Hi Alan,

Is there a way now (almost 2 months later :slight_smile: ) to do this in Grasshopper? Create branches?

Best,

Rick

I guess the only way right now would be to use a C# script to deal with that.

https://speckle.guide/user/grasshopper.html#using-the-c-python-script-nodes

I’ll try to whip up a super raw version of that if I have time this week.

1 Like

Thanks! If you could send an example would be awesome!

Hey @RickTitulaer this code should do it.
Before creating a new branch, I check if one with the same name already exists.

Also please note that when copy-pasting that code in the C# component, GH is automatically deleting my await and I have to manually add them! Not sure if a GH bug or what :slight_smile:

In general, I would not recommend generating branches programmatically but would suggest instead leveraging our flexible custom speckle objects. That’s why we will not add it as a feature to GH/DYN.

using System.Threading;
using System.Threading.Tasks;
using System.Linq;

    var t = Task<string>.Run(async () =>
      {
      var sw = x as Speckle.Core.Credentials.StreamWrapper;
      var acc = await sw.GetAccount();
      var client = new Speckle.Core.Api.Client(acc);

      var branches = await client.StreamGetBranches(sw.StreamId);

      if (!branches.Any(b => b.name == y))
      {
        var b = await client.BranchCreate(new Speckle.Core.Api.BranchCreateInput
          {
            streamId = sw.StreamId,
            name = y.ToString(),
            description = "hellooooo"
            });

        return b;

      }

      return "Branch already existing";

      });

    A = t.Result;

3 Likes

May I ask you to attach this node from the Grasshopper here?

Sure, but be aware that this has been minimally tested!

create-new-branch.gh (5.3 KB)

Also note that:

If this is a must have feature for you, we’d love to hear more about your specific use case!

1 Like

Where find this file ? :slight_smile:
Снимок экрана 2021-12-14 094319

Hey @Monfed!

There are 2 assemblies that need to be added to your C# node for them to work. These are usually located in the user folder, so the path varies slightly.

You have instructions on where to find them here → Grasshopper | Speckle Docs

But it should be as simple as swapping the Matteo in that path for the name of your computer’s user :wink:

C:\Users\YOUR_USER_NAME\AppData\Roaming\Grasshopper\Libraries\SpeckleGrasshopper2\SpeckleCore2.dll

1 Like

Thanks ! It’s worked

1 Like