I have written a Python reader to load the files I am getting from my simulations. The file contains a hierarchy of groups and meshes and I create a vtkMultiBlockDataSet hierarchy with vtkUnstructuredGrids for the meshes.
The loader works fine, the only issue is that I don’t know how to assign a name to each VTK object the reader is creating.
Given a vtkMultiBlockDataSet or vtkUnstructuredGrid created in Python, how can I assign names to them? Right now the hierarchy looks okay in the MultiBlock Inspector, but its very difficult to understand what is each block without names.
vtkCompositeDataIterator has a GetCurrentMetaData() method. vtkCompositeDataSet::NAME() returns a string information key that holds a name for the matching dataset.
So basically assigning “asdf” as name for each of the MultiBlockDataSet I am creating.
Executes fine, but in the Multi-Block Inspector, the name doesn’t appear, still says “Block0”
I still dont totally understand how to use the iterator to assign the names to the datasets I am loading.
I am creating MultiBlockDataSets following the hierarchy I have in an HDF5, using a recursive function.
def frame_scene(current_group):
mb = vtkMultiBlockDataSet()
# mb.SetName(current_group.name)
mb.SetNumberOfBlocks(len(current_group))
# Iterate on groups
if type(current_group) == h5py.Group:
for id, group in enumerate(current_group.values()):
if "mesh" in group.attrs:
mesh = parse_mesh_group(group) # Returns None or a vtkUnstructuredGrid
if mesh is not None:
mb.SetBlock(id, mesh)
else:
children_mb = frame_scene(group) # Returns a vtkMultiBlockDataSet
mb.SetBlock(id, children_mb)
it = mb.NewIterator()
it.GetCurrentMetaData().Set(vtkMultiBlockDataSet.NAME(), "adsf")
it.Delete()
return mb
This code only assigns “adsf” name to the first leaf node in each branch…
Your iterator is not iterating.
By default it points to the first block in your MBDS (flat index of 0), so you’re only setting the name of the first child each time. You need something like:
it = mb.NewIterator()
idx = 0
while not it.IsDoneWithTraversal():
it.GetCurrentMetaData().Set(vtkMultiBlockDataSet.NAME(), "name"+str(idx))
idx += 1
it.GoToNextItem()
iter = frame.NewIterator()
iter.SetVisitOnlyLeaves(False)
iter.SetSkipEmptyNodes(False)
iter.InitTraversal()
while not iter.IsDoneWithTraversal():
currentIdx += 1
name = pathlib.PurePath(names[currentIdx]).name
iter.GetCurrentMetaData().Set(
vtkMultiBlockDataSet.NAME(), name)
iter.GoToNextItem()
being frame the root vtkMultiBlockDataSet. Is there any way to also set the name for this root? The code only sets the children (and children of children)