Boolean union loop

I’ve been playing around with Anemone to create boolean unions of a lot of breps that under some circumstances don’t union just like that and somehow thought I’d want to try this in python just to see if I can piece it together. This is what I came up with and it seems to work. I’m self tought and I’m excited to know if there’s something I could optimize in the script below?

import rhinoscriptsyntax as rs

i = 0
sphere = []
sphere.append(spheres[0])

max = count - 1

while i < max:
    i = i + 1
    sphere.append(spheres[i])
    union = rs.BooleanUnion(sphere, False)
    if rs.IsBrep(union):
        brep = union
    else:
        brep = brep

loop_union_python.gh (10.7 KB)

1 Like

Your script looks good, and it is great that you were able to create a script that works for you. However, here are a few suggestions that may help optimize it:

  1. Instead of appending to the “sphere” list inside the loop, you can use the “extend” method to add all the spheres at once: sphere.extend(spheres).
  2. You can use a try-except block to handle cases where rs.BooleanUnion fails to create a valid Brep. This way, you don’t need to create a separate variable for brep at the start of the loop:
try:
    brep = rs.BooleanUnion(sphere, False)
except:
    pass
  1. Consider using the “timeit” module to time how long it takes to run your script. This will give you a better idea of how efficient it is and whether you need to optimize further.
  2. Depending on the size and complexity of your geometry, it may be faster to use RhinoCommon instead of rhinoscriptsyntax. You can use the “rs.coercebrep” function to convert your spheres to Breps, and then use the “Brep.CreateBooleanUnion” method to perform the Boolean union. Here’s an example
import Rhino.Geometry as rg

breps = [rs.coercebrep(s) for s in spheres]
brep = rg.Brep.CreateBooleanUnion(breps, 0.001)
  1. Something about While cycles being very slow due to the interpreter, and “for” cycles being about 1.5x-2x faster

Anyway It looks already good, I’m just nitpicking

1 Like

Thanks for the feedback. I’ll try this.

The goal however is to be able to join lists of breps where union fails with all breps at once.