# Macro to extract all leaf nodes of a multiblock dataset

**URL:** https://discourse.paraview.org/t/macro-to-extract-all-leaf-nodes-of-a-multiblock-dataset/15443
**Category:** ParaView Support
**Created:** [September 25, 2024, 8:21pm UTC](https://discourse.paraview.org/t/macro-to-extract-all-leaf-nodes-of-a-multiblock-dataset/15443 "2024-09-25T20:21:33Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![boonth](https://discourse.paraview.org/user_avatar/discourse.paraview.org/boonth/32/4763_2.png) [@boonth](https://discourse.paraview.org/u/boonth)
#### Post date: [September 25, 2024, 8:21pm UTC](https://discourse.paraview.org/t/macro-to-extract-all-leaf-nodes-of-a-multiblock-dataset/15443/1 "2024-09-25T20:21:33Z")

</div>

Hi,  
I would like to write a macro in which given an arbitrary multiblock dataset, apply Extract Blocks to every leaf block in that dataset.

@cory.quammen @spyridon97

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.paraview.org/user_avatar/discourse.paraview.org/cory.quammen/32/11193_2.png) [@cory.quammen](https://discourse.paraview.org/u/cory.quammen)
#### Post date: [October 2, 2024, 10:02am UTC](https://discourse.paraview.org/t/macro-to-extract-all-leaf-nodes-of-a-multiblock-dataset/15443/2 "2024-10-02T10:02:59Z")

</div>

Hi @boonth, here is a function that returns all selectors of leaf nodes in a multiblock dataset or a partitioned dataset collection:

```python
def get_selectors(source):
  dataInfo = source.GetDataInformation()
  dataAssembly = dataInfo.GetDataAssembly()
  if dataAssembly == None:
    dataAssembly = dataInfo.GetHierarchy()
  childNodeIds = [x for x in dataAssembly.GetChildNodes(0) if dataAssembly.GetNumberOfChildren(x) == 0]
  blockSelectors = [dataAssembly.GetNodePath(x) for x in childNodeIds]
  return blockSelectors

```

You can then iterate over the list of selectors returned to create an **Extract Blocks** filter for each selector with something like:

```python
blockSelectors = get_selectors(source)
for selector in blockSelectors:
  extract = ExtractBlock(Input=source)
  if source.GetDataInformation().GetDataAssembly():
    extract.Assembly = 'Assembly'
  else:
    extract.Assembly = 'Hierarchy'
  extract.Selectors = [selector]

```

Note the lines like `source.GetDataInformation().GetDataAssembly()` distinguish between dataset types where the hierarchy is encoded in a separate data assembly (partitioned dataset collections) and where the hierarchy is encoded as part of the data structure itset (multiblock dataset).

---

<div class="post-metadata">

### Author: ![boonth](https://discourse.paraview.org/user_avatar/discourse.paraview.org/boonth/32/4763_2.png) [@boonth](https://discourse.paraview.org/u/boonth)
#### Post date: [January 28, 2025, 10:15pm UTC](https://discourse.paraview.org/t/macro-to-extract-all-leaf-nodes-of-a-multiblock-dataset/15443/3 "2025-01-28T22:15:06Z")

</div>

Thanks, Cory! The code worked perfectly. I’m attaching the final macro for completeness sake.

[extract\_all\_blocks.py](https://discourse.paraview.org/uploads/short-url/v6btyMqZ0QZowsZbA1eM374TAZH.py) (7.9 KB)
