How to walk on a dictionary which represent a graph and return a list of element

Question:

I have a problem and I can not find a solution.

I have a graph like this:

The node graph in Maya

I have function which return the graph like this:

    data = {
    'Finition': {
        'Metal': {
            'colorCorrect1': {
                'Color': {
                    'aiLayerShader2': {
                        'colorConstant1': {},
                        'colorConstant3': {},
                        'colorConstant2': {
                            'aiFloatToRgba1': {
                                'place2dTexture1': {}
                                }
                            },
                        'colorConstant4': {},
                        'colorConstant5': {
                            'aiFloatToRgba1': {
                                'place2dTexture1': {}
                                }
                            },
                        'colorConstant6': {}
                        }
                    }
                }
            }
        }
    }

I have a list of main groups (Blues Nodes in the picture):
`

    selection = ['Finition', 'Metal', 'Color', 'colorConstant2']

I need a function that can return me the list of nodes (before the next group) for a specific group:

The return value should be like this:

    [
        ['Finition'],
        ['Metal', 'colorCorrect1'],
        ['Color', 'aiLayerShader2', 'colorConstant1', 'colorConstant3', 'colorConstant4', 'colorConstant5', 'colorConstant6', 'aiFloatToRgba1', 'place2dTexture1'],
        ['colorConstant2', 'aiFloatToRgba1', 'place2dTexture1']
    ]

I tried the following:

    def search_recurssive(element=None, main={}, depth=0):
        for key, value in main.items():
            if key != element:
                if isinstance(value, dict):
                    search_recurssive(element=element, main=value, depth=depth+1)
            else:
                pprint(value)
                print depth

    search_recurssive(element='Metal', main=data)

But it did not work. What can I try next?

Asked By: AlexLaur

||

Answers:

Here is a rather inefficient method:

def getChildren(data,s):
    global selection
    res = [s]
    for child in data[s].keys():
        if child in selection:
            continue
        else:
            res.extend(getChildren(data[s], child))
    return res

def getDataPart(data,s):
    for key in data.keys():
        if key == s:
            return data
        res = getDataPart(data[key],s)
        if res is not None:
            return res

results = []

for s in selection:
    data_part = getDataPart(data,s)
    results.append(getChildren(data_part,s))
Answered By: Ardweaden
def search_recurssive(element=None, main={}, depth=0):
    l = []
    for key, value in main.items():
        if key != element:
            if isinstance(value, dict):
                l.append(key)
                l += search_recurssive(element=element, main=value, depth=depth+1)
        else:
            pprint(value)
            print depth
    return l

I just adapted your code to work as you expect for the line

search_recurssive(element='Metal', main=data)

Now you need to adapt this to get the next sub graph and search for the next group.

Edit:
Just adapted my previous answer to do the full search for the selection.

selection = ['Finition', 'Metal', 'Color', 'colorConstant2']

next_group = data

def search_recurssive(element=None, main={}, depth=0):
    global next_group
    l = []
    for key, value in main.items():
        if key != element:
            if isinstance(value, dict):
                l.append(key)
                l += search_recurssive(element=element, main=value, depth=depth+1)
        else:
            print(value)
            print depth
            next_group = main
    return l

def search_selection(selection, data):
    return [search_recurssive(element, next_group) for element in selection]
Answered By: André Cascais
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.