-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathEasyBake.py
804 lines (582 loc) · 32 KB
/
EasyBake.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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
#this script is dedicated to the public domain under CC0 (https://creativecommons.org/publicdomain/zero/1.0/)
#do whatever you want with it! -Bram
bl_info = {
"name": "EasyBake",
"category": "3D View",
"blender": (2, 80, 0),
"author": "Bram Eulaers",
"description": "Simple texture baking UI for fast iteration. Can be found in the 3D View Sidebar under 'bake'."
}
import bpy
import os
import bmesh
from bpy.props import EnumProperty, BoolProperty, StringProperty, FloatProperty, IntProperty
def unhide(objectType):
if objectType is None:
for o in objectType.objects:
o.hide_viewport = False
else:
objectType.hide_viewport = False
def hide(objectType):
if objectType is None:
for o in objectType.objects:
o.hide_viewport = True
else:
objectType.hide_viewport = True
class EasyBakeUIPanel(bpy.types.Panel):
"""EasyBakeUIPanel Panel"""
bl_label = "EasyBake"
bl_space_type = 'VIEW_3D'
bl_region_type = "UI"
bl_category = "EasyBake"
def draw_header(self, _):
layout = self.layout
layout.label(text="", icon='SCENE')
def draw(self, context):
layout = self.layout
box = layout.box()
col = box.column(align=True)
row = col.row(align = True)
row.prop(context.scene, "lowpolyGroup", text="", icon="GROUP")
if context.scene.lowpolyGroup is True:
row.prop_search(context.scene, "lowpoly", bpy.data, "collections", text="", icon="MESH_ICOSPHERE")
if context.scene.lowpolyGroup is False:
row.prop_search(context.scene, "lowpoly", context.scene, "objects", text="", icon="MESH_ICOSPHERE")
if context.scene.lowpolyActive is True:
hideicon = "HIDE_OFF"
if context.scene.lowpolyActive is False:
hideicon = "HIDE_ON"
op = row.operator("brm.bakeuihide", text="", icon=hideicon)
op.targetmesh = "lowpoly"
row = col.row(align = True)
row.prop(context.scene, "hipolyGroup", text="", icon="GROUP")
if context.scene.hipolyGroup is True:
row.prop_search(context.scene, "hipoly", bpy.data, "collections", text="", icon="MESH_UVSPHERE")
if context.scene.hipolyGroup is False:
row.prop_search(context.scene, "hipoly", context.scene, "objects", text="", icon="MESH_UVSPHERE")
row.enabled = not context.scene.UseLowOnly
if context.scene.hipolyActive is True:
hideicon = "HIDE_OFF"
if context.scene.hipolyActive is False:
hideicon = "HIDE_ON"
op = row.operator("brm.bakeuihide", text="", icon=hideicon)
op.targetmesh = "hipoly"
col = box.column(align=True)
row = col.row(align = True)
row.operator("brm.bakeuitoggle", text="Toggle hi/low", icon="FILE_REFRESH")
#row.prop(context.scene, "UseBlenderGame", icon="MESH_UVSPHERE", text="")
col = layout.column(align=True)
col.separator()
row = col.row(align = True)
row.prop(context.scene.render.bake, "cage_extrusion", text="Ray Distance")
#row.prop(context.scene, "cageEnabled", icon="OBJECT_DATAMODE", text="")
row = col.row(align = True)
#row.enabled = context.scene.cageEnabled
#if context.scene.cageEnabled:
# op = row.prop_search(context.scene, "cage", bpy.data, "objects", text="", icon="MESH_UVSPHERE")
#op.enabled = context.scene.cageEnabled
col.separator()
box = layout.box()
col = box.column(align=True)
row = col.row(align = True)
row.label(text="Width:")
row.operator("brm.bakeuiincrement", text="", icon="REMOVE").target = "width/2"
row.prop(context.scene, "bakeWidth", text="")
row.operator("brm.bakeuiincrement", text="", icon="ADD").target = "width*2"
row = col.row(align = True)
row.label(text="Height:")
row.operator("brm.bakeuiincrement", text="", icon="REMOVE").target = "height/2"
row.prop(context.scene, "bakeHeight", text="")
row.operator("brm.bakeuiincrement", text="", icon="ADD").target = "height*2"
row = col.row(align = True)
row.label(text="Padding:")
row.prop(context.scene.render.bake, "margin", text="")
col = layout.column(align=True)
col.separator()
col.prop(context.scene, 'bakeFolder', text="")
row = col.row(align = True)
row.label(text="Filename:")
row.prop(context.scene, "bakePrefix", text="")
col.separator()
box = layout.box()
col = box.column(align=True)
row = col.row(align = True)
#row.enabled = not context.scene.UseLowOnly
if not context.scene.bakeNormal:
row.prop(context.scene, "bakeNormal", icon="SHADING_RENDERED", text="Tangent Normal")
if context.scene.bakeNormal:
row.prop(context.scene, "bakeNormal", icon="SHADING_RENDERED", text=" ")
row.prop(context.scene, "affixNormal", text="")
row.prop(context.scene, "samplesNormal", text="")
row = col.row(align = True)
#row.enabled = not context.scene.UseLowOnly
if not context.scene.bakeObject:
row.prop(context.scene, "bakeObject", icon="SHADING_RENDERED", text="Object Normal")
if context.scene.bakeObject:
row.prop(context.scene, "bakeObject", icon="SHADING_RENDERED", text=" ")
row.prop(context.scene, "affixObject", text="")
row.prop(context.scene, "samplesObject", text="")
row = col.row(align = True)
if not context.scene.bakeAO:
row.prop(context.scene, "bakeAO", icon="SHADING_SOLID", text="Occlusion")
if context.scene.bakeAO:
row.prop(context.scene, "bakeAO", icon="SHADING_SOLID", text=" ")
row.prop(context.scene, "affixAO", text="")
row.prop(context.scene, "samplesAO", text="")
row = col.row(align = True)
#row.enabled = not context.scene.UseLowOnly
if not context.scene.bakeColor:
row.prop(context.scene, "bakeColor", icon="SHADING_TEXTURE", text="Color")
if context.scene.bakeColor:
row.prop(context.scene, "bakeColor", icon="SHADING_TEXTURE", text=" ")
row.prop(context.scene, "affixColor", text="")
row.prop(context.scene, "samplesColor", text="")
row = col.row(align = True)
#row.enabled = not context.scene.UseLowOnly
if not context.scene.bakeRoughness:
row.prop(context.scene, "bakeRoughness", icon="SHADING_TEXTURE", text="Roughness")
if context.scene.bakeRoughness:
row.prop(context.scene, "bakeRoughness", icon="SHADING_TEXTURE", text=" ")
row.prop(context.scene, "affixRoughness", text="")
row.prop(context.scene, "samplesRoughness", text="")
row = col.row(align = True)
#row.enabled = not context.scene.UseLowOnly
if not context.scene.bakeEmission:
row.prop(context.scene, "bakeEmission", icon="SHADING_TEXTURE", text="Emission")
if context.scene.bakeEmission:
row.prop(context.scene, "bakeEmission", icon="SHADING_TEXTURE", text=" ")
row.prop(context.scene, "affixEmission", text="")
row.prop(context.scene, "samplesEmission", text="")
row.prop(context.scene, "bakeEmissionLinear", icon="NODE_TEXTURE", text="")
row = col.row(align = True)
if not context.scene.bakeUV:
row.prop(context.scene, "bakeUV", icon="SHADING_WIRE", text="UV Snapshot")
if context.scene.bakeUV:
row.prop(context.scene, "bakeUV", icon="SHADING_WIRE", text=" ")
row.prop(context.scene, "affixUV", text="")
row.prop(context.scene, "bakeUV", icon="BLANK1", text=" ")
col = layout.column(align=True)
col.separator()
row = col.row(align = True)
op = row.operator("brm.bake", text="BAKE", icon="RENDER_RESULT")
row.prop(context.scene, "UseLowOnly", icon="MESH_ICOSPHERE", text="")
class EasyBakeUIToggle(bpy.types.Operator):
"""toggle lowpoly/hipoly"""
bl_idname = "brm.bakeuitoggle"
bl_label = "Toggle"
bl_options = {"UNDO"}
def execute(self, context):
if bpy.context.object.mode == 'EDIT':
bpy.ops.object.mode_set(mode='OBJECT')
#test lowpoly/hipoly exists
if context.scene.lowpoly is None and not context.scene.lowpoly in bpy.data.collections:
self.report({'WARNING'}, "Select a valid lowpoly object or group!")
return {'FINISHED'}
if context.scene.hipoly is None and not context.scene.hipoly in bpy.data.collections:
self.report({'WARNING'}, "Select a valid hipoly object or group!")
return {'FINISHED'}
if context.scene.lowpolyActive is True:
context.scene.lowpolyActive = False
hide(context.scene.lowpoly)
context.scene.hipolyActive = True
unhide(context.scene.hipoly)
else:
context.scene.lowpolyActive = True
unhide(context.scene.lowpoly)
context.scene.hipolyActive = False
hide(context.scene.hipoly)
return {'FINISHED'}
class EasyBakeUIIncrement(bpy.types.Operator):
"""multiply/divide value"""
bl_idname = "brm.bakeuiincrement"
bl_label = "increment"
target : bpy.props.StringProperty()
def execute(self, context):
if self.target == "width/2" and context.scene.bakeWidth > 4:
context.scene.bakeWidth = context.scene.bakeWidth // 2
if self.target == "width*2":
context.scene.bakeWidth = context.scene.bakeWidth * 2
if self.target == "height/2" and context.scene.bakeHeight > 4:
context.scene.bakeHeight = context.scene.bakeHeight // 2
if self.target == "height*2":
context.scene.bakeHeight = context.scene.bakeHeight * 2
return {'FINISHED'}
class EasyBakeUIHide(bpy.types.Operator):
"""hide object"""
bl_idname = "brm.bakeuihide"
bl_label = "hide"
bl_options = {"UNDO"}
targetmesh : bpy.props.StringProperty()
def execute(self, context):
#test if collection:
if context.scene.hipoly.bl_rna.name == "Collection":
print("i am a collection!")
if context.scene.hipoly.bl_rna.name == "Object":
print("i am an object!")
#print(context.scene.hipoly)
#print(context.scene.lowpoly)
#test lowpoly/hipoly exists
if bpy.context.object.mode == 'EDIT':
bpy.ops.object.mode_set(mode='OBJECT')
if self.targetmesh == "lowpoly":
if context.scene.lowpoly is None and not context.scene.lowpoly in bpy.data.collections:
self.report({'WARNING'}, "Select a valid lowpoly object or collection!")
return {'FINISHED'}
else:
if context.scene.lowpolyActive is True:
context.scene.lowpolyActive = False
hide(context.scene.lowpoly)
else:
context.scene.lowpolyActive = True
unhide(context.scene.lowpoly)
if self.targetmesh == "hipoly":
if context.scene.hipoly is None and not context.scene.hipoly in bpy.data.collections:
self.report({'WARNING'}, "Select a valid hipoly object or collection!")
return {'FINISHED'}
else:
if context.scene.hipolyActive is True:
context.scene.hipolyActive = False
hide(context.scene.hipoly)
else:
context.scene.hipolyActive = True
unhide(context.scene.hipoly)
return {'FINISHED'}
class EasyBake(bpy.types.Operator):
"""Bake and save textures"""
bl_idname = "brm.bake"
bl_label = "set normal"
bl_options = {"UNDO"}
def execute(self, context):
#test if everything is set up OK first:
#test folder
hasfolder = os.access(context.scene.bakeFolder, os.W_OK)
if hasfolder is False:
self.report({'WARNING'}, "Select a valid export folder!")
return {'FINISHED'}
#test lowpoly/hipoly/cage exists
if context.scene.lowpoly is None and not context.scene.lowpoly in bpy.data.collections:
self.report({'WARNING'}, "Select a valid lowpoly object or collection!")
return {'FINISHED'}
if context.scene.hipoly is None and not context.scene.hipoly in bpy.data.collections and not context.scene.UseLowOnly:
self.report({'WARNING'}, "Select a valid hipoly object or collection!")
return {'FINISHED'}
#if bpy.data.objects.get(context.scene.cage) is None and context.scene.cageEnabled:
# self.report({'WARNING'}, "Select a valid cage object!")
# return {'FINISHED'}
#test if lowpoly, highpoly and cage objects are actually models
lowpolymeshes = 0
if context.scene.lowpolyGroup == True:
for o in bpy.context.scene.lowpoly.all_objects:
if o.type == 'MESH':
lowpolymeshes+=1
else:
if context.scene.lowpoly.type == 'MESH':
lowpolymeshes = 1
if lowpolymeshes == 0:
self.report({'WARNING'}, "lowpoly needs to have a mesh!")
return {'FINISHED'}
if not context.scene.UseLowOnly:
hipolymeshes = 0
if context.scene.hipolyGroup == True:
for o in bpy.context.scene.hipoly.all_objects:
if o.type == 'MESH':
hipolymeshes+=1
else:
if context.scene.hipoly.type == 'MESH':
hipolymeshes = 1
if hipolymeshes == 0:
self.report({'WARNING'}, "hipoly needs to have a mesh!")
return {'FINISHED'}
if context.scene.cageEnabled and bpy.data.objects[context.scene.cage].type != 'MESH':
self.report({'WARNING'}, "cage needs to be a mesh!")
return {'FINISHED'}
#setup
#HOTFIX get out of local view
if context.space_data.local_view:
bpy.ops.view3d.localview()
#1 unhide everything to be baked
if not context.scene.UseLowOnly:
unhide(context.scene.hipoly)
unhide(context.scene.lowpoly)
bpy.ops.object.hide_view_clear() #temporary until I figure out how hiding is actually handled
#2 make sure we are in object mode and nothing is selected
if bpy.context.object.mode == 'EDIT':
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
#3 setup lowpoly for baking
lowpolyobject = "null"
orig_lowpoly = None
#print("still working")
#return {'FINISHED'}
#if collection, create temporary lowpoly object
if context.scene.lowpolyGroup == True:
#print("still working")
#return {'FINISHED'}
context.scene.lowpoly.hide_render = False
#collections crash fix
low_objects_names = [obj.name for obj in bpy.context.scene.lowpoly.all_objects]
for o in low_objects_names:
#for o in bpy.context.scene.lowpoly.all_objects:
#if context.scene.lowpoly is None:
# context.scene.lowpoly.hide_render = False
# for o in context.scene.lowpoly.objects:
if bpy.data.objects[o].type == 'MESH':
#print("its a mesh")
#return {'FINISHED'}
bpy.data.objects[o].hide_viewport = False
bpy.data.objects[o].select_set(state=True)
context.view_layer.objects.active = bpy.data.objects[o]
bpy.data.objects[o].hide_render = True
#print(bpy.context.scene.lowpoly.all_objects)
#print("still working")
#return {'FINISHED'}
#duplicate selected and combine into new object
bpy.ops.object.duplicate()
bpy.ops.object.join()
bpy.ops.object.transform_apply(location=False, rotation=True, scale=True)
lowpolyobject = bpy.context.selected_objects[0].name
bpy.data.objects[lowpolyobject].hide_render = False
else:
bpy.ops.object.select_all(action='DESELECT')
#context.scene.lowpoly.select_set(state=True)
context.scene.lowpoly.hide_viewport = False
context.scene.lowpoly.hide_render = False
context.scene.lowpoly.select_set(state=True)
orig_lowpoly = context.scene.lowpoly
lowpolyobject = context.scene.lowpoly
#print("still working")
#return {'FINISHED'}
# if bake to self, create a temporary lowpoly duplicate to bake self to:
if context.scene.UseLowOnly:
bpy.ops.object.duplicate()
bpy.context.active_object.name = "temp_hipoly"
context.scene.hipoly = bpy.context.active_object
#return {'FINISHED'}
# test if cage has same tri count:
if context.scene.cageEnabled:
vcount_low = len(bpy.data.objects[lowpolyobject].data.vertices)
vcount_cage = len(bpy.data.objects[context.scene.cage].data.vertices)
if vcount_low != vcount_cage:
if context.scene.lowpolyGroup:
bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects[lowpolyobject].select_set(state=True)
bpy.ops.object.delete(use_global=False)
self.report({'WARNING'}, "cage and low poly vertex count don't match!")
return {'FINISHED'}
#4 test if lowpoly has a material and UV
#if len(context.scene.lowpoly.data.materials) == 0:
# if context.scene.lowpolyGroup:
# bpy.ops.object.select_all(action='DESELECT')
# bpy.data.objects[lowpolyobject].select_set(state=True)
# bpy.ops.object.delete(use_global=False)
# self.report({'WARNING'}, "Material required on low poly mesh!")
# return {'FINISHED'}
#if len(context.scene.lowpoly.data.uv_layers) == 0:
# if context.scene.lowpolyGroup:
# bpy.ops.object.select_all(action='DESELECT')
# bpy.data.objects[lowpolyobject].select_set(state=True)
# bpy.ops.object.delete(use_global=False)
# self.report({'WARNING'}, "low poly mesh has no UV!")
# return {'FINISHED'}
#5 remember render engine and switch to CYCLES for baking
orig_renderer = bpy.data.scenes[bpy.context.scene.name].render.engine
bpy.data.scenes[bpy.context.scene.name].render.engine = "CYCLES"
#6 create temporary bake image and material
bakeimage = bpy.data.images.new("BakeImage", width=context.scene.bakeWidth, height=context.scene.bakeHeight)
bakemat = bpy.data.materials.new(name="bakemat")
bakemat.use_nodes = True
#7 select hipoly target
#if not context.scene.UseLowOnly:
#select hipoly object or collection:
if context.scene.hipoly.bl_rna.name == "Collection":
context.scene.hipoly.hide_render = False
for o in bpy.context.scene.hipoly.all_objects:
if o.type == 'MESH':
o.hide_viewport = False
o.hide_render = False
o.select_set(state=True)
else:
context.scene.hipoly.hide_viewport = False
context.scene.hipoly.hide_render = False
context.scene.hipoly.select_set(state=True)
#8 select lowpoly target
print("whats happening here?")
print(context.scene.lowpoly)
print(lowpolyobject)
#bpy.context.view_layer.objects.active = bpy.data.objects[lowpolyobject]
#print(bpy.data.objects[lowpolyobject])
if context.scene.lowpolyGroup == True:
bpy.context.view_layer.objects.active = bpy.data.objects[lowpolyobject]
else:
bpy.context.view_layer.objects.active = lowpolyobject
#9 select lowpoly material and create temporary render target
orig_mat = bpy.context.active_object.data.materials[0]
bpy.context.active_object.data.materials[0] = bakemat
node_tree = bakemat.node_tree
node = node_tree.nodes.new("ShaderNodeTexImage")
node.select = True
node_tree.nodes.active = node
node.image = bakeimage
#10 check if theres a cage to be used
if context.scene.cageEnabled:
bpy.context.scene.render.bake.use_cage = True
bpy.context.scene.render.bake.cage_object = bpy.data.objects[context.scene.cage]
else:
bpy.context.scene.render.bake.use_cage = False
#11 bake all maps!
if context.scene.bakeNormal:
bpy.context.scene.cycles.samples = context.scene.samplesNormal
bpy.ops.object.bake(type='NORMAL', use_clear=True, use_selected_to_active=True, normal_space='TANGENT')
bakeimage.filepath_raw = context.scene.bakeFolder+context.scene.bakePrefix+context.scene.affixNormal+".tga"
bakeimage.file_format = 'TARGA'
bakeimage.save()
if context.scene.bakeObject:
bpy.context.scene.cycles.samples = context.scene.samplesObject
bpy.ops.object.bake(type='NORMAL', use_clear=True, use_selected_to_active=True, normal_space='OBJECT')
bakeimage.filepath_raw = context.scene.bakeFolder+context.scene.bakePrefix+context.scene.affixObject+".tga"
bakeimage.file_format = 'TARGA'
bakeimage.save()
if context.scene.bakeAO:
bpy.context.scene.cycles.samples = context.scene.samplesAO
bpy.ops.object.bake(type='AO', use_clear=True, use_selected_to_active=True)
bakeimage.filepath_raw = context.scene.bakeFolder+context.scene.bakePrefix+context.scene.affixAO+".tga"
bakeimage.file_format = 'TARGA'
bakeimage.save()
if context.scene.bakeColor:
bpy.context.scene.cycles.samples = context.scene.samplesColor
bpy.context.scene.render.bake.use_pass_direct = False
bpy.context.scene.render.bake.use_pass_indirect = False
bpy.context.scene.render.bake.use_pass_color = True
bpy.ops.object.bake(type='DIFFUSE', use_clear=True, use_selected_to_active=True)
bakeimage.filepath_raw = context.scene.bakeFolder+context.scene.bakePrefix+context.scene.affixColor+".tga"
bakeimage.file_format = 'TARGA'
bakeimage.save()
if context.scene.bakeRoughness:
bpy.context.scene.cycles.samples = context.scene.samplesRoughness
bpy.ops.object.bake(type='ROUGHNESS', use_clear=True, use_selected_to_active=True)
bakeimage.filepath_raw = context.scene.bakeFolder+context.scene.bakePrefix+context.scene.affixRoughness+".tga"
bakeimage.file_format = 'TARGA'
bakeimage.save()
if context.scene.bakeEmission:
bpy.context.scene.cycles.samples = context.scene.samplesEmission
print("colorspace")
print(bakeimage.colorspace_settings.name)
if context.scene.bakeEmissionLinear:
bakeimage.colorspace_settings.name="Linear"
bpy.ops.object.bake(type='EMIT', use_clear=True, use_selected_to_active=True)
bakeimage.filepath_raw = context.scene.bakeFolder+context.scene.bakePrefix+context.scene.affixEmission+".tga"
bakeimage.file_format = 'TARGA'
bakeimage.save()
#UV SNAPSHOT
if context.scene.bakeUV:
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.object.editmode_toggle()
original_type = bpy.context.area.type
bpy.context.area.type = "IMAGE_EDITOR"
uvfilepath = context.scene.bakeFolder+context.scene.bakePrefix+context.scene.affixUV+".png"
bpy.ops.uv.export_layout(filepath=uvfilepath, size=(context.scene.bakeWidth, context.scene.bakeHeight))
bpy.context.area.type = original_type
#cleanup temporary objects and materials
bpy.ops.object.select_all(action='DESELECT')
if not context.scene.lowpolyGroup:
orig_lowpoly.select_set(state=True)
bpy.data.images.remove(bakeimage)
bakemat.node_tree.nodes.remove(node)
bpy.data.materials.remove(bakemat)
bpy.context.active_object.data.materials[0] = orig_mat
bpy.data.scenes[bpy.context.scene.name].render.engine = orig_renderer
if context.scene.lowpolyGroup:
bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects[lowpolyobject].select_set(state=True)
bpy.ops.object.delete(use_global=False)
if context.scene.UseLowOnly:
#bpy.ops.object.select_all(action='DESELECT')
context.scene.hipoly.select_set(state=True)
#return {'FINISHED'}
bpy.ops.object.delete(use_global=False)
#return {'FINISHED'}
#reload all textures
for image in bpy.data.images:
image.reload()
#rehide back to original state
if context.scene.lowpolyActive is True:
if context.scene.lowpoly is None:
for o in context.scene.lowpoly.objects:
o.hide_viewport = False
context.view_layer.objects.active = o
else:
context.scene.lowpoly.hide_viewport = False
#context.view_layer.objects.active = context.scene.lowpoly
else:
if context.scene.lowpoly is None:
for o in context.scene.lowpoly.objects:
o.hide_viewport = True
else:
context.scene.lowpoly.hide_viewport = True
if not context.scene.UseLowOnly:
if context.scene.hipolyActive is True:
if context.scene.hipoly is None:
for o in context.scene.hipoly.objects:
o.hide_viewport = False
context.view_layer.objects.active = o
else:
context.scene.hipoly.hide_viewport = False
context.view_layer.objects.active =context.scene.hipoly
else:
if context.scene.hipoly is None:
for o in context.scene.hipoly.objects:
o.hide_viewport = True
else:
context.scene.hipoly.hide_viewport = True
return {'FINISHED'}
classes = (
EasyBake,
EasyBakeUIHide,
EasyBakeUIPanel,
EasyBakeUIToggle,
EasyBakeUIIncrement,
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.lowpoly = bpy.props.PointerProperty (name = "lowpoly", type=bpy.types.Object, description = "lowpoly object")
bpy.types.Scene.lowpolyActive = bpy.props.BoolProperty (name = "lowpolyActive", default = True, description = "lowpolyActive")
bpy.types.Scene.lowpolyGroup = bpy.props.BoolProperty (name = "lowpolyGroup",default = False,description = "enable lowpoly collection")
bpy.types.Scene.hipoly = bpy.props.PointerProperty (name = "hipoly",type=bpy.types.Object,description = "hipoly object or group")
bpy.types.Scene.hipolyActive = bpy.props.BoolProperty (name = "hipolyActive",default = True,description = "hipolyActive")
bpy.types.Scene.hipolyGroup = bpy.props.BoolProperty (name = "hipolyGroup",default = False,description = "enable hipoly collection")
bpy.types.Scene.cage = bpy.props.StringProperty (name = "cage",default = "cage",description = "cage object")
bpy.types.Scene.cageActive = bpy.props.BoolProperty (name = "cageActive",default = True,description = "cageActive")
bpy.types.Scene.cageEnabled = bpy.props.BoolProperty (name = "cageEnabled",default = False,description = "Enable cage object for baking")
bpy.types.Scene.bakeNormal = bpy.props.BoolProperty (name = "bakeNormal",default = False,description = "Bake Tangent Space Normal Map")
bpy.types.Scene.bakeObject = bpy.props.BoolProperty (name = "bakeObject",default = False,description = "Bake Object Space Normal Map")
bpy.types.Scene.bakeAO = bpy.props.BoolProperty (name = "bakeAO",default = False,description = "Bake Ambient Occlusion Map")
bpy.types.Scene.bakeColor = bpy.props.BoolProperty (name = "bakeColor",default = False,description = "Bake Albedo Color Map")
bpy.types.Scene.bakeRoughness = bpy.props.BoolProperty (name = "bakeRoughness",default = False,description = "Bake Roughness Map")
bpy.types.Scene.bakeEmission = bpy.props.BoolProperty (name = "bakeEmission",default = False,description = "Bake Emission Map")
bpy.types.Scene.bakeEmissionLinear = bpy.props.BoolProperty (name = "bakeEmissionLinear",default = False,description = "Use Linear")
bpy.types.Scene.bakeUV = bpy.props.BoolProperty (name = "bakeUV",default = False,description = "Bake UV Wireframe Snapshot of Lowpoly Mesh")
bpy.types.Scene.samplesNormal = bpy.props.IntProperty (name = "samplesNormal",default = 8,description = "Tangent Space Normal Map Sample Count")
bpy.types.Scene.samplesObject = bpy.props.IntProperty (name = "samplesObject",default = 8,description = "Object Space Normal Map Sample Count")
bpy.types.Scene.samplesAO = bpy.props.IntProperty (name = "samplesAO",default = 128,description = "Ambient Occlusion Map Sample Count")
bpy.types.Scene.samplesColor = bpy.props.IntProperty (name = "samplesColor",default = 1,description = "Color Map Sample Count")
bpy.types.Scene.samplesEmission = bpy.props.IntProperty (name = "samplesEmission",default = 1,description = "Emission Map Sample Count")
bpy.types.Scene.samplesRoughness = bpy.props.IntProperty (name = "samplesRoughness",default = 1,description = "Roughness Map Sample Count")
bpy.types.Scene.bakeWidth = bpy.props.IntProperty (name = "bakeWidth",default = 512,description = "Export Texture Width")
bpy.types.Scene.bakeHeight = bpy.props.IntProperty (name = "bakeHeight",default = 512,description = "Export Texture Height")
bpy.types.Scene.bakePrefix = bpy.props.StringProperty (name = "bakePrefix",default = "export",description = "export filename")
bpy.types.Scene.bakeFolder = bpy.props.StringProperty (name = "bakeFolder",default = "C:\\export\\",description = "destination folder",subtype = 'DIR_PATH')
bpy.types.Scene.UseLowOnly = bpy.props.BoolProperty (name = "UseLowOnly",default = False,description = "Only bake lowpoly on itself")
bpy.types.Scene.affixNormal = bpy.props.StringProperty (name = "affixNormal",default = "_normal",description = "normal map affix")
bpy.types.Scene.affixObject = bpy.props.StringProperty (name = "affixObject",default = "_object",description = "object normal map affix")
bpy.types.Scene.affixAO = bpy.props.StringProperty (name = "affixAO",default = "_ao",description = "AO map affix")
bpy.types.Scene.affixColor = bpy.props.StringProperty (name = "affixColor",default = "_color",description = "color map affix")
bpy.types.Scene.affixRoughness = bpy.props.StringProperty (name = "affixRoughness",default = "_rough",description = "Roughness map affix")
bpy.types.Scene.affixEmission = bpy.props.StringProperty (name = "affixEmission",default = "_emit",description = "Emission map affix")
bpy.types.Scene.affixUV = bpy.props.StringProperty (name = "affixUV",default = "_uv",description = "UV map affix")
def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)
if __name__ == "__main__":
register()