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 #####
20 from bpy
.types
import (
24 from bpy
.props
import (StringProperty
, )
27 "name": "Dependency Graph Debug",
28 "author": "Sergey Sharybin",
30 "blender": (2, 80, 0),
31 "description": "Various dependency graph debugging tools",
35 "category": "Development"}
38 def _get_depsgraph(context
):
40 if bpy
.app
.version
< (2, 80, 0,):
41 return scene
.depsgraph
43 view_layer
= context
.view_layer
44 return view_layer
.depsgraph
47 ###############################################################################
48 # Save data from depsgraph to a specified file.
50 class SCENE_OT_depsgraph_save_common
:
51 filepath
: StringProperty(
53 description
="Filepath used for saving the file",
58 def _getExtension(self
, context
):
62 def poll(cls
, context
):
63 depsgraph
= _get_depsgraph(context
)
64 return depsgraph
is not None
66 def invoke(self
, context
, event
):
69 blend_filepath
= context
.blend_data
.filepath
70 if not blend_filepath
:
71 blend_filepath
= "deg"
73 blend_filepath
= os
.path
.splitext(blend_filepath
)[0]
75 self
.filepath
= blend_filepath
+ self
._getExtension
(context
)
76 context
.window_manager
.fileselect_add(self
)
77 return {'RUNNING_MODAL'}
79 def execute(self
, context
):
80 depsgraph
= _get_depsgraph(context
)
81 if not self
.performSave(context
, depsgraph
):
85 def performSave(self
, context
, depsgraph
):
89 class SCENE_OT_depsgraph_relations_graphviz(
91 SCENE_OT_depsgraph_save_common
,
93 bl_idname
= "scene.depsgraph_relations_graphviz"
94 bl_label
= "Save Depsgraph"
95 bl_description
= "Save current scene's dependency graph to a graphviz file"
97 def _getExtension(self
, context
):
100 def performSave(self
, context
, depsgraph
):
102 basename
, extension
= os
.path
.splitext(self
.filepath
)
103 depsgraph
.debug_relations_graphviz(os
.path
.join(self
.filepath
, basename
+ ".dot"))
107 class SCENE_OT_depsgraph_stats_gnuplot(
109 SCENE_OT_depsgraph_save_common
,
111 bl_idname
= "scene.depsgraph_stats_gnuplot"
112 bl_label
= "Save Depsgraph Stats"
113 bl_description
= "Save current scene's evaluaiton stats to gnuplot file"
115 def _getExtension(self
, context
):
118 def performSave(self
, context
, depsgraph
):
119 depsgraph
.debug_stats_gnuplot(self
.filepath
, "")
123 ###############################################################################
124 # Visualize some depsgraph information as an image opening in image editor.
126 class SCENE_OT_depsgraph_image_common
:
127 def _getOrCreateImageForAbsPath(self
, filepath
):
128 for image
in bpy
.data
.images
:
129 if image
.filepath
== filepath
:
132 return bpy
.data
.images
.load(filepath
, check_existing
=True)
134 def _findBestImageEditor(self
, context
, image
):
135 first_none_editor
= None
136 for area
in context
.screen
.areas
:
137 if area
.type != 'IMAGE_EDITOR':
139 for space
in area
.spaces
:
140 if space
.type != 'IMAGE_EDITOR':
143 first_none_editor
= space
145 if space
.image
== image
:
147 return first_none_editor
149 def _createTempFile(self
, suffix
):
152 fd
, filepath
= tempfile
.mkstemp(suffix
=suffix
)
156 def _openImageInEditor(self
, context
, image_filepath
):
157 image
= self
._getOrCreateImageForAbsPath
(image_filepath
)
158 editor
= self
._findBestImageEditor
(context
, image
)
162 def execute(self
, context
):
163 depsgraph
= _get_depsgraph(context
)
164 if not self
.performSave(context
, depsgraph
):
168 def performSave(self
, context
, depsgraph
):
172 class SCENE_OT_depsgraph_relations_image(Operator
,
173 SCENE_OT_depsgraph_image_common
):
174 bl_idname
= "scene.depsgraph_relations_image"
175 bl_label
= "Depsgraph as Image"
176 bl_description
= "Create new image datablock from the dependency graph"
178 def performSave(self
, context
, depsgraph
):
181 # Create temporary file.
182 dot_filepath
= self
._createTempFile
(suffix
=".dot")
183 # Save dependency graph to graphviz file.
184 depsgraph
.debug_relations_graphviz(dot_filepath
)
185 # Convert graphviz to PNG image.
186 png_filepath
= os
.path
.join(bpy
.app
.tempdir
, "depsgraph.png")
187 command
= ("dot", "-Tpng", dot_filepath
, "-o", png_filepath
)
189 subprocess
.run(command
)
190 self
._openImageInEditor
(context
, png_filepath
)
192 self
.report({'ERROR'}, "Error invoking dot command")
195 # Remove graphviz file.
196 os
.remove(dot_filepath
)
200 class SCENE_OT_depsgraph_stats_image(Operator
,
201 SCENE_OT_depsgraph_image_common
):
202 bl_idname
= "scene.depsgraph_stats_image"
203 bl_label
= "Depsgraph Stats as Image"
204 bl_description
= "Create new image datablock from the dependency graph " + \
205 "execution statistics"
207 def performSave(self
, context
, depsgraph
):
210 # Create temporary file.
211 plot_filepath
= self
._createTempFile
(suffix
=".plot")
212 png_filepath
= os
.path
.join(bpy
.app
.tempdir
, "depsgraph_stats.png")
213 # Save dependency graph stats to gnuplot file.
214 depsgraph
.debug_stats_gnuplot(plot_filepath
, png_filepath
)
215 # Convert graphviz to PNG image.
216 command
= ("gnuplot", plot_filepath
)
218 subprocess
.run(command
)
219 self
._openImageInEditor
(context
, png_filepath
)
221 self
.report({'ERROR'}, "Error invoking gnuplot command")
224 # Remove graphviz file.
225 os
.remove(plot_filepath
)
229 ###############################################################################
233 class SCENE_PT_depsgraph_common
:
234 def draw(self
, context
):
236 col
= layout
.column()
237 # Everything related on relations and graph topology.
238 col
.label(text
="Relations:")
240 row
.operator("scene.depsgraph_relations_graphviz")
241 row
.operator("scene.depsgraph_relations_image")
242 # Everything related on evaluaiton statistics.
243 col
.label(text
="Statistics:")
245 row
.operator("scene.depsgraph_stats_gnuplot")
246 row
.operator("scene.depsgraph_stats_image")
249 class SCENE_PT_depsgraph(bpy
.types
.Panel
, SCENE_PT_depsgraph_common
):
250 bl_label
= "Dependency Graph"
251 bl_space_type
= "PROPERTIES"
252 bl_region_type
= "WINDOW"
254 bl_options
= {'DEFAULT_CLOSED'}
257 def poll(cls
, context
):
258 if bpy
.app
.version
>= (2, 80, 0,):
260 depsgraph
= _get_depsgraph(context
)
261 return depsgraph
is not None
264 class RENDERLAYER_PT_depsgraph(bpy
.types
.Panel
, SCENE_PT_depsgraph_common
):
265 bl_label
= "Dependency Graph"
266 bl_space_type
= "PROPERTIES"
267 bl_region_type
= "WINDOW"
268 bl_context
= "view_layer"
269 bl_options
= {'DEFAULT_CLOSED'}
272 def poll(cls
, context
):
273 if bpy
.app
.version
< (2, 80, 0,):
275 depsgraph
= _get_depsgraph(context
)
276 return depsgraph
is not None
280 bpy
.utils
.register_class(SCENE_OT_depsgraph_relations_graphviz
)
281 bpy
.utils
.register_class(SCENE_OT_depsgraph_relations_image
)
282 bpy
.utils
.register_class(SCENE_OT_depsgraph_stats_gnuplot
)
283 bpy
.utils
.register_class(SCENE_OT_depsgraph_stats_image
)
284 bpy
.utils
.register_class(SCENE_PT_depsgraph
)
285 bpy
.utils
.register_class(RENDERLAYER_PT_depsgraph
)
289 bpy
.utils
.unregister_class(SCENE_OT_depsgraph_relations_graphviz
)
290 bpy
.utils
.unregister_class(SCENE_OT_depsgraph_relations_image
)
291 bpy
.utils
.unregister_class(SCENE_OT_depsgraph_stats_gnuplot
)
292 bpy
.utils
.unregister_class(SCENE_OT_depsgraph_stats_image
)
293 bpy
.utils
.unregister_class(SCENE_PT_depsgraph
)
294 bpy
.utils
.unregister_class(RENDERLAYER_PT_depsgraph
)
297 if __name__
== "__main__":