Splitting list in Python

I’m trying to explode a mesh and split the list of faces into branches using specific indices per list.

The issue arrives with the splitting list method I’m using. As the list increases, the code computes super slow compared to exploding the mesh and splitting with the native “list item” component.

The value is to avoid being dependent on gh native components and code more.

Explode n split mesh.gh (22.6 KB)

not at the pc at the moment but I think the main issue comes with setting ix to List Access, that way the whole Python script is triggered again by each list supplied by ix

so when you plug the 3 branches of 800 indexes each, it actually:

  • computes the whole mesh of 2400 faces
  • picks the first 800 (first branch of ix)
  • computes the whole mesh of 2400 faces again
  • picks items 801-1600 (second branch of ix)
  • computes the whole mesh for the third time
  • picks the items 1601-2400 (third branch of ix)

I would set ix to Tree Access, then use the treehelpers library to transform ix into a list of lists with something like th.tree_to_list(ix) and iterate through it to create a list_of_lists_of_mesh_faces

then, as last step, you will have to transform such a list into a tree that GH can “digest” with something like
F = th.list_to_tree(list_of_list_of_mesh_faces)

Hi Uri, do you really need individual meshes per face? For cases such as this I usually extract sub-meshes per list of face indices (i.e. to split analysis meshes etc.). You can use Mesh.Faces.ExtractFaces for this, which is both simple and fast:

Explode n split mesh_Anders.gh (17.7 KB)

If you do need one mesh per face, make sure to wrap them in Grasshopper.Kernel.Types.GH_Mesh before outputting. That should help speed things up. Edit: Here’s a quick example that’s quite performant, the viewport lag gets crazy though. Hence, I’d recommend not outputting this many meshes unless there’s a real good reason to:

Explode n split mesh_Anders_01.gh (15.4 KB)

1 Like

Hi Anders! Super helpful, as always.
All the unnecessary steps I was doing for such simple and smooth methods you used; indeed, the next step was a mesh join for the retrieved faces so the previews run faster.

1 Like