-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
826 lines (644 loc) · 29.5 KB
/
utils.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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
import bpy
import os
import re
import colorsys
import math
### texel density calculation
def calculate_texel_density(obj, desired_texel_density):
# Calculate the surface area of the object
obj_surface_area = sum(p.area for p in obj.data.polygons)
# Calculate the required number of pixels (texels)
required_texels = desired_texel_density * math.sqrt(obj_surface_area)
# Determine the appropriate texture resolution
# We assume square textures and round to the nearest power of two
texture_size = 2 ** math.ceil(math.log2(required_texels))
return texture_size
### nodes to compositor and back
def create_compositor_node_tree(image1, image2, blend_mode):
bpy.context.scene.use_nodes = True
tree = bpy.context.scene.node_tree
# Clear existing nodes
for node in tree.nodes:
tree.nodes.remove(node)
# Create image input nodes
image_node1 = tree.nodes.new(type='CompositorNodeImage')
image_node1.image = image1
image_node1.location = -300, 200
image_node2 = tree.nodes.new(type='CompositorNodeImage')
image_node2.image = image2
image_node2.location = -300, -200
# Create a Mix node
mix_node = tree.nodes.new(type='CompositorNodeMixRGB')
mix_node.blend_type = blend_mode
mix_node.location = 200, 0
mix_node.use_alpha = True # Enable the use_alpha property
# Connect image nodes to the Mix node
tree.links.new(image_node1.outputs['Image'], mix_node.inputs[1])
tree.links.new(image_node2.outputs['Image'], mix_node.inputs[2])
# Connect the alpha output of the second image to the factor input of the Mix node
#tree.links.new(image_node2.outputs['Alpha'], mix_node.inputs[0])
# Add a Composite node to output the result
composite_node = tree.nodes.new(type='CompositorNodeComposite')
composite_node.location = 400, 0
tree.links.new(mix_node.outputs['Image'], composite_node.inputs['Image'])
###photostack start
def copy_photostack_nodes_to_compositor():
# Ensure an object is selected
obj = bpy.context.active_object
if not obj:
print("No active object selected.")
return
# Ensure the object has an active material
if not obj.active_material:
print("The selected object has no active material.")
return
material = obj.active_material
# Ensure the material uses nodes
if not material.use_nodes:
print("The active material does not use nodes.")
return
# Access the node tree of the active material
node_tree = material.node_tree
active_node = node_tree.nodes.active # Get the active node in the node editor
# Check if the active node is a group node
if not active_node or active_node.type != 'GROUP':
print("The active node is not a group node.")
return
shader_node_group = active_node.node_tree
# Enable Compositor Nodes
bpy.context.scene.use_nodes = True
compositor_nodes = bpy.context.scene.node_tree.nodes
compositor_links = bpy.context.scene.node_tree.links
# Create a Viewer Node if not already present
viewer_node = None
for node in compositor_nodes:
if node.type == 'VIEWER':
viewer_node = node
break
if not viewer_node:
viewer_node = compositor_nodes.new(type="CompositorNodeViewer")
viewer_node.location = (500, 300)
# Keep track of added image and mix nodes for connections
image_nodes = []
node_offset_x = 0
node_offset_y = 0
# Iterate through nodes in photoStack, copying Image Texture and Mix nodes
mix_nodes = [node for node in shader_node_group.nodes if node.type == 'MIX'] # Updated to 'MIX'
if not mix_nodes:
print("No Mix nodes found in the PhotoStack group.")
return
# Print the blend types found in shader Mix nodes
for i, mix_node in enumerate(mix_nodes):
print(f"Shader Mix Node {i} blend type: {mix_node.blend_type}")
# Copy Image Texture nodes
for node in shader_node_group.nodes:
if node.type == 'TEX_IMAGE':
new_node = compositor_nodes.new(type="CompositorNodeImage")
new_node.location = (node_offset_x, node_offset_y)
new_node.label = "PhotoStack Image"
new_node.name = node.name
if node.image:
new_node.image = node.image
image_nodes.append(new_node)
node_offset_x += 300
if len(image_nodes) >= 2:
# First Mix node setup
first_mix = compositor_nodes.new(type="CompositorNodeMixRGB")
first_mix.location = (node_offset_x, node_offset_y)
first_mix.use_alpha = True
first_mix.blend_type = mix_nodes[0].blend_type
print(f"Set first compositor mix node blend type to: {first_mix.blend_type}")
compositor_links.new(image_nodes[0].outputs[0], first_mix.inputs[1])
compositor_links.new(image_nodes[1].outputs[0], first_mix.inputs[2])
node_offset_x += 300
# Additional Mix nodes
previous_mix = first_mix
for i in range(2, len(image_nodes)):
mix_node = compositor_nodes.new(type="CompositorNodeMixRGB")
mix_node.location = (node_offset_x, node_offset_y)
mix_node.use_alpha = True
if i - 1 < len(mix_nodes):
blend_type = mix_nodes[i - 1].blend_type
mix_node.blend_type = blend_type
print(f"Set compositor mix node {i} blend type to: {blend_type}")
# Link previous Mix node output to the current Mix node input 1
compositor_links.new(previous_mix.outputs[0], mix_node.inputs[1])
compositor_links.new(image_nodes[i].outputs[0], mix_node.inputs[2])
previous_mix = mix_node
node_offset_x += 300
else:
print("Insufficient image nodes to perform mixing.")
compositor_links.new(previous_mix.outputs[0], viewer_node.inputs[0])
print("PhotoStack nodes copied to the Compositor with alpha blending and blend types applied.")
def find_selected_texture_nodes(node_tree):
"""Recursively find all selected image texture nodes in a node tree."""
selected_nodes = []
for node in node_tree.nodes:
if getattr(node, 'texture_swap_props', None) and node.texture_swap_props.swap_select:
selected_nodes.append(node)
elif node.type == 'GROUP' and node.node_tree:
selected_nodes.extend(find_selected_texture_nodes(node.node_tree))
return selected_nodes
### render settings for photostack
def update_texture_settings(self, context):
"""Update the texture settings collection whenever the number of textures changes."""
scene = context.scene
current_num = len(scene.texture_settings)
requested_num = scene.num_textures
# Add entries if num_textures is greater than the current number of entries
if requested_num > current_num:
for i in range(requested_num - current_num):
scene.texture_settings.add()
# Remove entries if num_textures is less than the current number of entries
elif requested_num < current_num:
for i in range(current_num - requested_num):
scene.texture_settings.remove(len(scene.texture_settings) - 1)
def render_and_extract_image(output_name, width, height):
bpy.context.scene.render.resolution_x = width
bpy.context.scene.render.resolution_y = height
bpy.context.scene.render.image_settings.file_format = 'PNG'
bpy.context.scene.render.filepath = f'/tmp/{output_name}.png'
bpy.ops.render.render(write_still=True)
combined_image = bpy.data.images.load(bpy.context.scene.render.filepath)
return combined_image
#define function for check if mesh and _canvas in name
def is_canvas_mesh(obj):
return obj and obj.type == 'MESH' and '_canvas' in obj.name
def is_subject_mesh(obj):
return obj and obj.type == 'MESH' and '_subject' in obj.name
# workup functions for Trace2Curve to work
def new_convert_curve_object(collection, name):
curve = bpy.data.curves.new(name=name, type="CURVE")
curve.dimensions = "2D"
convert_object = bpy.data.objects.new(name=name, object_data=curve)
collection.objects.link(convert_object)
return convert_object
def convert_gpencil_to_curve(gpencil_object, flatten_layers=True):
gp_col = gpencil_object.users_collection[0]
gp = gpencil_object.data
if flatten_layers:
obj = new_convert_curve_object(gp_col, gpencil_object.name + "_curve")
for layer in gp.layers:
if not flatten_layers:
obj = new_convert_curve_object(gp_col, f"{gpencil_object.name}_curve_layer_{layer.info}")
for frame in layer.frames:
for stroke in frame.strokes:
spline = obj.data.splines.new(type="POLY")
spline.points.add(len(stroke.points) - 1) # Spline starts with 1 point
for i, point in enumerate(stroke.points):
# Adjust the point coordinate to match the original GPencil position
co = gpencil_object.matrix_world @ point.co
spline.points[i].co = [co.x, co.y, co.z, 1]
return obj
def move_trace_objects_to_collection(objects, collection_name):
# Create a new collection if it doesn't exist
if collection_name not in bpy.data.collections:
new_collection = bpy.data.collections.new(collection_name)
bpy.context.scene.collection.children.link(new_collection)
else:
new_collection = bpy.data.collections[collection_name]
for obj in objects:
if obj.name not in new_collection.objects:
new_collection.objects.link(obj)
for coll in obj.users_collection:
if coll.name != collection_name:
coll.objects.unlink(obj)
def convert_image_plane_to_curve(plane_obj):
# Ensure the object is of type 'MESH'
if plane_obj.type != 'MESH':
print(f"Selected object {plane_obj.name} is not a mesh.")
return
# Retrieve the image from the plane's material
material = plane_obj.active_material
if not material or not material.use_nodes:
print(f"No material with nodes found on {plane_obj.name}.")
return
# Find the image texture node
texture_node = None
for node in material.node_tree.nodes:
if node.type == 'TEX_IMAGE':
texture_node = node
break
if not texture_node:
print(f"No image texture node found in the material of {plane_obj.name}.")
return
# Get the image from the texture node
image = texture_node.image
if not image:
print(f"No image found in the texture node of {plane_obj.name}.")
return
# Get the size of the plane
scale_x = plane_obj.dimensions.x
scale_y = plane_obj.dimensions.y
# Create a new empty object
bpy.ops.object.empty_add(type='IMAGE', location=plane_obj.location)
empty_obj = bpy.context.object
empty_obj.name = plane_obj.name + "_Empty"
# Assign the image to the empty
empty_obj.data = image
# Make the image data single user
bpy.ops.object.make_single_user(object=True, obdata=True, material=True, animation=False)
# Set the scale of the empty to match the plane
empty_obj.scale = (scale_x, scale_y, 1)
# Apply the scale to the empty
bpy.context.view_layer.objects.active = empty_obj
bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
print(f"Converted {plane_obj.name} to {empty_obj.name}.")
# Convert the Image Empty to Grease Pencil using Trace Image to Grease Pencil
bpy.context.view_layer.objects.active = empty_obj
bpy.ops.gpencil.trace_image()
# Ensure the created object is a GPencil object before conversion
gpencil_objs = [obj for obj in bpy.context.view_layer.objects if obj.type == 'GPENCIL']
if gpencil_objs:
gpencil_obj = gpencil_objs[-1] # Get the most recently created GPencil object
gpencil_obj.name = empty_obj.name + "_GPencil"
# Convert the Grease Pencil to a Curve
curve_obj = convert_gpencil_to_curve(gpencil_obj, flatten_layers=True)
# Match the orientation and scaling of the original plane
curve_obj.location = plane_obj.location
curve_obj.rotation_euler = plane_obj.rotation_euler
curve_obj.scale = plane_obj.scale
print(f"Converted {gpencil_obj.name} to {curve_obj.name}.")
# Move the original plane, image empty, and GPencil objects to the new collection
collection_name = 'autotrace_objects_' + plane_obj.name
move_trace_objects_to_collection([plane_obj, empty_obj, gpencil_obj], collection_name)
else:
print("The converted object is not a Grease Pencil object.")
# Get the active texture node and its size
def get_active_texture_node_image_size():
if bpy.context.space_data.type == 'NODE_EDITOR':
node_tree = bpy.context.space_data.node_tree
if node_tree:
active_node = node_tree.nodes.active
if active_node and active_node.type == 'TEX_IMAGE' and active_node.image:
width = active_node.image.size[0]
height = active_node.image.size[1]
return width, height
return None, None
# Get size of image in image editor
def get_active_image_size():
if bpy.context.space_data.type == 'IMAGE_EDITOR':
active_image = bpy.context.space_data.image
if active_image:
width = int(active_image.size[0])
height = int(active_image.size[1])
return width, height
return None, None
# Create a new texture node based on the active node size
def create_new_texture_node_with_size(width, height):
image_name = f"Texture_{width}x{height}"
new_image = bpy.data.images.new(name=image_name, width=width, height=height)
if bpy.context.space_data.type == 'NODE_EDITOR':
node_tree = bpy.context.space_data.node_tree
if node_tree:
new_texture_node = node_tree.nodes.new(type='ShaderNodeTexImage')
new_texture_node.image = new_image
new_texture_node.label = image_name
active_node = node_tree.nodes.active
if active_node:
new_texture_node.location = (active_node.location.x, active_node.location.y - 260)
# Function to set up single texture paint view
def single_texture_paint_view():
# Iterate through all areas in the current screen context
for area in bpy.context.screen.areas:
# Check if the area is a 3D Viewport
if area.type == 'VIEW_3D':
space = area.spaces.active
# Check if the active space is a 3D Viewport
if space.type == 'VIEW_3D':
# Set shading type to Solid
space.shading.type = 'SOLID'
# Set lighting to Flat
space.shading.light = 'FLAT'
# Set color type to Texture
space.shading.color_type = 'TEXTURE'
# Function to set up multi texture paint view
def multi_texture_paint_view():
# Iterate through all areas in the current screen context
for area in bpy.context.screen.areas:
# Check if the area is a 3D Viewport
if area.type == 'VIEW_3D':
space = area.spaces.active
# Check if the active space is a 3D Viewport
if space.type == 'VIEW_3D':
# Set shading type to Material
space.shading.type = 'MATERIAL'
# Function to set color management settings for painting
def paint_view_color_management_settings():
scene = bpy.context.scene
# Set the display device to sRGB
scene.display_settings.display_device = 'sRGB'
# Set the view transform to Standard
scene.view_settings.view_transform = 'Standard'
# Set the look to Medium High Contrast
scene.view_settings.look = 'Medium Contrast'
# Set the exposure to 0
scene.view_settings.exposure = 0
# Set the gamma to 1
scene.view_settings.gamma = 1
def next_power_of_2(x):
return 1 if x == 0 else 2**math.ceil(math.log2(x))
def previous_power_of_2(x):
return 1 if x == 0 else 2**math.floor(math.log2(x))
def create_image_plane_from_image(active_image, scale_factor=0.01):
width = active_image.size[0]
height = active_image.size[1]
name = active_image.name + "_canvas"
print(f"Active image dimensions: {width} x {height}")
mesh = bpy.data.meshes.new(name=name + "Mesh")
obj = bpy.data.objects.new(name=name, object_data=mesh)
bpy.context.collection.objects.link(obj)
bpy.ops.object.select_all(action='DESELECT')
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.primitive_plane_add(size=1)
bpy.ops.transform.resize(value=(width * scale_factor / 5, height * scale_factor / 5, 1))
bpy.ops.object.mode_set(mode='OBJECT')
# Create material and node setup with Image and Principled BSDF
mat = bpy.data.materials.new(name=name + "Material")
mat.use_nodes = True
# Get nodes
bsdf = mat.node_tree.nodes["Principled BSDF"]
bsdf.location = (-212.01303100585938, 704.966796875)
tex_image = mat.node_tree.nodes.new('ShaderNodeTexImage')
tex_image.location = (-617.7554931640625, 626.1090087890625)
tex_image.image = active_image
# Connect Image texture to (BSDF)
mat.node_tree.links.new(bsdf.inputs['Base Color'], tex_image.outputs['Color'])
# Connect Image Node DIRECTLY to Material Output
material_output = mat.node_tree.nodes['Material Output']
material_output.location = (453.1165466308594, 643.1837768554688)
mat.node_tree.links.new(material_output.inputs['Surface'], tex_image.outputs['Color'])
# Apply the material to the object
obj.data.materials.append(mat)
# Rename the UV map
#uv_layer = obj.data.uv_layers.active
#uv_layer.name = name + "_uvmap"
# Update the view layer to ensure transformations are applied
bpy.context.view_layer.update()
return obj, width * scale_factor, height * scale_factor
def create_matching_camera(image_plane_obj, width, height, distance=1):
im_name = image_plane_obj.name + "_camera_view"
cam_data = bpy.data.cameras.new(name=im_name)
cam_data.type = 'ORTHO'
cam_data.ortho_scale = max(width, height) / 5
cam_obj = bpy.data.objects.new(name=im_name, object_data=cam_data)
bpy.context.collection.objects.link(cam_obj)
cam_obj.location = (0, 0, distance)
cam_obj.rotation_euler = (0, 0, 0)
scene = bpy.context.scene
scene.render.resolution_x = int(width / 0.01) # Converting back to original resolution
scene.render.resolution_y = int(height / 0.01) # Converting back to original resolution
# Set the camera as the main (active) camera
bpy.context.scene.camera = cam_obj
return cam_obj
def switch_to_camera_view(camera_obj):
# Set the camera as the active camera for the scene
bpy.context.scene.camera = camera_obj
# Iterate over all the areas in the current screen to find the VIEW_3D areas
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
# Override the context for each 3D view area
with bpy.context.temp_override(area=area):
space = area.spaces.active
space.region_3d.view_perspective = 'CAMERA'
def get_image_from_selected_object(selected_object):
if selected_object.type == 'MESH' and selected_object.active_material:
nodes = selected_object.active_material.node_tree.nodes
for node in nodes:
if node.type == 'TEX_IMAGE':
return node.image
return None
def get_image_from_photostack_group(obj):
"""Retrieve the first image from the '_photostack' node group if available."""
if not obj or not obj.active_material or not obj.active_material.node_tree:
return None
node_tree = obj.active_material.node_tree
for node in node_tree.nodes:
if node.type == 'GROUP' and node.node_tree and '_photostack' in node.node_tree.name:
# Search for the first image node within the group
for subnode in node.node_tree.nodes:
if subnode.type == 'TEX_IMAGE' and subnode.image:
return subnode.image
return None
def move_object_to_collection(obj, collection_name):
# Get the collection or create it if it doesn't exist
collection = bpy.data.collections.get(collection_name)
if not collection:
collection = bpy.data.collections.new(collection_name)
bpy.context.scene.collection.children.link(collection)
# Unlink the object from all its current collections
for coll in obj.users_collection:
coll.objects.unlink(obj)
# Link the object to the new collection
collection.objects.link(obj)
### new function to move to new scene from name of active image in image editor or selected object active image node
def create_scene_based_on_active_image(selected_object=None):
active_image = None
# If a selected object is provided, try to get its texture image
if selected_object:
active_image = get_image_from_selected_object(selected_object)
# If no active image is found, try to get the primary image from the `_photostack` group node
if not active_image and selected_object:
active_image = get_image_from_photostack_group(selected_object)
# If still no image is found, try the image editor (as a last resort)
if not active_image:
try:
active_image = get_active_image_from_image_editor()
except ValueError as e:
print(e)
# If an image is found, create a new scene based on the image's name
if active_image:
image_name = active_image.name
new_scene = bpy.data.scenes.new(name=image_name)
# Link the selected object to the new scene (only if an object is provided)
if selected_object:
new_scene.collection.objects.link(selected_object)
# Switch to the new scene
bpy.context.window.scene = new_scene
# Set to transparent for the world
new_scene.render.film_transparent = True
# Unlock object selection for multiple objects
new_scene.tool_settings.lock_object_mode = False
# Set the render engine based on Blender version
if bpy.app.version >= (4, 2, 0):
new_scene.render.engine = 'BLENDER_EEVEE_NEXT'
else:
new_scene.render.engine = 'BLENDER_EEVEE'
# Set color management settings
paint_view_color_management_settings()
# Copy the World shader from the original scene to the new scene
bpy.context.scene.world = bpy.data.worlds['World']
print(f"New scene created: {image_name}")
else:
print("No active image found to create a new scene.")
def export_uv_layout(obj, filepath):
# Ensure the object is active and selected
bpy.context.view_layer.objects.active = obj
obj.select_set(True)
# Ensure the correct context is passed using context override
with bpy.context.temp_override(object=obj):
# Switch to Edit Mode
bpy.ops.object.mode_set(mode="EDIT")
# Select all faces
bpy.ops.mesh.select_all(action='SELECT')
# Export the UV layout to the given filepath
bpy.ops.uv.export_layout(filepath=filepath, mode='PNG', opacity=0)
# Switch back to Object Mode
bpy.ops.object.mode_set(mode='OBJECT')
def set_camera_background_image(camera_obj, filepath):
if not os.path.exists(filepath):
raise FileNotFoundError(f"File not found: {filepath}")
img = bpy.data.images.load(filepath)
camera_obj.data.show_background_images = True
bg = camera_obj.data.background_images.new()
bg.image = img
bg.alpha = 1.0
bg.display_depth = 'FRONT'
# needed to swap windows to get active image to work here
def get_active_image_from_image_editor():
# Try to get the active image from the Image Editor if it's already open
for area in bpy.context.screen.areas:
if area.type == 'IMAGE_EDITOR':
for space in area.spaces:
if space.type == 'IMAGE_EDITOR' and space.image:
return space.image
# If no Image Editor is found, dynamically open one and check for an image
image_found = False
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
# Temporarily switch the 3D Viewport to an Image Editor
old_type = area.type
area.type = 'IMAGE_EDITOR'
for space in area.spaces:
if space.type == 'IMAGE_EDITOR' and space.image:
image_found = True
active_image = space.image
break
# Restore the area type back to VIEW_3D
area.type = old_type
if image_found:
return active_image
# If no image is found in the Image Editor, search in the Shader Editor
for area in bpy.context.screen.areas:
if area.type == 'NODE_EDITOR':
for space in area.spaces:
if space.type == 'NODE_EDITOR':
if space.node_tree and space.shader_type == 'OBJECT':
for node in space.node_tree.nodes:
if node.type == 'TEX_IMAGE' and node.image:
return node.image
# If no image found in either Image or Shader Editors
raise ValueError("No active image found in the Image Editor or Shader Editor")
def increment_filename(filepath):
directory, filename = os.path.split(filepath)
name, ext = os.path.splitext(filename)
match = re.match(r"^(.*?)(\d+)$", name)
if match:
base_name = match.group(1)
number = int(match.group(2))
new_name = f"{base_name}{number + 1:04d}{ext}"
else:
base_name = name
new_name = f"{base_name}_0001{ext}"
new_filepath = os.path.join(directory, new_name)
while os.path.exists(new_filepath):
match = re.match(r"^(.*?)(\d+)$", base_name)
if match:
base_name = match.group(1)
number = int(match.group(2))
base_name = f"{base_name}{number + 1:04d}"
else:
base_name = base_name + "_0001"
new_name = f"{base_name}{ext}"
new_filepath = os.path.join(directory, new_name)
return new_filepath
def save_incremental_copy(image):
if image.packed_file:
print(f"Unpacking image {image.name}")
image.unpack(method='USE_ORIGINAL')
filepath = bpy.path.abspath(image.filepath)
new_filepath = increment_filename(filepath)
image.save_render(new_filepath)
print(f"Image saved as {new_filepath}")
def find_brush(context):
tool_settings = context.tool_settings
if context.mode == 'SCULPT':
return tool_settings.sculpt.brush
elif context.mode == 'PAINT_TEXTURE':
return tool_settings.image_paint.brush
elif context.mode == 'PAINT_VERTEX':
return tool_settings.vertex_paint.brush
else:
return None
### color calculations for palettes of color families
def rgb_to_hex(color):
return "#{:02x}{:02x}{:02x}".format(int(color[0] * 255), int(color[1] * 255), int(color[2] * 255))
def complementary_color(color):
return 1.0 - color[0], 1.0 - color[1], 1.0 - color[2]
def split_complementary_colors(color):
h, s, v = colorsys.rgb_to_hsv(color[0], color[1], color[2])
split1 = (h + 150.0 / 360.0) % 1.0
split2 = (h - 150.0 / 360.0) % 1.0
rgb1 = colorsys.hsv_to_rgb(split1, s, v)
rgb2 = colorsys.hsv_to_rgb(split2, s, v)
return rgb1, rgb2
def triadic_colors(color):
h, s, v = colorsys.rgb_to_hsv(color[0], color[1], color[2])
triad1 = (h + 120.0 / 360.0) % 1.0
triad2 = (h - 120.0 / 360.0) % 1.0
rgb1 = colorsys.hsv_to_rgb(triad1, s, v)
rgb2 = colorsys.hsv_to_rgb(triad2, s, v)
return rgb1, rgb2
def tetradic_colors(color):
h, s, v = colorsys.rgb_to_hsv(color[0], color[1], color[2])
tetrad1 = (h + 90.0 / 360.0) % 1.0
tetrad2 = (h + 180.0 / 360.0) % 1.0
tetrad3 = (h - 90.0 / 360.0) % 1.0
rgb1 = colorsys.hsv_to_rgb(tetrad1, s, v)
rgb2 = colorsys.hsv_to_rgb(tetrad2, s, v)
rgb3 = colorsys.hsv_to_rgb(tetrad3, s, v)
return rgb1, rgb2, rgb3
def analogous_colors(color):
h, s, v = colorsys.rgb_to_hsv(color[0], color[1], color[2])
ana1 = (h + 30.0 / 360.0) % 1.0
ana2 = (h - 30.0 / 360.0) % 1.0
rgb1 = colorsys.hsv_to_rgb(ana1, s, v)
rgb2 = colorsys.hsv_to_rgb(ana2, s, v)
return rgb1, rgb2
def create_palette(name, colors):
if name in bpy.data.palettes:
palette = bpy.data.palettes[name]
palette.colors.clear()
else:
palette = bpy.data.palettes.new(name=name)
# Add white and black to the palette
white = (1.0, 1.0, 1.0)
black = (0.0, 0.0, 0.0)
# Add the primary colors
colors = [white, black] + colors
for color in colors:
palette_color = palette.colors.new()
palette_color.color = color
# need for setting selection on toggle
def select_object_by_suffix(suffix):
"""Selects an object whose name ends with the given suffix in Blender and switches modes."""
# Ensure we are in Object Mode first
if bpy.context.object and bpy.context.object.mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT')
# Deselect all objects
bpy.ops.object.select_all(action='DESELECT')
# Iterate through all objects in the current view layer
for obj in bpy.context.view_layer.objects:
if obj.name.endswith(suffix):
obj.select_set(True)
bpy.context.view_layer.objects.active = obj # Set as active object
break
else:
# If no object with the suffix was found in the current view layer
print(f"No object with suffix '{suffix}' found in the current view layer.")
return {'CANCELLED'}
# Switch to Texture Paint Mode after selecting the object
bpy.ops.object.mode_set(mode='TEXTURE_PAINT')
return {'FINISHED'}