forked from atticus-lv/object_asset_wizard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_previews_ops.py
176 lines (136 loc) · 5.71 KB
/
render_previews_ops.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
# Copyright (C) 2019 h0bB1T
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
#
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import bpy, os
from bpy.types import Operator
from bpy.props import BoolProperty
from . preferences import PreferencesPanel
from . execute_blender import run_preview_render
from . preview_parsers import CollectionImageParser
from . utils import (CategoriesCache, categories, categories_enum, parse_entry_list, split_entry,
ASSET_TYPE_OBJECT, ASSET_TYPE_MATERIAL)
from . properties import Properties
running = False
class ModalTimerOperator(Operator):
"""
Used to track background rendering.
"""
bl_idname = "asset_wizard.modal_timer_op"
bl_label = "Modal Timer Operator"
timer = None
def modal(self, context, event):
if event.type == 'TIMER':
Properties.get_render_previews().poll()
return {'PASS_THROUGH'}
def execute(self, context):
global running
wm = context.window_manager
# Do it only once ...
if not running:
self.timer = wm.event_timer_add(0.1, window=context.window)
wm.modal_handler_add(self)
running = True
return {'RUNNING_MODAL'}
else:
return {'FINISHED'}
def cancel(self, context):
context.window_manager.event_timer_remove(self.timer)
class RenderPreviews:
def __init__(self):
# (asset_type, full path)
self.jobs = []
# subprocess.Popen
self.process = None
def poll(self):
"""
Check if render process is active. If completed, cleanup.
Start next if there's one on the pipe.
"""
if self.process:
# Job is active. Check if it has completed
if self.process.poll() != None:
# It has, reset.
self.process = None
# Refresh view (if preview currently selected).
if self.jobs[0][0] == ASSET_TYPE_OBJECT:
CategoriesCache.update_cache(ASSET_TYPE_OBJECT)
if self.jobs[0][1] == Properties.get().iobj_previews:
bpy.ops.asset_wizard.refresh_object_previews_op()
elif self.jobs[0][0] == ASSET_TYPE_MATERIAL:
CategoriesCache.update_cache(ASSET_TYPE_MATERIAL)
if self.jobs[0][1] == Properties.get().imat_previews:
bpy.ops.asset_wizard.refresh_material_previews_op()
self.jobs.pop(0)
# Force UI redraw (status display).
if bpy.context.area:
bpy.context.area.tag_redraw()
# If None, we can start a new one.
if not self.process and self.jobs:
self.process = run_preview_render(
self.jobs[0][0],
self.jobs[0][1],
PreferencesPanel.get().preview_engine
)
# Force UI redraw (status display).
if bpy.context.area:
bpy.context.area.tag_redraw()
def add_job(self, asset_type, filename):
"""
Add new job for preview rendering.
"""
# (Eventually) start modal timer.
bpy.ops.asset_wizard.modal_timer_op()
self.jobs.append((asset_type, filename))
self.poll()
def parse_render_list(self, root, asset_type, rerender):
"""
Adds all files that need to be preview rendered to job list.
"""
for category in categories_enum(asset_type):
for entry in parse_entry_list(asset_type, category[0]):
if rerender:
self.add_job(asset_type, entry)
else:
preview = split_entry(entry)[1]
if not os.path.exists(preview):
self.add_job(asset_type, entry)
def generate_render_list(self, rerender):
"""
Generate full render list in job list.
"""
prefs = PreferencesPanel.get()
for asset_type in ( ASSET_TYPE_OBJECT, ASSET_TYPE_MATERIAL ):
self.parse_render_list(prefs.root, asset_type, rerender)
def status(self):
if self.jobs:
return "Render queue (%i to render)::Current: %s" % (len(self.jobs), os.path.basename(self.jobs[0][1]))
else:
return None
class RenderPreviewsOperator(Operator):
bl_idname = "asset_wizard.render_previews_op"
bl_label = "Render"
bl_description = "Render missing previews"
def execute(self, context):
Properties.get_render_previews().generate_render_list(False)
return{'FINISHED'}
class RenderAllPreviewsOperator(Operator):
bl_idname = "asset_wizard.render_all_previews_op"
bl_label = "Render ALL"
bl_description = "Render ALL previews (can take a *LONG* time!)"
def execute(self, context):
Properties.get_render_previews().generate_render_list(True)
return{'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_confirm(self, event)