Fix T76383: Undo after cell fracture crashes
[blender-addons.git] / system_property_chart.py
blob4eeeb48e9895ad678087771ab1ae0b82d253815c
1 # ##### BEGIN GPL LICENSE BLOCK #####
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.
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.
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.
17 # ##### END GPL LICENSE BLOCK #####
19 # <pep8 compliant>
21 bl_info = {
22 "name": "Property Chart",
23 "author": "Campbell Barton (ideasman42)",
24 "version": (0, 1, 1),
25 "blender": (2, 80, 0),
26 "location": "View3D > Sidebar > Item Tab",
27 "description": ("Edit arbitrary selected properties for "
28 "objects/sequence strips of the same type"),
29 "warning": "",
30 "doc_url": "{BLENDER_MANUAL_URL}/addons/system/property_chart.html",
31 "category": "System",
34 """List properties of selected objects"""
36 import bpy
37 from bl_operators.presets import AddPresetBase
38 from bpy.types import (
39 Menu,
40 Operator,
41 Panel,
45 class AddPresetProperties(AddPresetBase, Operator):
46 """Add an properties preset"""
47 bl_idname = "scene.properties_preset_add"
48 bl_label = "Add Properties Preset"
49 preset_menu = "SCENE_MT_properties_presets"
51 preset_defines = [
52 "scene = bpy.context.scene",
55 def pre_cb(self, context):
56 space_type = context.space_data.type
57 if space_type == 'VIEW_3D':
58 self.preset_subdir = "system_property_chart_view3d"
59 self.preset_values = ["scene.view3d_edit_props"]
60 else:
61 self.preset_subdir = "system_property_chart_sequencer"
62 self.preset_values = ["scene.sequencer_edit_props"]
65 class SCENE_MT_properties_presets(Menu):
66 bl_label = "Properties Presets"
67 preset_operator = "script.execute_preset"
69 def draw(self, context):
70 space_type = context.space_data.type
72 if space_type == 'VIEW_3D':
73 self.preset_subdir = "system_property_chart_view3d"
74 else:
75 self.preset_subdir = "system_property_chart_sequencer"
77 Menu.draw_preset(self, context)
80 def _property_chart_data_get(self, context):
81 # eg. context.active_object
82 obj = eval("context.%s" % self.context_data_path_active)
84 if obj is None:
85 return None, None
87 # eg. context.selected_objects[:]
88 selected_objects = eval("context.%s" % self.context_data_path_selected)[:]
90 if not selected_objects:
91 return None, None
93 return obj, selected_objects
96 def _property_chart_draw(self, context):
97 """
98 This function can run for different types.
99 """
100 obj, selected_objects = _property_chart_data_get(self, context)
102 if not obj:
103 return
105 # active first
106 try:
107 active_index = selected_objects.index(obj)
108 except ValueError:
109 active_index = -1
111 if active_index > 0: # not the first already
112 selected_objects[0], selected_objects[active_index] = selected_objects[active_index], selected_objects[0]
114 id_storage = context.scene
116 strings = getattr(id_storage, self._PROP_STORAGE_ID)
118 # Collected all props, now display them all
119 layout = self.layout
121 if strings:
123 def obj_prop_get(obj, attr_string):
124 """return a pair (rna_base, "rna_property") to give to the rna UI property function"""
125 attrs = attr_string.split(".")
126 val_new = obj
127 for i, attr in enumerate(attrs):
128 val_old = val_new
129 val_new = getattr(val_old, attr, Ellipsis)
131 if val_new == Ellipsis:
132 return None, None
133 return val_old, attrs[-1]
135 strings = strings.split()
137 prop_all = []
139 for obj in selected_objects:
140 prop_pairs = []
141 prop_found = False
142 for attr_string in strings:
143 prop_pairs.append(obj_prop_get(obj, attr_string))
144 if prop_found is False and prop_pairs[-1] != (None, None):
145 prop_found = True
147 if prop_found:
148 prop_all.append((obj, prop_pairs))
150 row = layout.row(align=True)
152 col = row.column(align=True)
153 col.label(text="name")
154 for obj, prop_pairs in prop_all:
155 col.prop(obj, "name", text="")
157 for i in range(len(strings)):
158 col = row.column(align=True)
160 # name and copy button
161 rowsub = col.row(align=False)
162 rowsub.label(text=strings[i].rsplit(".", 1)[-1])
163 props = rowsub.operator("wm.chart_copy", text="", icon='PASTEDOWN', emboss=False)
164 props.data_path_active = self.context_data_path_active
165 props.data_path_selected = self.context_data_path_selected
166 props.data_path = strings[i]
168 for obj, prop_pairs in prop_all:
169 data, attr = prop_pairs[i]
170 # row is needed for vector buttons
171 if data:
172 col.row().prop(data, attr, text="") # , emboss=obj==active_object
173 else:
174 col.label(text="<missing>")
176 # Presets for properties
177 col = layout.column()
178 col.label(text="Properties")
179 row = col.row(align=True)
180 row.menu("SCENE_MT_properties_presets", text=bpy.types.SCENE_MT_properties_presets.bl_label)
181 row.operator("scene.properties_preset_add", text="", icon='ADD')
182 row.operator("scene.properties_preset_add", text="", icon='REMOVE').remove_active = True
183 # edit the display props
184 col.prop(id_storage, self._PROP_STORAGE_ID, text="")
187 class View3DEditProps(Panel):
188 bl_idname = "SYSPROP_CHART_PT_view3d"
189 bl_space_type = 'VIEW_3D'
190 bl_region_type = 'UI'
191 bl_category = "Item"
192 bl_label = "Property Chart"
193 bl_context = "objectmode"
194 bl_options = {'DEFAULT_CLOSED'}
196 _PROP_STORAGE_ID = "view3d_edit_props"
197 _PROP_STORAGE_ID_DESCR = "Properties of objects in the context"
198 _PROP_STORAGE_DEFAULT = "data data.use_auto_smooth"
200 # _property_chart_draw needs these
201 context_data_path_active = "active_object"
202 context_data_path_selected = "selected_objects"
204 draw = _property_chart_draw
207 class SequencerEditProps(Panel):
208 bl_idname = "SYSPROP_CHART_PT_sequencer"
209 bl_space_type = 'SEQUENCE_EDITOR'
210 bl_region_type = 'UI'
211 bl_category = "Item"
212 bl_label = "Property Chart"
213 bl_options = {'DEFAULT_CLOSED'}
215 _PROP_STORAGE_ID = "sequencer_edit_props"
216 _PROP_STORAGE_ID_DESCR = "Properties of sequencer strips in the context"
217 _PROP_STORAGE_DEFAULT = "blend_type blend_alpha"
219 # _property_chart_draw needs these
220 context_data_path_active = "scene.sequence_editor.active_strip"
221 context_data_path_selected = "selected_sequences"
223 draw = _property_chart_draw
225 @classmethod
226 def poll(cls, context):
227 return context.scene.sequence_editor is not None
229 # Operator to copy properties
232 def _property_chart_copy(self, context):
233 obj, selected_objects = _property_chart_data_get(self, context)
235 if not obj:
236 return
238 data_path = self.data_path
240 # quick & nasty method!
241 for obj_iter in selected_objects:
242 if obj != obj_iter:
243 try:
244 exec("obj_iter.%s = obj.%s" % (data_path, data_path))
245 except:
246 # just in case we need to know what went wrong!
247 import traceback
248 traceback.print_exc()
250 from bpy.props import StringProperty
253 class CopyPropertyChart(Operator):
254 "Open a path in a file browser"
255 bl_idname = "wm.chart_copy"
256 bl_label = "Copy properties from active to selected"
258 data_path_active: StringProperty()
259 data_path_selected: StringProperty()
260 data_path: StringProperty()
262 def execute(self, context):
263 # so attributes are found for '_property_chart_data_get()'
264 self.context_data_path_active = self.data_path_active
265 self.context_data_path_selected = self.data_path_selected
267 _property_chart_copy(self, context)
269 return {'FINISHED'}
271 # List The Classes #
273 classes = (
274 AddPresetProperties,
275 SCENE_MT_properties_presets,
276 View3DEditProps,
277 SequencerEditProps,
278 CopyPropertyChart
281 def register():
282 for cls in classes:
283 bpy.utils.register_class(cls)
285 Scene = bpy.types.Scene
287 for cls in View3DEditProps, SequencerEditProps:
288 setattr(
289 Scene,
290 cls._PROP_STORAGE_ID,
291 StringProperty(
292 name="Properties",
293 description=cls._PROP_STORAGE_ID_DESCR + " (separated by spaces)",
294 default=cls._PROP_STORAGE_DEFAULT, maxlen=1024,
299 def unregister():
300 for cls in classes:
301 bpy.utils.unregister_class(cls)
303 Scene = bpy.types.Scene
305 for cls in View3DEditProps, SequencerEditProps:
306 delattr(Scene,
307 cls._PROP_STORAGE_ID,
311 if __name__ == "__main__":
312 register()