json file is not populating my comboboxes correctly
<p>I am trying to iterate a json file such that my ui - if it found geos in scene, it will append the info into the first column and while doing so, it will append the color options for each of the geos it found in the second column (color options comes from a json file)</p>

<p>While I am able to add in geos into the first column, I am having issues in getting the color options to be added into the second column which are filled with comboboxes</p>

<p>Eg. There is a pCube and a pPlane in my scene, instead of having my comboboxes populated with the options, it seems to be grabbing the last geo object it found and populate just one of the color options for pPlane.</p>

<pre><code>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()
</code></pre>

<p>This is the contents in my json file:</p>

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

<p>Adding on, instead of seeing per field of my combobox to be filled with the names of the color, I am getting a single character per field.</p>

<p>Can someone kindly provide me any insights?</p>

<p>This bit of <code>get_color()</code>:</p>

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

<p>will return from your function (as soon as it hits the return statement) before going though all your colors.</p>

<p>You probably want to accumulate all your colors before returning.  Something like:  </p>

<pre><code>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
</code></pre>

<p>The reasons its showing you a list of characters for the first color is because this statement:</p>

<pre><code>combobox.addItems(color_list)
</code></pre>

<p>is treating that single color as a list, and iterating over it to populate the options.  Fixing the first part will fix this too.</p>

python json combobox maya