1

首先,对不起我的英语不是很好!我有一个问题,我找不到解决方案。

我有一个这样的图表:

Maya中的节点图

我有这样的函数返回图表:

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

我有一个主要组的列表(图中的蓝调节点):`

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

我需要一个可以返回特定组的节点列表(在下一组之前)的函数:

返回值应该是这样的:

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

我尝试了以下方法:

    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)

但它没有用。非常感谢你的帮助 !

4

2 回答 2

0

这是一个相当低效的方法:

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))
于 2019-12-30T11:27:59.757 回答
0
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

我刚刚调整了您的代码,使其按您期望的方式工作

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

现在您需要调整它以获取下一个子图并搜索下一个组。

编辑:刚刚调整了我之前的答案以对选择进行完整搜索。

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]
于 2019-12-30T11:49:39.847 回答