Condition statements in python

hello everyone
i have a square surface which is divided in 4 part in u and v direction(16 cell in total). i want to choose 8 of this 16 cell each time and extrude it for making my form.

i could write this code in ghpython but the problem is condition statement:
1.every cell should at least have one border connection to other cells(just corner connection is not acceptable)
2.totally it should be an integrated form not in 2 or … part

is it possible to help me please? (8.8 KB)

This version has more variety (use slider in blue group): (14.2 KB) (14.6 KB)

96 out of 1000 patterns qualify. The last pattern is repeated to fill out the array. Not ideal but… :thinking: (17.3 KB)

Solved that problem: (21.2 KB)


Not sure if you are searching for a way in code(you tagged it with python), if this is the case you could have a look at topology lists(Mesh or Brep).
Than it’s kind of a maze logic.

There are exactly 1474 possible combinations:

from itertools import combinations
from ghpythonlib.parallel import run
import ghpythonlib.treehelpers as th
from Rhino.Geometry.Brep import JoinBreps
from ghpythonlib.treehelpers import list_to_tree

def qualify(indices):
    if len(JoinBreps([cells[index] for index in indices], 0.1)) == 1:
        return indices
a = list_to_tree(list(filter(None, run(qualify, list(combinations(range(len(cells)), n)), False)))) (16.2 KB)

P.S. I’ve mistakenly inputted 7 instead of 8. but there mustn’t be any problem to change the input to 8.


thanks for your reply, its an amazing solution with grasshopper :rose:

thanks for your reply i will look at this list :rose:

thanks for your reply its an amazing code, brief and correct.
i could solve first part with combination but for the second part finally i used number of boundary line and put a condition for that if there is one boundary line my form is acceptable but your code is wonderful
i want to try it but after test there is an error:
cannot import treehelpers from ghpythonlib
do you know how i can solve this problem?

Unfortunately, this library is not available for Rhino 5.

1 Like

OK maybe i should update my rhino :slightly_smiling_face:
thank for your guidance :rose:

Would you mind to comment the code (or explain line 7)? I can’t understand python,and I would like to recreate it in c#.

1 Like
 def qualify(indices):
     if len(JoinBreps([cells[index] for index in indices], 0.1)) == 1:
         return indices

That line is expanded to the following.

private List<int> qualify(List<int> indices)
    List<Brep> map = new List<Brep>;
    foreach(int i in indices)
    if (JoinBreps(map, 0.1).Count == 1)
        return indices;
    return null;
1 Like

Now I got it I think. Did not see the itertools thing. Seems there is nothing similar in c#.

1 Like

@meh, This one should be fine on Rhino 5.

private void RunScript(List<Brep> cells, int n, ref object A)
    var r = new ConcurrentBag<IEnumerable<int>>();
    Parallel.ForEach(Combinations(Enumerable.Range(0, cells.Count), n), indices =>
        if(Brep.JoinBreps(indices.Select(i => cells[i]), 0.5).Length == 1)
    var dt = new DataTree<int>();
    for(var i = 0; i < r.Count; i++)
        dt.AddRange(r.ElementAt(i), new GH_Path(i));
    A = dt;
// <Custom additional code> 
private static IEnumerable<IEnumerable<int>> Combinations(IEnumerable<int> list, int length)
    if (length == 1) return list.Select(t => new int[] { t });
    return Combinations(list, length - 1)
    .SelectMany(t => list.Where(o => o.CompareTo(t.Last()) > 0),
    (t1, t2) => t1.Concat(new int[] { t2 }));
// </Custom additional code> 

Possibilities (16.0 KB)


Thanks a lot @Mahdiyar for the material to study !

1 Like

Yesterday I adapted my array display to show all of your results, which was quite easy (white group), and then got bogged down and failed to group them by the number of edges. I succeeded at that today (cyan group), though used my ‘Tree/List Viewer’ tool to show them instead of creating multiple grids.

The text panel shows the number of edges in each branch: 4, 8, 10, 6, 12, 14 or 16. Use the ‘path idx’ slider to choose a branch of the tree, then the ‘list idx’ slider to examine all the shapes in that branch. All displayed in yellow with blue background at the top right of the array. (41.8 KB)


thanks again i updated my rhino so it worked. its exactly 1474 possible combination :pray: