8ef50e20d76413def5f63b0578bc806411bab2c9
[blender.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 # <pep8 compliant>
20
21 import bpy
22
23 EVIL_PROP = "act_property"
24 EVIL_PROP_PATH = EVIL_PROP + '_path'
25 EVIL_PROP_VALUE = EVIL_PROP + '_value'
26 EVIL_PROP_PROP = EVIL_PROP + '_prop'
27 EVIL_PROP_PROP_ORIG = EVIL_PROP + '_prop_orig'
28
29 # nasty!, use a scene property to store the active edit item
30 def evil_prop_init():
31     Scene = bpy.types.Scene
32     if EVIL_PROP_PROP_ORIG not in Scene.bl_rna.properties:
33         Scene.StringProperty(attr=EVIL_PROP_PATH)
34         Scene.StringProperty(attr=EVIL_PROP_VALUE)
35         Scene.StringProperty(attr=EVIL_PROP_PROP)
36         Scene.StringProperty(attr=EVIL_PROP_PROP_ORIG)
37
38
39 def draw(layout, context, context_member):
40     
41     def assign_props(prop, val, key):
42         prop.path = context_member
43         prop.property = key
44         
45         try:
46             prop.value = str(val)
47         except:
48             pass
49     
50     rna_item = eval("context." + context_member)
51     
52     evil_prop_init()
53
54     scene = context.scene
55     
56     global_path = getattr(scene, EVIL_PROP_PATH)
57     global_value = getattr(scene, EVIL_PROP_VALUE)
58     global_prop = getattr(scene, EVIL_PROP_PROP)
59     global_prop_orig = getattr(scene, EVIL_PROP_PROP_ORIG)
60     
61     # print((global_path, global_value, global_prop, global_prop_orig))
62
63     items = rna_item.items()
64     items.sort()
65     
66     row = layout.row()
67     props = row.itemO("wm.properties_add", properties=True, text="Add")
68     props.path = context_member
69     
70     for key, val in items:
71         row = layout.row()
72         convert_to_pyobject = getattr(val, "convert_to_pyobject", None)
73         
74         val_orig = val
75         if convert_to_pyobject:
76             val_draw = val = val.convert_to_pyobject()
77             val_draw = str(val_draw)
78         else:
79             val_draw = val
80
81         box = row.box()
82         
83         if key == global_prop_orig and context_member == global_path:
84             split = box.split(percentage=0.75)
85             
86             row = split.row()
87             row.itemR(scene, EVIL_PROP_PROP)
88             row.itemR(scene, EVIL_PROP_VALUE)
89             
90             row = split.column()
91             prop = row.itemO("wm.properties_edit_end", properties=True, text="done")
92             assign_props(prop, val_draw, key)
93             
94         else:
95             split = box.split(percentage=0.75)
96             row = split.row()
97             row.itemL(text=key)
98             
99             # explicit exception for arrays
100             if convert_to_pyobject and not hasattr(val_orig, "len"):
101                 row.itemL(text=val_draw)
102             else:
103                 row.itemR(rna_item, '["%s"]' % key, text="")
104                 
105             
106             row = split.row(align=True)
107             prop = row.itemO("wm.properties_edit_begin", properties=True, text="edit")
108             assign_props(prop, val_draw, key)
109             
110             prop = row.itemO("wm.properties_remove", properties=True, text="", icon='ICON_ZOOMOUT')
111             assign_props(prop, val_draw, key)
112     
113
114 from bpy.props import *
115
116
117 rna_path = StringProperty(name="Property Edit",
118     description="Property path edit", maxlen=1024, default="")
119
120 rna_value = StringProperty(name="Property Value",
121     description="Property value edit", maxlen=1024, default="")
122
123 rna_property = StringProperty(name="Property Name",
124     description="Property name edit", maxlen=1024, default="")
125
126 class WM_OT_properties_edit_begin(bpy.types.Operator):
127     '''Internal use (edit a property path)'''
128     bl_idname = "wm.properties_edit_begin"
129     bl_label = "Edit Property"
130
131     path = rna_path
132     value = rna_value
133     property = rna_property
134
135     def execute(self, context):
136         scene = context.scene
137         
138         setattr(scene, EVIL_PROP_PATH, self.path)
139         setattr(scene, EVIL_PROP_VALUE, self.value)
140         setattr(scene, EVIL_PROP_PROP, self.property)
141         setattr(scene, EVIL_PROP_PROP_ORIG, self.property)
142         
143         return ('FINISHED',)
144
145
146 class WM_OT_properties_edit_end(bpy.types.Operator):
147     '''Internal use (edit a property path)'''
148     bl_idname = "wm.properties_edit_end"
149     bl_label = "Edit Property"
150
151     path = rna_path
152     value = rna_value
153     property = rna_property
154
155     def execute(self, context):
156         
157         scene = context.scene
158         global_path = getattr(scene, EVIL_PROP_PATH)
159         global_value = getattr(scene, EVIL_PROP_VALUE)
160         global_prop = getattr(scene, EVIL_PROP_PROP)
161         
162         setattr(scene, EVIL_PROP_PATH, "")
163         setattr(scene, EVIL_PROP_VALUE, "")
164         setattr(scene, EVIL_PROP_PROP, "")
165         setattr(scene, EVIL_PROP_PROP_ORIG, "")
166         
167         try:
168             value = eval(global_value)
169         except:
170             value = global_value
171         
172         if type(value) == str:
173             value = '"' + value + '"'
174         
175         
176         # First remove
177         exec_str = "del context.%s['%s']" % (global_path, self.property)
178         # print(exec_str)
179         exec(exec_str)
180         
181         
182         # Reassign
183         exec_str = "context.%s['%s'] = %s" % (global_path, global_prop, value)
184         # print(exec_str)
185         exec(exec_str)
186         
187         return ('FINISHED',)
188
189
190 class WM_OT_properties_add(bpy.types.Operator):
191     '''Internal use (edit a property path)'''
192     bl_idname = "wm.properties_add"
193     bl_label = "Add Property"
194
195     path = rna_path
196
197     def execute(self, context):
198         item = eval("context.%s" % self.path)
199         
200         def unique_name(names):
201             prop = 'prop'
202             prop_new = prop
203             i = 1
204             while prop_new in names:
205                 prop_new = prop + str(i)
206                 i+=1
207         
208             return prop_new
209         
210         property = unique_name(item.keys())
211         
212         item[property] = 1.0
213         return ('FINISHED',)
214         
215 class WM_OT_properties_remove(bpy.types.Operator):
216     '''Internal use (edit a property path)'''
217     bl_idname = "wm.properties_remove"
218     bl_label = "Add Property"
219
220     path = rna_path
221     property = rna_property
222
223     def execute(self, context):
224         item = eval("context.%s" % self.path)
225         del item[self.property]
226         return ('FINISHED',)