我正在尝试迭代一个json文件,这样我的ui - 如果它在场景中找到地理位置,它会将信息附加到第一列,同时这样做,它会为它找到的每个地图附加颜色选项在第二列(颜色选项来自json文件)
虽然我可以在第一列中添加地理位置,但是在将颜色选项添加到第二列中时遇到问题,这些颜色选项已添加到组合框中
EG。在我的场景中有一个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"
]
}
添加,而不是看到我的组合框的每个字段填充颜色的名称,我得到每个字段一个字符。
有人可以向我提供任何见解吗?
答案 0 :(得分:2)
这一点get_color()
:
for item in (data[name]):
print "{0} - {1}".format(name, item)
return item
将在您执行所有颜色之前从您的函数返回(一旦它返回return语句)。
您可能希望在返回之前累积所有颜色。类似的东西:
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)
将单一颜色视为列表,并对其进行迭代以填充选项。修复第一部分也将解决这个问题。