File headers: use SPDX license identifiers
[blender-addons.git] / depsgraph_debug.py
blob4405eb8e3b67d08bc06db162744fcaf36edc7001
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 import bpy
4 from bpy.types import (
5 Operator,
6 Panel,
8 from bpy.props import (StringProperty, )
10 bl_info = {
11 "name": "Dependency Graph Debug",
12 "author": "Sergey Sharybin",
13 "version": (0, 1),
14 "blender": (2, 80, 0),
15 "description": "Various dependency graph debugging tools",
16 "warning": "",
17 "doc_url": "",
18 "tracker_url": "",
19 "category": "Development",
23 def _get_depsgraph(context):
24 scene = context.scene
25 if bpy.app.version < (2, 80, 0,):
26 return scene.depsgraph
27 else:
28 view_layer = context.view_layer
29 return view_layer.depsgraph
32 ###############################################################################
33 # Save data from depsgraph to a specified file.
35 class SCENE_OT_depsgraph_save_common:
36 filepath: StringProperty(
37 name="File Path",
38 description="Filepath used for saving the file",
39 maxlen=1024,
40 subtype='FILE_PATH',
43 def _getExtension(self, context):
44 return ""
46 @classmethod
47 def poll(cls, context):
48 depsgraph = _get_depsgraph(context)
49 return depsgraph is not None
51 def invoke(self, context, event):
52 import os
53 if not self.filepath:
54 blend_filepath = context.blend_data.filepath
55 if not blend_filepath:
56 blend_filepath = "deg"
57 else:
58 blend_filepath = os.path.splitext(blend_filepath)[0]
60 self.filepath = blend_filepath + self._getExtension(context)
61 context.window_manager.fileselect_add(self)
62 return {'RUNNING_MODAL'}
64 def execute(self, context):
65 depsgraph = _get_depsgraph(context)
66 if not self.performSave(context, depsgraph):
67 return {'CANCELLED'}
68 return {'FINISHED'}
70 def performSave(self, context, depsgraph):
71 pass
74 class SCENE_OT_depsgraph_relations_graphviz(
75 Operator,
76 SCENE_OT_depsgraph_save_common,
78 bl_idname = "scene.depsgraph_relations_graphviz"
79 bl_label = "Save Depsgraph"
80 bl_description = "Save current scene's dependency graph to a graphviz file"
82 def _getExtension(self, context):
83 return ".dot"
85 def performSave(self, context, depsgraph):
86 import os
87 basename, extension = os.path.splitext(self.filepath)
88 depsgraph.debug_relations_graphviz(os.path.join(self.filepath, basename + ".dot"))
89 return True
92 class SCENE_OT_depsgraph_stats_gnuplot(
93 Operator,
94 SCENE_OT_depsgraph_save_common,
96 bl_idname = "scene.depsgraph_stats_gnuplot"
97 bl_label = "Save Depsgraph Stats"
98 bl_description = "Save current scene's evaluaiton stats to gnuplot file"
100 def _getExtension(self, context):
101 return ".plot"
103 def performSave(self, context, depsgraph):
104 depsgraph.debug_stats_gnuplot(self.filepath, "")
105 return True
108 ###############################################################################
109 # Visualize some depsgraph information as an image opening in image editor.
111 class SCENE_OT_depsgraph_image_common:
112 def _getOrCreateImageForAbsPath(self, filepath):
113 for image in bpy.data.images:
114 if image.filepath == filepath:
115 image.reload()
116 return image
117 return bpy.data.images.load(filepath, check_existing=True)
119 def _findBestImageEditor(self, context, image):
120 first_none_editor = None
121 for area in context.screen.areas:
122 if area.type != 'IMAGE_EDITOR':
123 continue
124 for space in area.spaces:
125 if space.type != 'IMAGE_EDITOR':
126 continue
127 if not space.image:
128 first_none_editor = space
129 else:
130 if space.image == image:
131 return space
132 return first_none_editor
134 def _createTempFile(self, suffix):
135 import os
136 import tempfile
137 fd, filepath = tempfile.mkstemp(suffix=suffix)
138 os.close(fd)
139 return filepath
141 def _openImageInEditor(self, context, image_filepath):
142 image = self._getOrCreateImageForAbsPath(image_filepath)
143 editor = self._findBestImageEditor(context, image)
144 if editor:
145 editor.image = image
147 def execute(self, context):
148 depsgraph = _get_depsgraph(context)
149 if not self.performSave(context, depsgraph):
150 return {'CANCELLED'}
151 return {'FINISHED'}
153 def performSave(self, context, depsgraph):
154 pass
157 class SCENE_OT_depsgraph_relations_image(Operator,
158 SCENE_OT_depsgraph_image_common):
159 bl_idname = "scene.depsgraph_relations_image"
160 bl_label = "Depsgraph as Image"
161 bl_description = "Create new image datablock from the dependency graph"
163 def performSave(self, context, depsgraph):
164 import os
165 import subprocess
166 # Create temporary file.
167 dot_filepath = self._createTempFile(suffix=".dot")
168 # Save dependency graph to graphviz file.
169 depsgraph.debug_relations_graphviz(dot_filepath)
170 # Convert graphviz to PNG image.
171 png_filepath = os.path.join(bpy.app.tempdir, "depsgraph.png")
172 command = ("dot", "-Tpng", dot_filepath, "-o", png_filepath)
173 try:
174 subprocess.run(command)
175 self._openImageInEditor(context, png_filepath)
176 except:
177 self.report({'ERROR'}, "Error invoking dot command")
178 return False
179 finally:
180 # Remove graphviz file.
181 os.remove(dot_filepath)
182 return True
185 class SCENE_OT_depsgraph_stats_image(Operator,
186 SCENE_OT_depsgraph_image_common):
187 bl_idname = "scene.depsgraph_stats_image"
188 bl_label = "Depsgraph Stats as Image"
189 bl_description = "Create new image datablock from the dependency graph " + \
190 "execution statistics"
192 def performSave(self, context, depsgraph):
193 import os
194 import subprocess
195 # Create temporary file.
196 plot_filepath = self._createTempFile(suffix=".plot")
197 png_filepath = os.path.join(bpy.app.tempdir, "depsgraph_stats.png")
198 # Save dependency graph stats to gnuplot file.
199 depsgraph.debug_stats_gnuplot(plot_filepath, png_filepath)
200 # Convert graphviz to PNG image.
201 command = ("gnuplot", plot_filepath)
202 try:
203 subprocess.run(command)
204 self._openImageInEditor(context, png_filepath)
205 except:
206 self.report({'ERROR'}, "Error invoking gnuplot command")
207 return False
208 finally:
209 # Remove graphviz file.
210 os.remove(plot_filepath)
211 return True
214 ###############################################################################
215 # Interface.
218 class SCENE_PT_depsgraph_common:
219 def draw(self, context):
220 layout = self.layout
221 col = layout.column()
222 # Everything related on relations and graph topology.
223 col.label(text="Relations:")
224 row = col.row()
225 row.operator("scene.depsgraph_relations_graphviz")
226 row.operator("scene.depsgraph_relations_image")
227 # Everything related on evaluaiton statistics.
228 col.label(text="Statistics:")
229 row = col.row()
230 row.operator("scene.depsgraph_stats_gnuplot")
231 row.operator("scene.depsgraph_stats_image")
234 class SCENE_PT_depsgraph(bpy.types.Panel, SCENE_PT_depsgraph_common):
235 bl_label = "Dependency Graph"
236 bl_space_type = "PROPERTIES"
237 bl_region_type = "WINDOW"
238 bl_context = "scene"
239 bl_options = {'DEFAULT_CLOSED'}
241 @classmethod
242 def poll(cls, context):
243 if bpy.app.version >= (2, 80, 0,):
244 return False
245 depsgraph = _get_depsgraph(context)
246 return depsgraph is not None
249 class RENDERLAYER_PT_depsgraph(bpy.types.Panel, SCENE_PT_depsgraph_common):
250 bl_label = "Dependency Graph"
251 bl_space_type = "PROPERTIES"
252 bl_region_type = "WINDOW"
253 bl_context = "view_layer"
254 bl_options = {'DEFAULT_CLOSED'}
256 @classmethod
257 def poll(cls, context):
258 if bpy.app.version < (2, 80, 0,):
259 return False
260 depsgraph = _get_depsgraph(context)
261 return depsgraph is not None
264 def register():
265 bpy.utils.register_class(SCENE_OT_depsgraph_relations_graphviz)
266 bpy.utils.register_class(SCENE_OT_depsgraph_relations_image)
267 bpy.utils.register_class(SCENE_OT_depsgraph_stats_gnuplot)
268 bpy.utils.register_class(SCENE_OT_depsgraph_stats_image)
269 bpy.utils.register_class(SCENE_PT_depsgraph)
270 bpy.utils.register_class(RENDERLAYER_PT_depsgraph)
273 def unregister():
274 bpy.utils.unregister_class(SCENE_OT_depsgraph_relations_graphviz)
275 bpy.utils.unregister_class(SCENE_OT_depsgraph_relations_image)
276 bpy.utils.unregister_class(SCENE_OT_depsgraph_stats_gnuplot)
277 bpy.utils.unregister_class(SCENE_OT_depsgraph_stats_image)
278 bpy.utils.unregister_class(SCENE_PT_depsgraph)
279 bpy.utils.unregister_class(RENDERLAYER_PT_depsgraph)
282 if __name__ == "__main__":
283 register()