cleanup for python scripts - unused vars and imports
[blender-staging.git] / release / scripts / modules / rna_prop_ui.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 # <pep8 compliant>
20
21 import bpy
22
23
24 def rna_idprop_ui_get(item, create=True):
25     try:
26         return item['_RNA_UI']
27     except:
28         if create:
29             item['_RNA_UI'] = {}
30             return item['_RNA_UI']
31         else:
32             return None
33
34
35 def rna_idprop_ui_prop_get(item, prop, create=True):
36
37     rna_ui = rna_idprop_ui_get(item, create)
38
39     if rna_ui is None:
40         return None
41
42     try:
43         return rna_ui[prop]
44     except:
45         rna_ui[prop] = {}
46         return rna_ui[prop]
47
48
49 def rna_idprop_ui_prop_clear(item, prop):
50     rna_ui = rna_idprop_ui_get(item, False)
51
52     if rna_ui is None:
53         return
54
55     try:
56         del rna_ui[prop]
57     except:
58         pass
59
60
61 def rna_idprop_context_value(context, context_member, property_type):
62     space = context.space_data
63
64     if space is None or isinstance(space, bpy.types.SpaceProperties):
65         pin_id = space.pin_id
66     else:
67         pin_id = None
68
69     if pin_id and isinstance(pin_id, property_type):
70         rna_item = pin_id
71         context_member = "space_data.pin_id"
72     else:
73         rna_item = eval("context." + context_member)
74
75     return rna_item, context_member
76
77
78 def draw(layout, context, context_member, property_type, use_edit=True):
79
80     def assign_props(prop, val, key):
81         prop.data_path = context_member
82         prop.property = key
83
84         try:
85             prop.value = str(val)
86         except:
87             pass
88
89     rna_item, context_member = rna_idprop_context_value(context, context_member, property_type)
90
91     # poll should really get this...
92     if not rna_item:
93         return
94
95     assert(isinstance(rna_item, property_type))
96
97     items = rna_item.items()
98     items.sort()
99
100     if use_edit:
101         row = layout.row()
102         props = row.operator("wm.properties_add", text="Add")
103         props.data_path = context_member
104         del row
105
106     rna_properties = {prop.identifier for prop in rna_item.bl_rna.properties if prop.is_runtime} if items else None
107
108     for key, val in items:
109
110         if key == '_RNA_UI':
111             continue
112
113         row = layout.row()
114         to_dict = getattr(val, "to_dict", None)
115         to_list = getattr(val, "to_list", None)
116
117         # val_orig = val  # UNUSED
118         if to_dict:
119             val = to_dict()
120             val_draw = str(val)
121         elif to_list:
122             val = to_list()
123             val_draw = str(val)
124         else:
125             val_draw = val
126
127         box = row.box()
128
129         if use_edit:
130             split = box.split(percentage=0.75)
131             row = split.row()
132         else:
133             row = box.row()
134
135         row.label(text=key)
136
137         # explicit exception for arrays
138         if to_dict or to_list:
139             row.label(text=val_draw)
140         else:
141             if key in rna_properties:
142                 row.prop(rna_item, key, text="")
143             else:
144                 row.prop(rna_item, '["%s"]' % key, text="")
145
146         if use_edit:
147             row = split.row(align=True)
148             prop = row.operator("wm.properties_edit", text="edit")
149             assign_props(prop, val_draw, key)
150
151             prop = row.operator("wm.properties_remove", text="", icon='ZOOMOUT')
152             assign_props(prop, val_draw, key)
153
154
155 class PropertyPanel():
156     """
157     The subclass should have its own poll function
158     and the variable '_context_path' MUST be set.
159     """
160     bl_label = "Custom Properties"
161     bl_options = {'DEFAULT_CLOSED'}
162
163     @classmethod
164     def poll(cls, context):
165         rna_item, context_member = rna_idprop_context_value(context, cls._context_path, cls._property_type)
166         return bool(rna_item)
167
168     """
169     def draw_header(self, context):
170         rna_item, context_member = rna_idprop_context_value(context, self._context_path, self._property_type)
171         tot = len(rna_item.keys())
172         if tot:
173             self.layout().label("%d:" % tot)
174     """
175
176     def draw(self, context):
177         draw(self.layout, context, self._context_path, self._property_type)