mirrored from https://projects.blender.org/blender/blender-addons.git
-
Notifications
You must be signed in to change notification settings - Fork 188
/
depsgraph_debug.py
318 lines (264 loc) · 10.4 KB
/
depsgraph_debug.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# SPDX-FileCopyrightText: 2017-2023 Blender Foundation
#
# SPDX-License-Identifier: GPL-2.0-or-later
import bpy
from bpy.types import (
Operator,
Panel,
)
from bpy.props import (StringProperty, )
bl_info = {
"name": "Dependency Graph Debug",
"author": "Sergey Sharybin",
"version": (0, 1),
"blender": (2, 80, 0),
"description": "Various dependency graph debugging tools",
"location": "Properties > View Layer > Dependency Graph",
"warning": "",
"doc_url": "",
"tracker_url": "",
"category": "Development",
}
def _get_depsgraph(context):
scene = context.scene
if bpy.app.version < (2, 80, 0,):
return scene.depsgraph
else:
view_layer = context.view_layer
return view_layer.depsgraph
###############################################################################
# Save data from depsgraph to a specified file.
class SCENE_OT_depsgraph_save_common:
filepath: StringProperty(
name="File Path",
description="Filepath used for saving the file",
maxlen=1024,
subtype='FILE_PATH',
)
def _getExtension(self, context):
return ""
@classmethod
def poll(cls, context):
depsgraph = _get_depsgraph(context)
return depsgraph is not None
def invoke(self, context, event):
import os
if not self.filepath:
blend_filepath = context.blend_data.filepath
if not blend_filepath:
blend_filepath = "deg"
else:
blend_filepath = os.path.splitext(blend_filepath)[0]
self.filepath = blend_filepath + self._getExtension(context)
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
def execute(self, context):
depsgraph = _get_depsgraph(context)
if not self.performSave(context, depsgraph):
return {'CANCELLED'}
return {'FINISHED'}
def performSave(self, context, depsgraph):
pass
class SCENE_OT_depsgraph_relations_graphviz(
Operator,
SCENE_OT_depsgraph_save_common,
):
bl_idname = "scene.depsgraph_relations_graphviz"
bl_label = "Save Depsgraph"
bl_description = "Save current scene's dependency graph to a graphviz file"
def _getExtension(self, context):
return ".dot"
def performSave(self, context, depsgraph):
import os
basename, extension = os.path.splitext(self.filepath)
depsgraph.debug_relations_graphviz(os.path.join(self.filepath, basename + ".dot"))
return True
class SCENE_OT_depsgraph_stats_gnuplot(
Operator,
SCENE_OT_depsgraph_save_common,
):
bl_idname = "scene.depsgraph_stats_gnuplot"
bl_label = "Save Depsgraph Stats"
bl_description = "Save current scene's evaluaiton stats to gnuplot file"
def _getExtension(self, context):
return ".plot"
def performSave(self, context, depsgraph):
depsgraph.debug_stats_gnuplot(self.filepath, "")
return True
###############################################################################
# Visualize some depsgraph information as an image opening in image editor.
class SCENE_OT_depsgraph_image_common:
def _getOrCreateImageForAbsPath(self, filepath):
for image in bpy.data.images:
if image.filepath == filepath:
image.reload()
return image
return bpy.data.images.load(filepath, check_existing=True)
def _findBestImageEditor(self, context, image):
first_none_editor = None
for area in context.screen.areas:
if area.type != 'IMAGE_EDITOR':
continue
for space in area.spaces:
if space.type != 'IMAGE_EDITOR':
continue
if not space.image:
first_none_editor = space
else:
if space.image == image:
return space
return first_none_editor
def _createTempFile(self, suffix):
import os
import tempfile
fd, filepath = tempfile.mkstemp(suffix=suffix)
os.close(fd)
return filepath
def _openImageInEditor(self, context, image_filepath):
image = self._getOrCreateImageForAbsPath(image_filepath)
editor = self._findBestImageEditor(context, image)
if editor:
editor.image = image
def execute(self, context):
depsgraph = _get_depsgraph(context)
if not self.performSave(context, depsgraph):
return {'CANCELLED'}
return {'FINISHED'}
def performSave(self, context, depsgraph):
pass
class SCENE_OT_depsgraph_relations_image(Operator,
SCENE_OT_depsgraph_image_common):
bl_idname = "scene.depsgraph_relations_image"
bl_label = "Depsgraph as Image"
bl_description = "Create new image datablock from the dependency graph"
def performSave(self, context, depsgraph):
import os
import subprocess
# Create temporary file.
dot_filepath = self._createTempFile(suffix=".dot")
# Save dependency graph to graphviz file.
depsgraph.debug_relations_graphviz(dot_filepath)
# Convert graphviz to PNG image.
png_filepath = os.path.join(bpy.app.tempdir, "depsgraph.png")
command = ("dot", "-Tpng", dot_filepath, "-o", png_filepath)
try:
subprocess.run(command)
self._openImageInEditor(context, png_filepath)
except:
self.report({'ERROR'}, "Error invoking dot command")
return False
finally:
# Remove graphviz file.
os.remove(dot_filepath)
return True
class SCENE_OT_depsgraph_stats_image(Operator,
SCENE_OT_depsgraph_image_common):
bl_idname = "scene.depsgraph_stats_image"
bl_label = "Depsgraph Stats as Image"
bl_description = "Create new image datablock from the dependency graph " + \
"execution statistics"
def performSave(self, context, depsgraph):
import os
import subprocess
# Create temporary file.
plot_filepath = self._createTempFile(suffix=".plot")
png_filepath = os.path.join(bpy.app.tempdir, "depsgraph_stats.png")
# Save dependency graph stats to gnuplot file.
depsgraph.debug_stats_gnuplot(plot_filepath, png_filepath)
# Convert graphviz to PNG image.
command = ("gnuplot", plot_filepath)
try:
subprocess.run(command)
self._openImageInEditor(context, png_filepath)
except:
self.report({'ERROR'}, "Error invoking gnuplot command")
return False
finally:
# Remove graphviz file.
os.remove(plot_filepath)
return True
class SCENE_OT_depsgraph_relations_svg(Operator,
SCENE_OT_depsgraph_image_common):
bl_idname = "scene.depsgraph_relations_svg"
bl_label = "Depsgraph as SVG in Browser"
bl_description = "Create an SVG image from the dependency graph and open it in the web browser"
def performSave(self, context, depsgraph):
import os
import subprocess
import webbrowser
# Create temporary file.
dot_filepath = self._createTempFile(suffix=".dot")
# Save dependency graph to graphviz file.
depsgraph.debug_relations_graphviz(dot_filepath)
# Convert graphviz to SVG image.
svg_filepath = os.path.join(bpy.app.tempdir, "depsgraph.svg")
command = ("dot", "-Tsvg", dot_filepath, "-o", svg_filepath)
try:
subprocess.run(command)
webbrowser.open_new_tab("file://" + os.path.abspath(svg_filepath))
except:
self.report({'ERROR'}, "Error invoking dot command")
return False
finally:
# Remove graphviz file.
os.remove(dot_filepath)
return True
###############################################################################
# Interface.
class SCENE_PT_depsgraph_common:
def draw(self, context):
layout = self.layout
col = layout.column()
# Everything related on relations and graph topology.
col.label(text="Relations:")
row = col.row()
row.operator("scene.depsgraph_relations_graphviz")
row.operator("scene.depsgraph_relations_image")
col.operator("scene.depsgraph_relations_svg")
# Everything related on evaluaiton statistics.
col.label(text="Statistics:")
row = col.row()
row.operator("scene.depsgraph_stats_gnuplot")
row.operator("scene.depsgraph_stats_image")
class SCENE_PT_depsgraph(bpy.types.Panel, SCENE_PT_depsgraph_common):
bl_label = "Dependency Graph"
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
bl_context = "scene"
bl_options = {'DEFAULT_CLOSED'}
@classmethod
def poll(cls, context):
if bpy.app.version >= (2, 80, 0,):
return False
depsgraph = _get_depsgraph(context)
return depsgraph is not None
class RENDERLAYER_PT_depsgraph(bpy.types.Panel, SCENE_PT_depsgraph_common):
bl_label = "Dependency Graph"
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
bl_context = "view_layer"
bl_options = {'DEFAULT_CLOSED'}
@classmethod
def poll(cls, context):
if bpy.app.version < (2, 80, 0,):
return False
depsgraph = _get_depsgraph(context)
return depsgraph is not None
def register():
bpy.utils.register_class(SCENE_OT_depsgraph_relations_graphviz)
bpy.utils.register_class(SCENE_OT_depsgraph_relations_image)
bpy.utils.register_class(SCENE_OT_depsgraph_relations_svg)
bpy.utils.register_class(SCENE_OT_depsgraph_stats_gnuplot)
bpy.utils.register_class(SCENE_OT_depsgraph_stats_image)
bpy.utils.register_class(SCENE_PT_depsgraph)
bpy.utils.register_class(RENDERLAYER_PT_depsgraph)
def unregister():
bpy.utils.unregister_class(SCENE_OT_depsgraph_relations_graphviz)
bpy.utils.unregister_class(SCENE_OT_depsgraph_relations_image)
bpy.utils.unregister_class(SCENE_OT_depsgraph_relations_svg)
bpy.utils.unregister_class(SCENE_OT_depsgraph_stats_gnuplot)
bpy.utils.unregister_class(SCENE_OT_depsgraph_stats_image)
bpy.utils.unregister_class(SCENE_PT_depsgraph)
bpy.utils.unregister_class(RENDERLAYER_PT_depsgraph)
if __name__ == "__main__":
register()