| 
									
										
										
										
											2009-11-16 22:53:28 +00:00
										 |  |  | # ##### BEGIN GPL LICENSE BLOCK ##### | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #  This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  | #  modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  | #  as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  | #  of the License, or (at your option) any later version. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #  This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | #  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | #  GNU General Public License for more details. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #  You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | #  along with this program; if not, write to the Free Software Foundation, | 
					
						
							| 
									
										
										
										
											2010-02-12 13:34:04 +00:00
										 |  |  | #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							| 
									
										
										
										
											2009-11-16 22:53:28 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # ##### END GPL LICENSE BLOCK ##### | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # <pep8 compliant> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import bpy | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-04 17:54:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-19 17:12:08 +00:00
										 |  |  | def rna_idprop_ui_get(item, create=True): | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         return item['_RNA_UI'] | 
					
						
							|  |  |  |     except: | 
					
						
							|  |  |  |         if create: | 
					
						
							|  |  |  |             item['_RNA_UI'] = {} | 
					
						
							|  |  |  |             return item['_RNA_UI'] | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             return None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def rna_idprop_ui_prop_get(item, prop, create=True): | 
					
						
							| 
									
										
										
										
											2009-11-21 23:55:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-19 17:12:08 +00:00
										 |  |  |     rna_ui = rna_idprop_ui_get(item, create) | 
					
						
							| 
									
										
										
										
											2009-11-21 23:55:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-18 10:43:32 +00:00
										 |  |  |     if rna_ui is None: | 
					
						
							| 
									
										
										
										
											2009-11-19 17:12:08 +00:00
										 |  |  |         return None | 
					
						
							| 
									
										
										
										
											2009-11-21 23:55:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-19 17:12:08 +00:00
										 |  |  |     try: | 
					
						
							|  |  |  |         return rna_ui[prop] | 
					
						
							|  |  |  |     except: | 
					
						
							|  |  |  |         rna_ui[prop] = {} | 
					
						
							|  |  |  |         return rna_ui[prop] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def rna_idprop_ui_prop_clear(item, prop): | 
					
						
							|  |  |  |     rna_ui = rna_idprop_ui_get(item, False) | 
					
						
							| 
									
										
										
										
											2009-11-21 23:55:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-18 10:43:32 +00:00
										 |  |  |     if rna_ui is None: | 
					
						
							| 
									
										
										
										
											2009-11-19 17:12:08 +00:00
										 |  |  |         return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         del rna_ui[prop] | 
					
						
							|  |  |  |     except: | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-16 22:53:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-17 10:33:28 +00:00
										 |  |  | def rna_idprop_context_value(context, context_member, property_type): | 
					
						
							|  |  |  |     space = context.space_data | 
					
						
							| 
									
										
										
										
											2011-01-01 07:20:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-19 13:04:14 +00:00
										 |  |  |     if space is None or isinstance(space, bpy.types.SpaceProperties): | 
					
						
							|  |  |  |         pin_id = space.pin_id | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         pin_id = None | 
					
						
							| 
									
										
										
										
											2010-12-17 10:33:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if pin_id and isinstance(pin_id, property_type): | 
					
						
							|  |  |  |         rna_item = pin_id | 
					
						
							|  |  |  |         context_member = "space_data.pin_id" | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         rna_item = eval("context." + context_member) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return rna_item, context_member | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def draw(layout, context, context_member, property_type, use_edit=True): | 
					
						
							| 
									
										
										
										
											2009-11-21 23:55:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-16 22:53:28 +00:00
										 |  |  |     def assign_props(prop, val, key): | 
					
						
							| 
									
										
										
										
											2010-06-14 03:52:10 +00:00
										 |  |  |         prop.data_path = context_member | 
					
						
							| 
									
										
										
										
											2009-11-16 22:53:28 +00:00
										 |  |  |         prop.property = key | 
					
						
							| 
									
										
										
										
											2009-11-21 23:55:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-16 22:53:28 +00:00
										 |  |  |         try: | 
					
						
							|  |  |  |             prop.value = str(val) | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             pass | 
					
						
							| 
									
										
										
										
											2009-11-21 23:55:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-17 10:33:28 +00:00
										 |  |  |     rna_item, context_member = rna_idprop_context_value(context, context_member, property_type) | 
					
						
							| 
									
										
										
										
											2009-12-04 17:54:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-24 00:02:21 +00:00
										 |  |  |     # poll should really get this... | 
					
						
							|  |  |  |     if not rna_item: | 
					
						
							|  |  |  |         return | 
					
						
							| 
									
										
										
										
											2009-11-16 22:53:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-23 06:09:53 +00:00
										 |  |  |     if rna_item.id_data.library is not None: | 
					
						
							|  |  |  |         use_edit = False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-17 10:33:28 +00:00
										 |  |  |     assert(isinstance(rna_item, property_type)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-16 22:53:28 +00:00
										 |  |  |     items = rna_item.items() | 
					
						
							|  |  |  |     items.sort() | 
					
						
							| 
									
										
										
										
											2009-11-21 23:55:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-18 20:01:35 +00:00
										 |  |  |     if use_edit: | 
					
						
							|  |  |  |         row = layout.row() | 
					
						
							| 
									
										
										
										
											2009-11-23 11:43:38 +00:00
										 |  |  |         props = row.operator("wm.properties_add", text="Add") | 
					
						
							| 
									
										
										
										
											2010-06-14 03:52:10 +00:00
										 |  |  |         props.data_path = context_member | 
					
						
							| 
									
										
										
										
											2009-11-18 20:01:35 +00:00
										 |  |  |         del row | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-02 14:43:22 +00:00
										 |  |  |     rna_properties = {prop.identifier for prop in rna_item.bl_rna.properties if prop.is_runtime} if items else None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-16 22:53:28 +00:00
										 |  |  |     for key, val in items: | 
					
						
							| 
									
										
										
										
											2009-11-19 18:22:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-18 20:01:35 +00:00
										 |  |  |         if key == '_RNA_UI': | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											2009-11-21 23:55:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-16 22:53:28 +00:00
										 |  |  |         row = layout.row() | 
					
						
							| 
									
										
										
										
											2011-06-17 05:45:46 +00:00
										 |  |  |         to_dict = getattr(val, "to_dict", None) | 
					
						
							|  |  |  |         to_list = getattr(val, "to_list", None) | 
					
						
							| 
									
										
										
										
											2009-11-21 23:55:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-10 12:51:37 +00:00
										 |  |  |         # val_orig = val  # UNUSED | 
					
						
							| 
									
										
										
										
											2011-06-17 05:45:46 +00:00
										 |  |  |         if to_dict: | 
					
						
							|  |  |  |             val = to_dict() | 
					
						
							|  |  |  |             val_draw = str(val) | 
					
						
							|  |  |  |         elif to_list: | 
					
						
							|  |  |  |             val = to_list() | 
					
						
							|  |  |  |             val_draw = str(val) | 
					
						
							| 
									
										
										
										
											2009-11-17 10:30:54 +00:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2009-11-17 13:18:26 +00:00
										 |  |  |             val_draw = val | 
					
						
							| 
									
										
										
										
											2009-11-17 10:30:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-16 22:53:28 +00:00
										 |  |  |         box = row.box() | 
					
						
							| 
									
										
										
										
											2009-11-19 18:22:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if use_edit: | 
					
						
							| 
									
										
										
										
											2009-11-17 10:30:54 +00:00
										 |  |  |             split = box.split(percentage=0.75) | 
					
						
							|  |  |  |             row = split.row() | 
					
						
							| 
									
										
										
										
											2009-11-16 22:53:28 +00:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2009-11-19 18:22:21 +00:00
										 |  |  |             row = box.row() | 
					
						
							| 
									
										
										
										
											2009-11-21 23:55:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-23 00:27:30 +00:00
										 |  |  |         row.label(text=key) | 
					
						
							| 
									
										
										
										
											2009-11-21 23:55:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-19 18:22:21 +00:00
										 |  |  |         # explicit exception for arrays | 
					
						
							| 
									
										
										
										
											2011-06-17 05:45:46 +00:00
										 |  |  |         if to_dict or to_list: | 
					
						
							| 
									
										
										
										
											2009-11-23 00:27:30 +00:00
										 |  |  |             row.label(text=val_draw) | 
					
						
							| 
									
										
										
										
											2009-11-19 18:22:21 +00:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2010-09-02 14:43:22 +00:00
										 |  |  |             if key in rna_properties: | 
					
						
							|  |  |  |                 row.prop(rna_item, key, text="") | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 row.prop(rna_item, '["%s"]' % key, text="") | 
					
						
							| 
									
										
										
										
											2009-11-21 23:55:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-19 18:22:21 +00:00
										 |  |  |         if use_edit: | 
					
						
							|  |  |  |             row = split.row(align=True) | 
					
						
							| 
									
										
										
										
											2012-01-01 13:09:58 +00:00
										 |  |  |             props = row.operator("wm.properties_edit", text="edit") | 
					
						
							|  |  |  |             assign_props(props, val_draw, key) | 
					
						
							| 
									
										
										
										
											2009-11-21 23:55:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-01 13:09:58 +00:00
										 |  |  |             props = row.operator("wm.properties_remove", text="", icon='ZOOMOUT') | 
					
						
							|  |  |  |             assign_props(props, val_draw, key) | 
					
						
							| 
									
										
										
										
											2009-11-21 23:55:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-16 22:53:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-02 02:55:12 +00:00
										 |  |  | class PropertyPanel(): | 
					
						
							| 
									
										
										
										
											2010-01-08 08:54:41 +00:00
										 |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2010-01-31 14:46:28 +00:00
										 |  |  |     The subclass should have its own poll function | 
					
						
							| 
									
										
										
										
											2010-01-08 08:54:41 +00:00
										 |  |  |     and the variable '_context_path' MUST be set. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     bl_label = "Custom Properties" | 
					
						
							| 
									
										
										
										
											2010-08-26 01:05:37 +00:00
										 |  |  |     bl_options = {'DEFAULT_CLOSED'} | 
					
						
							| 
									
										
										
										
											2010-01-08 08:54:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-09 01:37:09 +00:00
										 |  |  |     @classmethod | 
					
						
							|  |  |  |     def poll(cls, context): | 
					
						
							| 
									
										
										
										
											2010-12-17 10:33:28 +00:00
										 |  |  |         rna_item, context_member = rna_idprop_context_value(context, cls._context_path, cls._property_type) | 
					
						
							|  |  |  |         return bool(rna_item) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     def draw_header(self, context): | 
					
						
							|  |  |  |         rna_item, context_member = rna_idprop_context_value(context, self._context_path, self._property_type) | 
					
						
							|  |  |  |         tot = len(rna_item.keys()) | 
					
						
							|  |  |  |         if tot: | 
					
						
							|  |  |  |             self.layout().label("%d:" % tot) | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2010-08-09 01:37:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-08 08:54:41 +00:00
										 |  |  |     def draw(self, context): | 
					
						
							| 
									
										
										
										
											2010-12-17 10:33:28 +00:00
										 |  |  |         draw(self.layout, context, self._context_path, self._property_type) |