-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathop_color_assign.py
104 lines (81 loc) · 3 KB
/
op_color_assign.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
import bpy
from . import utilities_color
from . import utilities_ui
from .settings import tt_settings, prefs
gamma = 2.2
class op(bpy.types.Operator):
bl_idname = "uv.textools_color_assign"
bl_label = "Assign Color"
bl_description = "Assign color to selected Objects or faces in Edit Mode"
bl_options = {'UNDO'}
index: bpy.props.IntProperty(description="Color Index", default=0)
@classmethod
def poll(cls, context):
if bpy.context.area.ui_type != 'UV':
return False
if not bpy.context.active_object:
return False
if bpy.context.active_object not in bpy.context.selected_objects:
return False
if bpy.context.active_object.type != 'MESH':
return False
return True
def execute(self, context):
assign_color(self, context, self.index)
return {'FINISHED'}
def assign_color(self, context, index):
selected_obj = bpy.context.selected_objects.copy()
previous_mode = 'OBJECT'
if len(selected_obj) == 1:
previous_mode = bpy.context.active_object.mode
for obj in selected_obj:
# Select object
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
# Enter Edit mode
bpy.ops.object.mode_set(mode='EDIT')
if previous_mode == 'OBJECT':
bpy.ops.mesh.select_all(action='SELECT')
if tt_settings().color_assign_mode == 'MATERIALS':
# Verify material slots
for _ in range(index+1):
if index >= len(obj.material_slots):
bpy.ops.object.material_slot_add()
utilities_color.assign_slot(obj, index)
# Assign to selection
obj.active_material_index = index
bpy.ops.object.material_slot_assign()
else: # mode == VERTEXCOLORS
color = utilities_color.get_color(index).copy()
if prefs().bool_color_id_vertex_color_gamma:
# Fix Gamma
color[0] = pow(color[0], 1/gamma)
color[1] = pow(color[1], 1/gamma)
color[2] = pow(color[2], 1/gamma)
# Manage Vertex Color layer
context_override = utilities_ui.GetContextView3D()
if not context_override:
self.report({'ERROR_INVALID_INPUT'}, "This tool requires an available View3D view.")
return {'CANCELLED'}
if 'TexTools_colorID' not in obj.data.vertex_colors:
obj.data.vertex_colors.new(name='TexTools_colorID')
obj.data.vertex_colors['TexTools_colorID'].active = True
# Paint
bpy.ops.object.mode_set(mode='VERTEX_PAINT')
bpy.context.tool_settings.vertex_paint.brush.color = color
bpy.context.object.data.use_paint_mask = True
with bpy.context.temp_override(**context_override):
bpy.ops.paint.vertex_color_set()
bpy.context.object.data.use_paint_mask = False
# restore mode
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
for obj in selected_obj:
obj.select_set(True)
bpy.ops.object.mode_set(mode=previous_mode)
# Show Material or Data Tab
utilities_color.update_properties_tab()
# Change View mode
utilities_color.update_view_mode()