-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathutilities_color.py
182 lines (142 loc) · 5.37 KB
/
utilities_color.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
import bpy
import bmesh
from mathutils import Color
from .settings import tt_settings
material_prefix = "TT_color_"
gamma = 2.2
def assign_slot(obj, index):
if index < len(obj.material_slots):
obj.material_slots[index].material = get_material(index)
assign_color(index) # Verify color
def safe_color(color):
if len(color) == 3:
return *color, 1
return color
def assign_color(index):
material = get_material(index)
if material:
rgba = (*get_color(index), 1)
for n in material.node_tree.nodes:
if n.bl_idname == "ShaderNodeBsdfPrincipled":
n.inputs[0].default_value = rgba
material.diffuse_color = rgba
def get_material(index):
name = get_name(index)
# Material already exists?
if name in bpy.data.materials:
material = bpy.data.materials[name]
return material
material = create_material(index)
assign_color(index)
return material
def create_material(index):
name = get_name(index)
material = bpy.data.materials.new(name)
material.preview_render_type = 'FLAT'
material.use_nodes = True
return material
def get_name(index):
return f"{material_prefix}{index:02d}"
def get_color(index):
if index < tt_settings().color_ID_count:
return getattr(tt_settings(), f"color_ID_color_{index}")
return 0, 0, 0 # Default return (Black)
def set_color(index, color):
if index < tt_settings().color_ID_count:
setattr(tt_settings(), f"color_ID_color_{index}", color)
def validate_face_colors(obj):
# Validate face colors and material slots
previous_mode = bpy.context.object.mode
count = tt_settings().color_ID_count
# Verify enough material slots
if len(obj.material_slots) < count:
for i in range(count):
if len(obj.material_slots) < count:
bpy.ops.object.material_slot_add()
assign_slot(obj, len(obj.material_slots)-1)
else:
break
# TODO: Check face.material_index
bpy.ops.object.mode_set(mode='EDIT')
bm = bmesh.from_edit_mesh(obj.data)
for face in bm.faces:
face.material_index %= count
obj.data.update()
# Remove material slots that are not used
if len(obj.material_slots) > count:
bpy.ops.object.mode_set(mode='OBJECT')
for i in range(len(obj.material_slots) - count):
if len(obj.material_slots) > count:
# Remove last
bpy.context.object.active_material_index = len(obj.material_slots)-1
bpy.ops.object.material_slot_remove()
# Restore previous mode
bpy.ops.object.mode_set(mode=previous_mode)
def hex_to_color(hex):
hex = hex.strip('#')
lv = len(hex)
fin = list(int(hex[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
r = pow(fin[0] / 255, gamma)
g = pow(fin[1] / 255, gamma)
b = pow(fin[2] / 255, gamma)
fin.clear()
fin.append(r)
fin.append(g)
fin.append(b)
return tuple(fin)
def color_to_hex(color):
rgb = []
for i in range(3):
rgb.append(pow(color[i], 1.0/gamma))
r = int(rgb[0]*255)
g = int(rgb[1]*255)
b = int(rgb[2]*255)
return f"#{r:02X}{g:02X}{b:02X}"
def get_color_id(index, count, jitter=False):
# Get unique color
color = Color()
indexList = [0, 171, 64, 213, 32, 96, 160, 224, 16, 48, 80, 112, 144, 176, 208, 240, 8, 24, 40, 56, 72, 88, 104,
120, 136, 152, 168, 184, 200, 216, 232, 248, 4, 12, 20, 28, 36, 44, 52, 60, 68, 76, 84, 92, 100, 108, 116, 124,
132, 140, 148, 156, 164, 172, 180, 188, 196, 204, 212, 220, 228, 236, 244, 252, 2, 6, 10, 14, 18, 22, 26, 30, 34,
38, 42, 46, 50, 54, 58, 62, 66, 70, 74, 78, 82, 86, 90, 94, 98, 102, 106, 110, 114, 118, 122, 126, 130, 134, 138,
142, 146, 150, 154, 158, 162, 166, 170, 174, 178, 182, 186, 190, 194, 198, 202, 206, 210, 214, 218, 222, 226, 230,
234, 238, 242, 246, 250, 254, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43,
45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101,
103, 105, 107, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147,
149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 128, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193,
195, 197, 199, 201, 203, 205, 207, 209, 211, 192, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, 237, 239,
241, 243, 245, 247, 249, 251, 253, 255]
i = 0
if index > 255:
while index > 255:
index -= 256
i += 1
if jitter:
color.hsv = ( ( indexList[index] + 1/(2**i) ) / 256 ), 0.9, 1.0
else:
color.hsv = ( index / (count) ), 0.9, 1.0
return color
def update_properties_tab():
for area in bpy.context.screen.areas:
if area.type == 'PROPERTIES':
for space in area.spaces:
if space.type == 'PROPERTIES':
if tt_settings().color_assign_mode == 'MATERIALS':
space.context = 'MATERIAL'
else: #mode == VERTEXCOLORS
space.context = 'DATA'
def update_view_mode():
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
for space in area.spaces:
if space.type == 'VIEW_3D':
if space.shading.type == 'RENDERED':
continue
elif space.shading.type == 'MATERIAL' and tt_settings().color_assign_mode == 'MATERIALS':
continue
space.shading.type = 'SOLID'
if tt_settings().color_assign_mode == 'MATERIALS':
if space.shading.color_type != 'TEXTURE':
space.shading.color_type = 'MATERIAL'
else: #mode == VERTEXCOLORS
space.shading.color_type = 'VERTEX'