forked from BlenderKit/BlenderKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bg_blender.py
278 lines (236 loc) · 9.34 KB
/
bg_blender.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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# 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 2
# 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.
#
# ##### END GPL LICENSE BLOCK #####
from . import utils
import bpy
import sys, threading, os
import re
from bpy.props import (
EnumProperty,
)
bg_processes = []
class threadCom: # object passed to threads to read background process stdout info
''' Object to pass data between thread and '''
def __init__(self, eval_path_computing, eval_path_state, eval_path, process_type, proc, location=None, name=''):
# self.obname=ob.name
self.name = name
self.eval_path_computing = eval_path_computing # property that gets written to.
self.eval_path_state = eval_path_state # property that gets written to.
self.eval_path = eval_path # property that gets written to.
self.process_type = process_type
self.outtext = ''
self.proc = proc
self.lasttext = ''
self.message = '' # the message to be sent.
self.progress = 0.0
self.location = location
self.error = False
self.log = ''
def threadread(tcom):
'''reads stdout of background process.
this threads basically waits for a stdout line to come in,
fills the data, dies.'''
found = False
while not found:
if tcom.proc.poll() is not None:
#process terminated
return
inline = tcom.proc.stdout.readline()
# print('readthread', time.time())
inline = str(inline)
s = inline.find('progress{')
if s > -1:
e = inline.find('}')
tcom.outtext = inline[s + 9:e]
found = True
if tcom.outtext.find('%') > -1:
tcom.progress = float(re.findall('\d+\.\d+|\d+', tcom.outtext)[0])
return
if s == -1:
s = inline.find('Remaining')
if s > -1:
# e=inline.find('}')
tcom.outtext = inline[s: s + 18]
found = True
return
if len(inline) > 3:
print(inline, len(inline))
# if inline.find('Error'):
# tcom.error = True
# tcom.outtext = inline[2:]
def progress(text, n=None):
'''function for reporting during the script, works for background operations in the header.'''
# for i in range(n+1):
# sys.stdout.flush()
text = str(text)
if n is None:
n = ''
else:
n = ' ' + ' ' + str(int(n * 1000) / 1000) + '% '
spaces = ' ' * (len(text) + 55)
try:
sys.stdout.write('progress{%s%s}\n' % (text, n))
sys.stdout.flush()
except Exception as e:
print('background progress reporting race condition')
print(e)
# @bpy.app.handlers.persistent
def bg_update():
'''monitoring of background process'''
text = ''
#utils.p('timer search')
# utils.p('start bg_blender timer bg_update')
s = bpy.context.scene
global bg_processes
if len(bg_processes) == 0:
# utils.p('end bg_blender timer bg_update')
return 2
#cleanup dead processes first
remove_processes = []
for p in bg_processes:
if p[1].proc.poll() is not None:
remove_processes.append(p)
for p in remove_processes:
bg_processes.remove(p)
#Parse process output
for p in bg_processes:
# proc=p[1].proc
readthread = p[0]
tcom = p[1]
if not readthread.is_alive():
readthread.join()
# readthread.
estring = None
if tcom.error:
estring = tcom.eval_path_computing + ' = False'
tcom.lasttext = tcom.outtext
if tcom.outtext != '':
tcom.outtext = ''
text =tcom.lasttext.replace("'","")
estring = tcom.eval_path_state + ' = text'
# print(tcom.lasttext)
if 'finished successfully' in tcom.lasttext:
bg_processes.remove(p)
estring = tcom.eval_path_computing + ' = False'
else:
readthread = threading.Thread(target=threadread, args=([tcom]), daemon=True)
readthread.start()
p[0] = readthread
if estring:
try:
exec(estring)
except Exception as e:
print('Exception while reading from background process')
print(e)
# if len(bg_processes) == 0:
# bpy.app.timers.unregister(bg_update)
if len(bg_processes) > 0:
# utils.p('end bg_blender timer bg_update')
return .3
# utils.p('end bg_blender timer bg_update')
return 1.
process_types = (
('UPLOAD', 'Upload', ''),
('THUMBNAILER', 'Thumbnailer', ''),
)
process_sources = (
('MODEL', 'Model', 'set of objects'),
('SCENE', 'Scene', 'set of scenes'),
('HDR', 'HDR', 'HDR image'),
('MATERIAL', 'Material', 'any .blend Material'),
('TEXTURE', 'Texture', 'a texture, or texture set'),
('BRUSH', 'Brush', 'brush, can be any type of blender brush'),
)
class KillBgProcess(bpy.types.Operator):
'''Remove processes in background'''
bl_idname = "object.kill_bg_process"
bl_label = "Kill Background Process"
bl_options = {'REGISTER'}
process_type: EnumProperty(
name="Type",
items=process_types,
description="Type of process",
default="UPLOAD",
)
process_source: EnumProperty(
name="Source",
items=process_sources,
description="Source of process",
default="MODEL",
)
def execute(self, context):
s = bpy.context.scene
cls = bpy.ops.object.convert.__class__
# first do the easy stuff...TODO all cases.
props = utils.get_upload_props()
if self.process_type == 'UPLOAD':
props.uploading = False
if self.process_type == 'THUMBNAILER':
props.is_generating_thumbnail = False
global blenderkit_bg_process
# print('killing', self.process_source, self.process_type)
# then go kill the process. this wasn't working for unsetting props and that was the reason for changing to the method above.
processes = bg_processes
for p in processes:
tcom = p[1]
# print(tcom.process_type, self.process_type)
if tcom.process_type == self.process_type:
source = eval(tcom.eval_path)
kill = False
#TODO HDR - add killing of process
if source.bl_rna.name == 'Object' and self.process_source == 'MODEL':
if source.name == bpy.context.active_object.name:
kill = True
if source.bl_rna.name == 'Scene' and self.process_source == 'SCENE':
if source.name == bpy.context.scene.name:
kill = True
if source.bl_rna.name == 'Image' and self.process_source == 'HDR':
ui_props = bpy.context.window_manager.blenderkitUI
if source.name == ui_props.hdr_upload_image.name:
kill = False
if source.bl_rna.name == 'Material' and self.process_source == 'MATERIAL':
if source.name == bpy.context.active_object.active_material.name:
kill = True
if source.bl_rna.name == 'Brush' and self.process_source == 'BRUSH':
brush = utils.get_active_brush()
if brush is not None and source.name == brush.name:
kill = True
if kill:
estring = tcom.eval_path_computing + ' = False'
exec(estring)
processes.remove(p)
tcom.proc.kill()
return {'FINISHED'}
def add_bg_process(location=None, name=None, eval_path_computing='', eval_path_state='', eval_path='', process_type='',
process=None):
'''adds process for monitoring'''
global bg_processes
tcom = threadCom(eval_path_computing, eval_path_state, eval_path, process_type, process, location, name)
readthread = threading.Thread(target=threadread, args=([tcom]), daemon=True)
readthread.start()
bg_processes.append([readthread, tcom])
# if not bpy.app.timers.is_registered(bg_update):
# bpy.app.timers.register(bg_update, persistent=True)
def register():
bpy.utils.register_class(KillBgProcess)
user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
if user_preferences.use_timers and not bpy.app.background:
bpy.app.timers.register(bg_update)
def unregister():
bpy.utils.unregister_class(KillBgProcess)
if bpy.app.timers.is_registered(bg_update):
bpy.app.timers.unregister(bg_update)