def layers_ui(layers, layout):
    """ Turn a list of booleans + a list of names into a layer UI.
    """
    code = """
class RigLayers(bpy.types.Panel):
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_label = "Rig Layers"
    bl_idname = rig_id + "_PT_rig_layers"

    @classmethod
    def poll(self, context):
        try:
            return (context.active_object.data.get("rig_id") == rig_id)
        except (AttributeError, KeyError, TypeError):
            return False

    def draw(self, context):
        layout = self.layout
        col = layout.column()
"""
    rows = {}
    for i in range(28):
        if layers[i]:
            if layout[i][1] not in rows:
                rows[layout[i][1]] = []
            rows[layout[i][1]] += [(layout[i][0], i)]
    keys = list(rows.keys())
    keys.sort()
    for key in keys:
        code += """
        row = col.row()
"""
        i = 0
        for l in rows[key]:
            if i > 3:
                code += '\n        row = col.row()\n'
                i = 0
            code += (
                """        row.prop(context.active_object.data, 'layers', index=%s, toggle=True, text='%s')
"""
                 % (str(l[1]), l[0]))
            i += 1
    code += """
        row = col.row()"""
    code += """
        row.separator()"""
    code += """
        row = col.row()"""
    code += """
        row.separator()
"""
    code += """
        row = col.row()
"""
    code += """        row.prop(context.active_object.data, 'layers', index=28, toggle=True, text='Root')
"""
    return code
