json 文件未正确填充我的组合框

json file is not populating my comboboxes correctly

我正在尝试迭代一个 json 文件,这样我的 ui - 如果它在场景中找到了 geos,它会将信息附加到第一列,同时它会附加它在第二列中找到的每个地理位置的颜色选项(颜色选项来自 json 文件)

虽然我可以将地理信息添加到第一列中,但我在将颜色选项添加到充满组合框的第二列中时遇到了问题

例如。在我的场景中有一个 pCube 和一个 pPlane,而不是让我的组合框填充选项,它似乎正在抓住它找到的最后一个地理对象并只填充 pPlane 的一个颜色选项。

def get_all_mesh():
    all_mesh = cmds.listRelatives(cmds.ls(type = 'mesh'), parent=True)
    # Result: [u'pCube1', u'pSphere1', u'pPlane1'] #
    return all_mesh

def get_color():
    with open('/Desktop/colors.json') as data_file:
        data = json.load(data_file)   

        for index, name in enumerate(data):
            geo_names = get_all_mesh()
            for geo in geo_names:
                # Remove all the digits
                geo_exclude_num = ''.join(i for i in geo if not i.isdigit())
                if geo_exclude_num in name:
                    for item in (data[name]):
                        print "{0} - {1}".format(name, item)
                        return item


class testTableView(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.setWindowTitle('Color Test')
        self.setModal(False)

        self.all_mesh = get_all_mesh()

        # Build the GUI
        self.init_ui()
        self.populate_data()


    def init_ui(self):
        # Table setup
        self.mesh_table = QtGui.QTableWidget()
        self.mesh_table.setRowCount(len(self.all_mesh))
        self.mesh_table.setColumnCount(3)
        self.mesh_table.setHorizontalHeaderLabels(['Mesh Found', 'Color for Mesh'])
        self.md_insert_color_btn = QtGui.QPushButton('Apply color')

        # Layout
        self.layout = QtGui.QVBoxLayout()
        self.layout.addWidget(self.mesh_table)
        self.layout.addWidget(self.md_insert_color_btn)
        self.setLayout(self.layout)

    def populate_data(self):
        geo_name = self.all_mesh
        for row_index, geo_item in enumerate(geo_name):
            new_item = QtGui.QTableWidgetItem(geo_item)
            # Add in each and every mesh found in scene and append them into rows
            self.mesh_table.setItem(row_index, 0, new_item)

            # Insert in the color
            combobox = QtGui.QComboBox()
            color_list = get_color()
            combobox.addItems(color_list)
            self.mesh_table.setCellWidget(row_index, 1, combobox)


# To opent the dialog window
dialog = testTableView()
dialog.show()

这是我的 json 文件中的内容:

{
    "pCube": [
        "blue", 
        "purple", 
        "yellow", 
        "green", 
        "white", 
        "silver", 
        "red"
    ], 
    "pCone": [
        "black", 
        "yellow"
    ], 
    "pSphere": [
        "silver"
    ], 
    "pPlane": [
        "red", 
        "yellow"
    ], 
    "pPrism": [
        "white"
    ]
}

此外,我没有看到我的组合框的每个字段都填充了颜色名称,而是每个字段只有一个字符。

有人可以给我提供任何见解吗?

get_color()的这一点:

for item in (data[name]):
    print "{0} - {1}".format(name, item)
    return item

将 return 从你的函数中(一旦它命中 return 语句)在遍历你所有的颜色之前。

您可能想在 returning 之前积累所有颜色。类似于:

def get_color():
    with open('/Desktop/colors.json') as data_file:
        data = json.load(data_file)   

        items = set()            
        for index, name in enumerate(data):
            geo_names = get_all_mesh()
            for geo in geo_names:
                # Remove all the digits
                geo_exclude_num = ''.join(i for i in geo if not i.isdigit())
                if geo_exclude_num in name:
                    for item in (data[name]):
                        print "{0} - {1}".format(name, item)
                        items.add(item)
    return items

它向您显示第一种颜色的字符列表的原因是因为以下语句:

combobox.addItems(color_list)

正在将单一颜色视为一个列表,并对其进行迭代以填充选项。修复第一部分也会解决这个问题。