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

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;

2 Likes