-
Notifications
You must be signed in to change notification settings - Fork 1
/
bt_cyclesbake.py
524 lines (398 loc) · 19.4 KB
/
bt_cyclesbake.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# Copyright (C) Cogumelo Softworks
# License: http://www.gnu.org/licenses/gpl.html GPL version 3 or higher
import bpy
import random
import os, sys
import subprocess
import json
import io
import time
from . bt_utils import *
from . bt_autopack import *
from bpy_extras.image_utils import load_image
class BakeAtlas(bpy.types.Operator):
"""Tooltip"""
bl_idname = "textureoven.bakeatlas"
bl_label = "TextureOven: Bake Atlas"
job : bpy.props.StringProperty()
def execute(self, context):
Jobs = context.scene.TextureOven_Jobs.Jobs
for j in Jobs:
if(j.name == self.job):
Cycles_DoAtlasBake(j)
return {'FINISHED'}
class BakeIndividual(bpy.types.Operator):
"""Tooltip"""
bl_idname = "textureoven.bakeindividual"
bl_label = "TextureOven: Bake Individual"
job : bpy.props.StringProperty()
def execute(self, context):
Jobs = context.scene.TextureOven_Jobs.Jobs
for j in Jobs:
if(j.name == self.job):
Cycles_DoIndividualBake(j)
return {'FINISHED'}
def Cycles_DoAtlasBake(activeJob):
print("Cycles_DoAtlasBake")
if(not activeJob.enabled):
reports = LoadBakeReports()
jobList = reports["jobs"]
for Job in jobList:
if(Job["name"] == activeJob.name):
Job["fileList"] = ""
Job["status"] = "Baked"
SaveBakeReport(reports)
return
else:
settings = activeJob.job_settings
listObjects = GetObjectListJob(activeJob,settings,True)
listObjectsFrom = GetObjectListJob(activeJob,settings,False)
passList = GetActivePasses(activeJob)
reports = LoadBakeReports()
jobList = reports["jobs"]
outputFileList = []
# Para Cada Passo Ativo
for idx_pass,jobPass in enumerate(passList):
JobReport = None
for Job in jobList:
if(Job["name"] == activeJob.name):
JobReport = Job
reports["general"]["processCurrent"] = str(int(reports["general"]["processCurrent"])+1)
JobReport["passCount"] = str(len(passList))
JobReport["passCurrent"] = str(idx_pass + 1)
SaveBakeReport(reports)
#keepMaterials = []
keepMaterialOutput = []
AlreadyHandledMaterials = []
#Setup materials for object, that will be baked from
for idx_obj,obj in enumerate(listObjectsFrom):
currentObject = obj[0]
for mat in currentObject.data.materials:
if(mat in AlreadyHandledMaterials):
SetTextureOvenUV(obj)
continue
print("FIXING MAT----------------------------------")
print(mat)
AlreadyHandledMaterials.append(mat)
node_tree = mat.node_tree
for node in node_tree.nodes:
node.select = False
FixUVNodes(mat,obj)
keepMaterialOutput.append([node_tree,SetCustomMaterial(currentObject,mat,jobPass,settings)])
SetTextureOvenUV(obj)
# Para Cada Objeto
for idx_obj,obj in enumerate(listObjects):
reports = LoadBakeReports()
jobList = reports["jobs"]
JobReport = None
for Job in jobList:
if(Job["name"] == activeJob.name):
JobReport = Job
JobReport["objCurrent"] = str(len(listObjects))
JobReport["objCount"] = str(len(listObjects))
SaveBakeReport(reports)
currentObject = obj[0]
tempImage = CreateImage(activeJob,currentObject,jobPass,True)
#keepMaterials.append(ApplyExternalMaterial(currentObject,jobPass.type_simple));
# Para cada Material
for mat in currentObject.data.materials:
if(mat in AlreadyHandledMaterials):
SetTextureOvenUV(obj)
continue
print("FIXING MAT----------------------------------")
print(mat)
AlreadyHandledMaterials.append(mat)
# Desseleciona Todos os Nodes
node_tree = mat.node_tree
for node in node_tree.nodes:
node.select = False
FixUVNodes(mat,obj)
keepMaterialOutput.append([node_tree,SetCustomMaterial(currentObject,mat,jobPass,settings)]);
# Cria Image Nodes
image_node = node_tree.nodes.new('ShaderNodeTexImage')
image_node.location = (100,100)
image_node.image = tempImage
image_node.select = True
image_node.update()
# Torna Imagem Ativa
mat.node_tree.nodes.active = image_node
SetTextureOvenUV(obj)
#Configura o Render Settings
SetRenderSettings(jobPass,settings)
#Bake -------------------------------
# Se estiver utilizando target
if settings.target != "":
#Cria Lista de Objetos a serem usados como source
ListBake = []
for obj_name in activeJob.job_objs.coll:
for obj in bpy.context.scene.objects:
if obj_name.name == obj.name:
ListBake.append((obj,obj_name))
# Seleciona todos os objetos da lista e o target e o torna ativo
for obj in bpy.context.scene.objects:
obj.select_set(False)
for obj2 in ListBake:
if obj == obj2[0]:
obj.select_set(True)
for obj in listObjects:
obj[0].select_set(True)
bpy.context.view_layer.objects.active = obj[0]
else:
# Desseleciona todos os objetos da cena e seleciona Apenas os Objetos da Lista
bpy.ops.object.select_all(action="DESELECT")
for obj in listObjects:
obj[0].select_set(True)
bpy.context.view_layer.objects.active = obj[0]
# BAKE
if(settings.profile_type == "TEXTUREOVEN"):
jobPass.type = SetupTextureOvenPass(jobPass,settings)
# BAKE!
if settings.target == "":
bpy.ops.object.bake(type=jobPass.type,
use_clear=True,
use_selected_to_active=False,
normal_r = jobPass.normal_r,
normal_g = jobPass.normal_g,
normal_b = jobPass.normal_b,
normal_space = jobPass.normal_space,
margin = GetFixedMargin(jobPass))
else:
_cage = False
if settings.cage != "":
_cage = True
bpy.ops.object.bake(type=jobPass.type,
use_clear=True,
use_selected_to_active=True,
cage_extrusion=settings.distance,
normal_r = jobPass.normal_r,
normal_g = jobPass.normal_g,
normal_b = jobPass.normal_b,
cage_object = settings.cage,
normal_space = jobPass.normal_space,
margin = GetFixedMargin(jobPass),
use_cage = _cage)
RestoreCustomOutput(keepMaterialOutput)
#Save Image
path = SaveImage(activeJob,currentObject,jobPass,tempImage,True)
outputFileList.append(path)
# Refaz o output Node
print("-------------- REFAZ OUTPUT NODE ------------")
print(keepMaterialOutput[:])
reports = LoadBakeReports()
jobList = reports["jobs"]
for Job in jobList:
if(Job["name"] == activeJob.name):
Job["fileList"] = outputFileList
Job["status"] = "Baked"
SaveBakeReport(reports)
def Cycles_DoIndividualBake(activeJob):
if(not activeJob.enabled):
reports = LoadBakeReports()
jobList = reports["jobs"]
for Job in jobList:
if(Job["name"] == activeJob.name):
Job["fileList"] = ""
Job["status"] = "Baked"
SaveBakeReport(reports)
return
else:
settings = activeJob.job_settings
listObjects = GetObjectListJob(activeJob,settings,False)
passList = GetActivePasses(activeJob)
reports = LoadBakeReports()
jobList = reports["jobs"]
outputFileList = []
# Para cada objeto da Lista
for idx_obj,objData in enumerate(listObjects):
JobReport = None
for Job in jobList:
if(Job["name"] == activeJob.name):
JobReport = Job
JobReport["objCurrent"] = str(idx_obj + 1)
JobReport["objCount"] = str(len(listObjects))
JobReport["passCount"] = str(len(passList))
SaveBakeReport(reports)
# Para cada Passo Ativo
for idx_pass,jobPass in enumerate(passList):
JobReport["passCurrent"] = str(idx_pass + 1)
reports["general"]["processCurrent"] = str(int(reports["general"]["processCurrent"])+1)
SaveBakeReport(reports)
# Seleciona o Objeto para exibição
currentObject = objData[0]
bpy.ops.object.select_all(action="DESELECT")
currentObject.select_set(True)
bpy.context.view_layer.objects.active = currentObject
# Cria a imagem para dar bake
tempImage = CreateImage(activeJob,currentObject,jobPass,False)
# Materiais que precisam ser importados ao objeto
#keepMaterials = ApplyExternalMaterial(currentObject,jobPass.type_simple);
keepMaterialOutput = []
# Para cada Material
for mat in currentObject.data.materials:
# Desseleciona Todos os Nodes (TODO VER SE PRECISA POIS ELE JÁ SETA O IMAGEM COMO ATIVO )
node_tree = mat.node_tree
for node in node_tree.nodes:
node.select = False
FixUVNodes(mat,objData)
CreateImageNode(mat,tempImage)
# Seta a UV ativa
SetTextureOvenUV(objData)
#Configura o Render Settings
SetRenderSettings(jobPass,settings)
# Configura Custom Materials
keepMaterialOutput.append([node_tree,SetCustomMaterial(currentObject,mat,jobPass,settings)]);
# MODIFICADO AQUI, BAKE POR MATERIAL???
# BAKE
if(settings.profile_type == "TEXTUREOVEN"):
jobPass.type = SetupTextureOvenPass(jobPass,settings)
bpy.ops.object.bake(type=jobPass.type,
use_clear=True,
use_selected_to_active=False,
normal_r = jobPass.normal_r,
normal_g = jobPass.normal_g,
normal_b = jobPass.normal_b,
normal_space = jobPass.normal_space,
margin = GetFixedMargin(jobPass))
#RestoreMaterials(keepMaterials)
RestoreCustomOutput(keepMaterialOutput)
SaveBakeReport(reports)
path = SaveImage(activeJob,currentObject,jobPass,tempImage,False)
outputFileList.append(path)
reports = LoadBakeReports()
jobList = reports["jobs"]
for Job in jobList:
if(Job["name"] == activeJob.name):
Job["fileList"] = outputFileList
Job["status"] = "Baked"
SaveBakeReport(reports)
def BakeCycles(context):
print("-----------------------------START BAKE --------------------------------")
jobList = context.scene.TextureOven_Jobs.Jobs
jobList = [x for x in jobList if x.enabled == True]
status = CheckBake(context,jobList)
if status != True:
print(status)
return status
# Informa ao report que iniciou o processo de bake
context.scene.TextureOven_ReportData.is_baking = True;
MakeUVs(context,jobList)
# Salva a cena atual
bpy.ops.wm.save_mainfile()
# Reseta Variaveis de Report
bpy.context.scene.TextureOven_ReportData.objCount = 0
bpy.context.scene.TextureOven_ReportData.objCurrent = 0
bpy.context.scene.TextureOven_ReportData.passCount = 0
bpy.context.scene.TextureOven_ReportData.passCurrent = 0
bpy.context.scene.TextureOven_ReportData.processCurrent = 0
# TODO Calcular o ProgressCount baseado na cena
processCount = 0
bpy.context.scene.TextureOven_ReportData.jobCount = len(jobList)
for activeJob in jobList:
if(activeJob.job_settings.mode != "ATLAS"):
passList = GetActivePasses(activeJob)
settings = activeJob.job_settings
objList = GetObjectListJob(activeJob,settings,False)
processCount += (len(passList) * len(objList))
else:
passList = GetActivePasses(activeJob)
processCount += len(passList)
print("")
print(processCount);
print("")
bpy.context.scene.TextureOven_ReportData.processCount = processCount
# Inicializa o Json para essa sessão do Bake
script_file = os.path.realpath(__file__)
directory = os.path.dirname(script_file)
reports = {}
reports["general"] = { "jobsCount" : 0,
"jobsCurrent" : 0,
"processCurrent" : 0
}
reports["jobs"] = []
for activeJob in jobList:
activeJob = { "name" : activeJob.name,
"status" : "Waiting",
"objCount" : 0,
"objCurrent" : 0,
"passCount" : 0,
"passCurrent" : 0
}
reports["jobs"].append(activeJob)
with open(directory + "/report.json", 'w') as file:
json.dump(reports, file, indent=2)
# Carrega a Interface
# bpy.ops.textureoven.report()
# Inicializa a Verificação de Progresso
print("-----------------------------PROCESS JOB --------------------------------")
bpy.app.timers.register(ProcessBakeJobProgress)
return status
def ProcessBakeJobProgress():
reports = LoadBakeReports()
jobList = reports["jobs"]
bpy.context.scene.TextureOven_ReportData.processCurrent = int(reports["general"]["processCurrent"])
isProcessing = False
for idx,job in enumerate(jobList):
if(job["status"] == "Baking" or job["status"] == "Waiting"):
isProcessing = True
if(job["status"] == "Baking"):
bpy.context.scene.TextureOven_ReportData.objCount = int(job["objCount"])
bpy.context.scene.TextureOven_ReportData.objCurrent = int(job["objCurrent"])
bpy.context.scene.TextureOven_ReportData.passCount = int(job["passCount"])
bpy.context.scene.TextureOven_ReportData.passCurrent = int(job["passCurrent"])
if(job["status"] == "Baked"):
#activeJob = bpy.context.scene.TextureOven_Jobs.Jobs[idx]
activeJob = bpy.context.scene.TextureOven_Jobs.Jobs[job["name"]]
job["status"] = "Finished"
SaveBakeReport(reports)
PostBakeProcess(job,activeJob)
# Se estiver aguardando ser chamado para o bake e finalizou ou ultimo ou é o primeiro então começa o bake desse passo
if((job["status"] == "Waiting" and idx == 0) or (idx>0 and job["status"] == "Waiting" and jobList[idx-1]["status"] == "Finished" )):
# Informa no status que iniciou o bake
job["status"] = "Baking"
SaveBakeReport(reports)
#activeJob = bpy.context.scene.TextureOven_Jobs.Jobs[idx]
activeJob = bpy.context.scene.TextureOven_Jobs.Jobs[job["name"]]
filepath = bpy.data.filepath
process = None
if(activeJob.job_settings.mode != "ATLAS"):
print("------------------------ BAKE INDIVIDUAL -----------------------------")
process = subprocess.Popen([ bpy.app.binary_path,
"--background",
filepath,
"--python-expr",
'import bpy;p=bpy.ops.textureoven.bakeindividual(job ="' + activeJob.name + '");'],
shell=False)
else:
print("------------------------ BAKE ATLAS -----------------------------")
process = subprocess.Popen([ bpy.app.binary_path,
"--background",
filepath,
"--python-expr",
'import bpy;p=bpy.ops.textureoven.bakeatlas(job ="' + activeJob.name + '");'],
shell=False)
bpy.context.scene.TextureOven_ReportData.current_processPid = int(process.pid)
bpy.context.scene.TextureOven_ReportData.jobCurrent = int(idx) + 1
if(not isProcessing):
FinishBake()
RedrawScreen()
return 0.1
def RedrawScreen():
""" run tag_redraw for given area types """
screens = [bpy.context.screen] if bpy.context.screen else bpy.data.screens
for screen in screens:
for area in screen.areas:
if(area.type == "VIEW_3D"):
area.tag_redraw()
def FinishBake():
bpy.app.timers.unregister(ProcessBakeJobProgress)
bpy.context.scene.TextureOven_ReportData.is_baking = False
def PostBakeProcess(reportJob,activeJob):
print("----------------- APPLY POST PROCESS----------------------")
# Import Images
if(activeJob.job_settings.postbake_importImages):
for image in reportJob["fileList"]:
img = load_image(image,check_existing=True,force_reload=True)
img.name = os.path.splitext(os.path.basename(image))[0]
if(activeJob.job_settings.postbake_createEevee and activeJob.job_settings.target == ""):
if(activeJob.job_settings.mode != "ATLAS"):
CreateIndividualEveeScene(activeJob)