forked from sakalond/StableGen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.py
More file actions
570 lines (493 loc) · 28.6 KB
/
project.py
File metadata and controls
570 lines (493 loc) · 28.6 KB
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
import bpy
import os
from math import atan, tan
from .utils import get_last_material_index, get_file_path, get_dir_path
from .render_tools import prepare_baking, bake_texture, unwrap
from mathutils import Vector
def project_image(context, to_project, mat_id, stop_index=1000000):
"""
Projects an image onto all mesh objects using UV Project Modifier.
"""
def build_mix_tree(shaders, weight_nodes, nodes_collection, links, last_node=None, level=0, stop_index=1000000):
"""
Recursively builds a binary tree of mix shader nodes.
The mix factor between two shader nodes is computed dynamically using
the outputs of weight_nodes assumed to come from OSL/maths nodes.
For each pair, the total weight is created using an ADD node.
Then a DIVIDE node computes:
mix_factor = weight_A / (weight_A + weight_B)
which is connected to the Mix Shader node's Fac input.
Params:
shaders: list of shader nodes (e.g., Principled BSDF nodes).
weight_nodes: list of nodes whose outputs provide the dynamic weight values.
(e.g. the less_than nodes outputs)
nodes_collection: the node tree's nodes collection.
links: the node tree's links collection.
Returns:
A tuple (final_shader, final_weight_node).
"""
# Compute offsets based on recursion level
if context.scene.refine_preserve:
x_offset = 1000 + level * 800 + last_node.location[0] + 200 if context.scene.early_priority else 1000 + level * 600 + last_node.location[0] + 200
else:
x_offset = 1000 + level * 800 if context.scene.early_priority else 1000 + level * 600
y_offset = 0
if len(shaders) == 1:
# Base-case: Mix single color with fallback magenta
final_mix = nodes_collection.new("ShaderNodeMixRGB")
final_mix.location = (x_offset - 200, y_offset)
# Convert fallback_color from 3 to 4 components by appending alpha 1.0
final_mix.inputs["Color2"].default_value = (*context.scene.fallback_color, 1.0)
# Connect the single color output to Color1
links.new(shaders[0].outputs[0], final_mix.inputs["Color1"])
if last_node:
# Connect the final mix node to the last node
links.new(last_node.outputs[0], final_mix.inputs["Color2"])
# Create a compare node to drive the mix factor
if context.scene.generation_method == 'refine' and context.scene.refine_preserve:
compare_node = nodes_collection.new("ShaderNodeValToRGB")
compare_node.location = (x_offset - 500, y_offset)
compare_node.color_ramp.elements[0].position = 0.0
compare_node.color_ramp.elements[0].color = (1, 1, 1, 1)
compare_node.color_ramp.elements[1].position = 0.6
compare_node.color_ramp.elements[1].color = (0, 0, 0, 1)
compare_node.color_ramp.interpolation = 'LINEAR'
else:
compare_node = nodes_collection.new("ShaderNodeMath")
compare_node.operation = 'COMPARE'
# Value to compare against (Value 2)
compare_node.inputs[1].default_value = 0.0
# Epsilon for comparison
compare_node.inputs[2].default_value = 0.0
compare_node.location = (x_offset - 500, y_offset)
# Connect final add node to compare node
links.new(weight_nodes[0].outputs[0], compare_node.inputs[0])
links.new(compare_node.outputs[0], final_mix.inputs["Fac"])
if not context.scene.apply_bsdf:
return final_mix, compare_node
# Add a principle shader for the mixed color
should_add_principled = True
if context.scene.generation_method == 'refine' and context.scene.refine_preserve:
# Final principled is at last_node's output if it is BSDF
final_principled = last_node.outputs[0].links[0].to_node
if final_principled.type == 'BSDF_PRINCIPLED':
should_add_principled = False
if should_add_principled:
final_principled = nodes_collection.new("ShaderNodeBsdfPrincipled")
final_principled.location = (x_offset, y_offset)
final_principled.inputs["Roughness"].default_value = 1.0
links.new(final_mix.outputs[0], final_principled.inputs[0])
return final_principled, compare_node
new_shaders = []
new_weight_nodes = []
i = 0
while i < len(shaders):
if i + 1 < len(shaders):
# Sum the weights
vert_offset = -200 * (i // 2)
sum_node = nodes_collection.new("ShaderNodeMath")
sum_node.operation = 'ADD'
sum_node.location = (x_offset - 800, y_offset + vert_offset) if context.scene.early_priority else (x_offset - 600, y_offset + vert_offset)
links.new(weight_nodes[i].outputs[0], sum_node.inputs[0])
links.new(weight_nodes[i+1].outputs[0], sum_node.inputs[1])
# Compute mix factor: weight_A / (weight_A+weight_B)
div_node = nodes_collection.new("ShaderNodeMath")
div_node.operation = 'DIVIDE'
div_node.location = (x_offset - 600, y_offset + vert_offset) if context.scene.early_priority else (x_offset - 400, y_offset + vert_offset)
links.new(weight_nodes[i+1].outputs[0], div_node.inputs[0])
links.new(sum_node.outputs[0], div_node.inputs[1])
if context.scene.early_priority:
# Add map range node
map_range_node = nodes_collection.new("ShaderNodeMapRange")
map_range_node.location = (x_offset - 400, y_offset + vert_offset)
# to_min, to_max, from_max are set to 0.0 and 1.0 by default
# set to prioritize earlier images
map_range_node.inputs[1].default_value = context.scene.early_priority_strength
links.new(div_node.outputs[0], map_range_node.inputs[0])
# Create a MixRGB node for color blending of the two inputs
mix_node = nodes_collection.new("ShaderNodeMixRGB")
mix_node.location = (x_offset - 200, y_offset + vert_offset)
mix_node.use_clamp = True
# Connect first color to Color1 and second color to Color2
links.new(shaders[i].outputs[0], mix_node.inputs["Color1"])
links.new(shaders[i+1].outputs[0], mix_node.inputs["Color2"])
# Use the computed mix factor
to_connect = div_node if not context.scene.early_priority else map_range_node
links.new(to_connect.outputs[0], mix_node.inputs["Fac"])
new_shaders.append(mix_node)
# The new effective weight is the sum (stored in sum_node)
new_weight_nodes.append(sum_node)
i += 2
else:
new_shaders.append(shaders[i])
new_weight_nodes.append(weight_nodes[i])
i += 1
return build_mix_tree(new_shaders, new_weight_nodes, nodes_collection, links, last_node, level+1)
cameras = [obj for obj in context.scene.objects if obj.type == 'CAMERA']
cameras.sort(key=lambda x: x.name)
# Force refresh of the UI
for area in context.screen.areas:
area.tag_redraw()
# Apply projection to all mesh objects for each camera
for i, camera in enumerate(cameras):
for x, obj in enumerate(to_project):
# We can skip the UV Projection step in sequential mode for i > 0
if context.scene.generation_method != 'sequential' or stop_index == 0 or context.scene.bake_texture:
# Deselect all objects
bpy.ops.object.select_all(action='DESELECT')
# Select object as active (needed for applying the modifier)
context.view_layer.objects.active = obj
obj.select_set(True)
# Make object data single-user before applying modifier
bpy.ops.object.make_single_user(object=True, obdata=True)
# Check if the data is now single user
if obj.data.users > 1:
# If not, we need to make it single user again
print("Warning: Cannot make object data single user. Making a copy.")
obj.data = obj.data.copy()
obj.data.name = f"{obj.name}_data"
obj.data.update()
# Check again
if obj.data.users > 1:
print("Error: Cannot make object data single user. Exiting.")
return Exception("Cannot make object data single user. Exiting.")
# Add new UV map if not present (with same name)
# Check if "ProjectionUV" UV map already exists
uv_map = None
if context.scene.overwrite_material and not context.scene.bake_texture:
for uv in obj.data.uv_layers:
if uv.name == f"ProjectionUV_{i}_{mat_id}":
uv_map = uv
break
# If the objects has no UV map and we are baking textures, create a new one
if not obj.data.uv_layers and context.scene.bake_texture:
obj.data.uv_layers.new(name="BakeUV")
if not uv_map:
uv_map = obj.data.uv_layers.new(name=f"ProjectionUV_{i}_{mat_id}")
# Add the UV Project Modifier if not present
uv_project_mod = obj.modifiers.new(name="UVProject", type='UV_PROJECT')
# Assign the active camera to the UV Project modifier
if not camera:
return False
uv_project_mod.projectors[0].object = camera
# Set the UV map for the modifier
try:
uv_project_mod.uv_layer = uv_map.name
except:
# Throw custom exception: Not enough UV map slots
raise Exception("Not enough UV map slots. Please remove some UV maps.")
# Calculate and set the aspect ratio
render = context.scene.render
aspect_ratio = render.resolution_x / render.resolution_y
uv_project_mod.aspect_x = aspect_ratio if aspect_ratio > 1 else 1
uv_project_mod.aspect_y = 1 / aspect_ratio if aspect_ratio < 1 else 1
# Apply the modifier
bpy.ops.object.modifier_apply(modifier=uv_project_mod.name)
original_uv_map = obj.data.uv_layers[0]
# If we are running in sequential mode, we already have baked textures for i < stop_index
if context.scene.bake_texture:
if (stop_index > 0 and context.scene.generation_method == 'sequential'):
# Deselect all objects
bpy.ops.object.select_all(action='DESELECT')
# Select object as active (needed for applying the modifier)
context.view_layer.objects.active = obj
obj.select_set(True)
if i <= stop_index and (not context.scene.generation_method == 'sequential' or i == stop_index):
simple_project_bake(context, i, obj, mat_id)
obj.data.uv_layers.remove(obj.data.uv_layers[-1]) # Remove the last UV map
for obj in to_project:
# Deselect all objects
bpy.ops.object.select_all(action='DESELECT')
context.view_layer.objects.active = obj
obj.select_set(True)
# Create the material
to_switch = False
if context.scene.generation_method == "refine" and context.scene.refine_preserve and not context.scene.overwrite_material:
# Copy active material
mat = obj.active_material.copy()
obj.data.materials.append(mat)
to_switch = True
elif obj.active_material and (context.scene.overwrite_material or (context.scene.generation_method == "refine" and context.scene.refine_preserve) or (context.scene.generation_method == 'sequential' and stop_index > 0)):
# Use active material
mat = obj.active_material
else:
mat = bpy.data.materials.new(name="ProjectionMaterial")
obj.data.materials.append(mat)
# Mark as active material
obj.active_material_index = obj.material_slots.find(mat.name)
to_switch = True
if to_switch:
original_materials = obj.data.materials[:]
# Clear existing materials
obj.data.materials.clear()
# Add the new material
obj.data.materials.append(mat)
# Add the rest of the original materials
for original_mat in original_materials:
if original_mat != mat:
obj.data.materials.append(original_mat)
# Enable use of nodes
mat.use_nodes = True
nodes = mat.node_tree.nodes
links = mat.node_tree.links
previous_node = None
output = None
original_uv_map = obj.data.uv_layers[0]
if context.scene.generation_method == 'sequential' and stop_index > 0:
# We just need to remove the compare nodes which are connected to script node at stop_index
script_node = None
# First find all script nodes with label {stop_index}-{mat_id}
for node in nodes:
if node.type == 'SCRIPT' and node.label == f"{stop_index}-{mat_id}":
script_node = node
break
compare_output_sockets = set()
if script_node:
# Find all compare nodes connected to the script node
for link in script_node.outputs[0].links:
if link.to_node.type == 'MATH' and link.to_node.operation == 'LESS_THAN':
# Save all outputs (nodes connected to) of compare nodes
for link2 in link.to_node.outputs[0].links:
compare_output_sockets.add(link2.to_socket)
# Remove the compare node
nodes.remove(link.to_node)
# Connect the script node to all outputs
for output in compare_output_sockets:
links.new(script_node.outputs[0], output)
# We also need to set the generated image to the texture node with label {stop_index}-{mat_id}
for node in nodes:
if node.type == 'TEX_IMAGE' and node.label == f"{stop_index}-{mat_id}":
output_dir = context.preferences.addons[__package__].preferences.output_dir
if not context.scene.bake_texture:
image = bpy.data.images.load(get_file_path(context, "generated", camera_id=stop_index, material_id=mat_id))
if (context.scene.generation_method == 'sequential' or context.scene.generation_method == 'separate') and context.scene.sequential_ipadapter and context.scene.sequential_ipadapter_regenerate and not context.scene.use_ipadapter and stop_index == 0 and context.scene.sequential_ipadapter_mode == 'first':
image = bpy.data.images.load(get_file_path(context, "generated", camera_id=stop_index, material_id=mat_id).replace(".png", "_ipadapter.png"))
else:
# Use baked texture
image = bpy.data.images.load(get_file_path(context, "generated_baked", camera_id=stop_index, material_id=mat_id, object_name=obj.name))
node.image = image
break
# Now we can continue to the next object
continue
elif not context.scene.refine_preserve or not context.scene.generation_method == 'refine':
# Clear existing nodes
for node in nodes:
nodes.remove(node)
else:
# Find the node connected to the output node
for node in nodes:
if node.type == 'OUTPUT_MATERIAL':
output = node
break
if output:
# Save the node connected to the output node into a variable
if output.inputs[0].links[0].from_node.type == 'BSDF_PRINCIPLED':
previous_node = output.inputs[0].links[0].from_node.inputs[0].links[0].from_node
else:
previous_node = output.inputs[0].links[0].from_node
if not (context.scene.generation_method == 'refine' and context.scene.refine_preserve):
output = nodes.new("ShaderNodeOutputMaterial")
output.location = (3000, 0)
geometry = nodes.new("ShaderNodeNewGeometry")
geometry.location = (-600, 0)
tex_image_nodes = []
uv_map_nodes = []
subtract_nodes = []
normalize_nodes = []
script_nodes = []
script_nodes_outputs = []
add_camera_loc_nodes = []
length_nodes = []
camera_fov_nodes = []
camera_aspect_ratio_nodes = []
camera_direction_nodes = []
camera_up_nodes = []
for i, camera in enumerate(cameras):
# Add image texture node
tex_image = nodes.new("ShaderNodeTexImage")
output_dir = context.preferences.addons[__package__].preferences.output_dir
if i <= stop_index:
if not context.scene.bake_texture:
image = bpy.data.images.load(get_file_path(context, "generated", camera_id=i, material_id=mat_id))
if (context.scene.generation_method == 'sequential' or context.scene.generation_method == 'separate') and context.scene.sequential_ipadapter and context.scene.sequential_ipadapter_regenerate and not context.scene.use_ipadapter and i == 0 and context.scene.sequential_ipadapter_mode == 'first':
image = bpy.data.images.load(get_file_path(context, "generated", camera_id=i, material_id=mat_id).replace(".png", "_ipadapter.png"))
else:
# Use baked texture
image = bpy.data.images.load(get_file_path(context, "generated_baked", camera_id=i, material_id=mat_id, object_name=obj.name))
tex_image.image = image
tex_image.location = (0, -200 * i)
tex_image.extension = 'CLIP'
tex_image.label = f"{i}-{mat_id}"
tex_image_nodes.append(tex_image)
# Add UV map node
uv_map_node = nodes.new("ShaderNodeUVMap")
if not context.scene.bake_texture:
uv_map_node.uv_map = f"ProjectionUV_{i}_{mat_id}"
else:
# Use the original UV map
uv_map_node.uv_map = original_uv_map.name
uv_map_node.location = (-200, -200 * (i+1))
uv_map_nodes.append(uv_map_node)
# Compute the dot product of the direction vector and geometry normal
subtract = nodes.new("ShaderNodeVectorMath")
subtract.operation = 'SUBTRACT'
subtract.location = (-400, -300 + (-800) * i)
subtract.inputs[1].default_value = camera.location
subtract_nodes.append(subtract)
normalize = nodes.new("ShaderNodeVectorMath")
normalize.operation = 'NORMALIZE'
normalize.location = (-400, -500 + (-800) * (i))
normalize_nodes.append(normalize)
# Switch to Cycles for OSL support
context.scene.render.engine = 'CYCLES'
context.scene.cycles.device = 'CPU'
context.scene.cycles.shading_system = True
# Add a script node
script = nodes.new("ShaderNodeScript")
script.location = (-400, (-800) * i)
script.mode = 'EXTERNAL'
script.filepath = os.path.join(os.path.dirname(__file__), "raycast.osl")
script.inputs["AngleThreshold"].default_value = context.scene.discard_factor
script.inputs["Power"].default_value = context.scene.weight_exponent
script.label = f"{i}-{mat_id}"
script_nodes.append(script)
if i > stop_index:
# Connect a temporary less than node to the script node
less_than = nodes.new("ShaderNodeMath")
less_than.operation = 'LESS_THAN'
less_than.location = (-200, (-800) * i)
less_than.inputs[1].default_value = -1
links.new(script.outputs[0], less_than.inputs[0])
script_nodes_outputs.append(less_than)
else:
script_nodes_outputs.append(script)
# Add additional add node, which will contain camera's FOV in first default value, and camera's aspect ratio in second default value
camera_fov = nodes.new("ShaderNodeValue")
camera_fov.location = (-600, 200 + 300 * i)
fov = camera.data.angle_x
# Correct the FOV for vertical aspect ratio
if context.scene.render.resolution_y > context.scene.render.resolution_x:
fov = 2 * atan(tan(fov / 2) * context.scene.render.resolution_x / context.scene.render.resolution_y)
camera_fov.outputs[0].default_value = fov
camera_fov_nodes.append(camera_fov)
camera_aspect_ratio = nodes.new("ShaderNodeValue")
camera_aspect_ratio.location = (-600, 200 + 300 * i)
# Add camera direction and up nodes (combine XYZ)
camera_direction = nodes.new("ShaderNodeCombineXYZ")
camera_direction.location = (-600, 200 + 300 * i)
# Get the camera direction vector
camera_direction.inputs[0].default_value = (camera.matrix_world.to_quaternion() @ Vector((0, 0, -1))).x
camera_direction.inputs[1].default_value = (camera.matrix_world.to_quaternion() @ Vector((0, 0, -1))).y
camera_direction.inputs[2].default_value = (camera.matrix_world.to_quaternion() @ Vector((0, 0, -1))).z
camera_direction_nodes.append(camera_direction)
camera_up = nodes.new("ShaderNodeCombineXYZ")
camera_up.location = (-600, 200 + 300 * i)
# Get the camera up vector
camera_up.inputs[0].default_value = (camera.matrix_world.to_quaternion() @ Vector((0, 1, 0))).x
camera_up.inputs[1].default_value = (camera.matrix_world.to_quaternion() @ Vector((0, 1, 0))).y
camera_up.inputs[2].default_value = (camera.matrix_world.to_quaternion() @ Vector((0, 1, 0))).z
camera_up_nodes.append(camera_up)
camera_aspect_ratio.outputs[0].default_value = context.scene.render.resolution_x / context.scene.render.resolution_y
camera_aspect_ratio_nodes.append(camera_aspect_ratio)
# Add combine XYZ node
add_camera_loc = nodes.new("ShaderNodeCombineXYZ")
add_camera_loc.location = (-600, 200 + 300 * i)
add_camera_loc.inputs[0].default_value = camera.location.x
add_camera_loc.inputs[1].default_value = camera.location.y
add_camera_loc.inputs[2].default_value = camera.location.z
add_camera_loc_nodes.append(add_camera_loc)
# Add length node
length = nodes.new("ShaderNodeVectorMath")
length.operation = 'LENGTH'
length.location = (-400, 200 * (i+1))
length_nodes.append(length)
# Build mix shader tree
mix_node, _ = build_mix_tree(tex_image_nodes, script_nodes_outputs, nodes, links, previous_node, stop_index=stop_index)
links.new(mix_node.outputs[0], output.inputs["Surface"])
# Move output node right to the mix_node
output.location = (mix_node.location[0] + 400, mix_node.location[1])
for i, camera in enumerate(cameras):
tex_image = tex_image_nodes[i]
uv_map_node = uv_map_nodes[i]
subtract = subtract_nodes[i]
normalize = normalize_nodes[i]
script = script_nodes[i]
add_camera_loc = add_camera_loc_nodes[i]
length = length_nodes[i]
camera_fov = camera_fov_nodes[i]
camera_aspect_ratio = camera_aspect_ratio_nodes[i]
camera_direction = camera_direction_nodes[i]
camera_up = camera_up_nodes[i]
# Connect common nodes
links.new(uv_map_node.outputs["UV"], tex_image.inputs["Vector"])
links.new(geometry.outputs["Position"], subtract.inputs[0])
links.new(subtract.outputs["Vector"], normalize.inputs[0])
links.new(normalize.outputs["Vector"], script.inputs["Direction"])
links.new(add_camera_loc.outputs["Vector"], script.inputs["Origin"])
links.new(length.outputs["Value"], script.inputs["threshold"])
links.new(geometry.outputs["Normal"], script.inputs["SurfaceNormal"])
links.new(camera_fov.outputs[0], script.inputs["CameraFOV"])
links.new(camera_aspect_ratio.outputs[0], script.inputs["CameraAspect"])
links.new(camera_direction.outputs[0], script.inputs["CameraDir"])
links.new(camera_up.outputs[0], script.inputs["CameraUp"])
links.new(subtract.outputs["Vector"], length.inputs[0])
# Add material index node (subtract node)
subtract_node = nodes.new("ShaderNodeMath")
subtract_node.operation = 'SUBTRACT'
subtract_node.inputs[0].default_value = mat_id
subtract_node.location = (-1000, 0)
return True
def simple_project_bake(context, camera_id, obj, mat_id):
# Create a temporary material for the projection
mat = bpy.data.materials.new(name="ProjectionMaterialTemp")
obj.data.materials.append(mat)
# Switch the active material to the new material (Switch to edit mode, select all, assign the material)
obj.active_material_index = len(obj.material_slots) - 1
bpy.context.view_layer.objects.active = obj
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.object.material_slot_assign()
bpy.ops.object.mode_set(mode='OBJECT')
# Unwrap (only for the first camera)
if camera_id == 0:
unwrap(obj, context.scene.bake_unwrap_method, context.scene.bake_unwrap_overlap_only)
# Enable use of nodes
mat.use_nodes = True
nodes = mat.node_tree.nodes
links = mat.node_tree.links
# Clear existing nodes
for node in nodes:
nodes.remove(node)
# Add image texture node
tex_image = nodes.new("ShaderNodeTexImage")
file_path = get_file_path(context, "generated", camera_id=camera_id, material_id=mat_id)
if (context.scene.generation_method == 'sequential' or context.scene.generation_method == 'separate') and context.scene.sequential_ipadapter and context.scene.sequential_ipadapter_regenerate and not context.scene.use_ipadapter and camera_id == 0 and context.scene.sequential_ipadapter_mode == 'first':
file_path = get_file_path(context, "generated", camera_id=camera_id, material_id=mat_id).replace(".png", "_ipadapter.png")
tex_image.image = bpy.data.images.load(file_path)
# Add UV map node
uv_map_node = nodes.new("ShaderNodeUVMap")
uv_map_node.uv_map = f"ProjectionUV_{camera_id}_{mat_id}"
# Add BSDF node
bsdf = nodes.new("ShaderNodeBsdfPrincipled")
bsdf.inputs["Roughness"].default_value = 1.0
# Add output node
output = nodes.new("ShaderNodeOutputMaterial")
links.new(uv_map_node.outputs["UV"], tex_image.inputs["Vector"])
# Connect the nodes
if context.scene.apply_bsdf:
links.new(tex_image.outputs["Color"], bsdf.inputs["Base Color"])
links.new(bsdf.outputs["BSDF"], output.inputs["Surface"])
else:
links.new(tex_image.outputs["Color"], output.inputs["Surface"])
# Bake texture using BakeTextures.bake_texture
texture_size = context.scene.bake_texture_size
original_engine = context.scene.render.engine
prepare_baking(context)
# If the object has no UV map, create one
if not obj.data.uv_layers:
obj.data.uv_layers.new(name="UVMap")
bake_texture(context, obj, texture_size, suffix=f"{camera_id}-{mat_id}", output_dir=get_dir_path(context, "generated_baked"))
context.scene.render.engine = original_engine
# Remove the temporary material
bpy.ops.object.material_slot_remove()