forked from franMarz/TexTools-Blender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
op_color_from_directions.py
206 lines (162 loc) · 5.89 KB
/
op_color_from_directions.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
import bpy
import bmesh
import operator
from mathutils import Vector
from collections import defaultdict
from math import pi
from . import utilities_color
class op(bpy.types.Operator):
bl_idname = "uv.textools_color_from_directions"
bl_label = "Color Directions"
bl_description = "Assign a color ID to different face directions"
bl_options = {'REGISTER', 'UNDO'}
directions : bpy.props.EnumProperty(items=
[('2', '2', 'Top & Bottom, Sides'),
('3', '3', 'Top & Bottom, Left & Right, Front & Back'),
('4', '4', 'Top, Left & Right, Front & Back, Bottom'),
('6', '6', 'All sides')],
name = "Directions",
default = '3'
)
def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_props_dialog(self)
# def draw(self, context):
# layout = self.layout
# layout.prop(self, "directions")
@classmethod
def poll(cls, context):
if not bpy.context.active_object:
return False
if bpy.context.active_object not in bpy.context.selected_objects:
return False
if len(bpy.context.selected_objects) != 1:
return False
if bpy.context.active_object.type != 'MESH':
return False
#Only in UV editor mode
if bpy.context.area.type != 'IMAGE_EDITOR':
return False
return True
def execute(self, context):
color_elements(self, context)
return {'FINISHED'}
def color_elements(self, context):
obj = bpy.context.active_object
# Setup Edit & Face mode
if obj.mode != 'EDIT':
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='FACE')
# Collect groups
bm = bmesh.from_edit_mesh(bpy.context.active_object.data);
face_directions = {
'top':[],
'bottom':[],
'left':[],
'right':[],
'front':[],
'back':[]
}
print("Directions {}".format(self.directions))
for face in bm.faces:
print("face {} n: {}".format(face.index, face.normal))
# Find dominant direction
abs_x = abs(face.normal.x)
abs_y = abs(face.normal.y)
abs_z = abs(face.normal.z)
max_xyz = max(abs_x, abs_y, abs_z)
if max_xyz == abs_x:
if face.normal.x > 0:
face_directions['right'].append(face.index)
else:
face_directions['left'].append(face.index)
elif max_xyz == abs_y:
if face.normal.y > 0:
face_directions['front'].append(face.index)
else:
face_directions['back'].append(face.index)
elif max_xyz == abs_z:
if face.normal.z > 0:
face_directions['top'].append(face.index)
else:
face_directions['bottom'].append(face.index)
count = int(self.directions)
bpy.context.scene.texToolsSettings.color_ID_count = count
groups = []
# for i in range(count):
# groups.append([])
if self.directions == '2':
groups.append(face_directions['top']+face_directions['bottom'])
groups.append(face_directions['left']+face_directions['right']+face_directions['front']+face_directions['back'])
if self.directions == '3':
groups.append(face_directions['top']+face_directions['bottom'])
groups.append(face_directions['left']+face_directions['right'])
groups.append(face_directions['front']+face_directions['back'])
elif self.directions == '4':
groups.append(face_directions['top'])
groups.append(face_directions['left']+face_directions['right'])
groups.append(face_directions['front']+face_directions['back'])
groups.append(face_directions['bottom'])
elif self.directions == '6':
groups.append(face_directions['top'])
groups.append(face_directions['right'])
groups.append(face_directions['left'])
groups.append(face_directions['front'])
groups.append(face_directions['back'])
groups.append(face_directions['bottom'])
# Assign Groups to colors
index_color = 0
for group in groups:
# # rebuild bmesh data (e.g. left edit mode previous loop)
bm = bmesh.from_edit_mesh(bpy.context.active_object.data);
if hasattr(bm.faces, "ensure_lookup_table"):
bm.faces.ensure_lookup_table()
# Select group
bpy.ops.mesh.select_all(action='DESELECT')
for index_face in group:
bm.faces[index_face].select = True
# Assign to selection
bpy.ops.uv.textools_color_assign(index=index_color)
index_color = (index_color+1) % bpy.context.scene.texToolsSettings.color_ID_count
bpy.ops.object.mode_set(mode='OBJECT')
utilities_color.validate_face_colors(obj)
'''
faces_indices_processed = []
for face in bm.faces:
if face.index not in faces_indices_processed:
# Select face & extend
bpy.ops.mesh.select_all(action='DESELECT')
face.select = True
bpy.ops.mesh.select_linked(delimit={'NORMAL'})
faces = [f.index for f in bm.faces if (f.select and f.index not in faces_indices_processed)]
for f in faces:
faces_indices_processed.append(f)
groups.append(faces)
# Assign color count (caps automatically e.g. max 20)
bpy.context.scene.texToolsSettings.color_ID_count = len(groups)
gamma = 2.2
for i in range(bpy.context.scene.texToolsSettings.color_ID_count):
color = utilities_color.get_color_id(i, bpy.context.scene.texToolsSettings.color_ID_count)
# Fix Gamma
color[0] = pow(color[0] , gamma)
color[1] = pow(color[1] , gamma)
color[2] = pow(color[2], gamma)
utilities_color.set_color(i, color)
# Assign Groups to colors
index_color = 0
for group in groups:
# rebuild bmesh data (e.g. left edit mode previous loop)
bm = bmesh.from_edit_mesh(bpy.context.active_object.data);
if hasattr(bm.faces, "ensure_lookup_table"):
bm.faces.ensure_lookup_table()
# Select group
bpy.ops.mesh.select_all(action='DESELECT')
for index_face in group:
bm.faces[index_face].select = True
# Assign to selection
bpy.ops.uv.textools_color_assign(index=index_color)
index_color = (index_color+1) % bpy.context.scene.texToolsSettings.color_ID_count
bpy.ops.object.mode_set(mode='OBJECT')
utilities_color.validate_face_colors(obj)
'''
bpy.utils.register_class(op)