Hello, devs!
I have a 3D scene tree represented as a hash map (JSON file). Is it possible to perform some tree operations with default Python tools or do I need to build (or use an existing library) a tree data structure?
For example, I need to know if “B” is a parent of “H”. I can recursively traverse the tree and find “H” node, but how can I start getting parents of a node I found (even if I had “parent” attribute with a string name of a parent node)?
scene_data = {
"hierarchy": {
"A": {
"properties": {
"metadata": {}
},
"children": {
"B": {
"properties": {
"metadata": {}
},
"children": {
"D": {
"properties": {
"metadata": {}
},
"children": {
"H": {
"properties": {
"metadata": {}
},
"children": {}
}
}
},
"E": {
"properties": {
"metadata": {}
},
"children": {
"I": {
"properties": {
"metadata": {}
},
"children": {}
}
}
}
}
},
"C": {
"properties": {
"metadata": {}
},
"children": {
"F": {
"properties": {
"metadata": {}
},
"children": {}
},
"G": {
"properties": {
"metadata": {}
},
"children": {}
}
}
}
}
}
}
}
def traverse_tree(node):
for node_name, node_data in node.iteritems():
if node_name == 'H':
print 'Node Found!'
traverse_tree(node_data['children'])
traverse_tree(scene_data['hierarchy'])