forked from drewcassidy/TexTools-Blender
-
Notifications
You must be signed in to change notification settings - Fork 1
/
op_texel_checker_map.py
250 lines (181 loc) · 7.47 KB
/
op_texel_checker_map.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
import bpy
import os
import bmesh
import operator
from mathutils import Vector
from collections import defaultdict
from math import pi
from . import utilities_texel
texture_modes = ['UV_GRID','COLOR_GRID','GRAVITY','NONE']
class op(bpy.types.Operator):
bl_idname = "uv.textools_texel_checker_map"
bl_label = "Checker Map"
bl_description = "Add a checker map to the selected model and UV view"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
if len(get_valid_objects()) == 0:
return False
return True
def execute(self, context):
assign_checker_map(
bpy.context.scene.texToolsSettings.size[0],
bpy.context.scene.texToolsSettings.size[1]
)
return {'FINISHED'}
def assign_checker_map(size_x, size_y):
# Force Object mode
if bpy.context.view_layer.objects.active != None and bpy.context.object.mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT')
# Collect Objects
objects = get_valid_objects()
if len(objects) == 0:
self.report({'ERROR_INVALID_INPUT'}, "No UV mapped objects selected" )
#Change View mode to TEXTURED
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
for space in area.spaces:
if space.type == 'VIEW_3D':
space.shading.type = 'MATERIAL'
if len(objects) > 0:
# Detect current Checker modes
mode_count = {}
for mode in texture_modes:
mode_count[mode] = 0
# Image sizes
image_sizes_x = []
image_sizes_y = []
# Collect current modes in selected objects
for obj in objects:
image = utilities_texel.get_object_texture_image(obj)
mode = 'NONE'
if image:
if "GRAVITY" in image.name.upper():
mode = 'GRAVITY'
elif image.generated_type in texture_modes:
# Generated checker maps
mode = image.generated_type
# Track image sizes
if image.size[0] not in image_sizes_x:
image_sizes_x.append(image.size[0])
if image.size[1] not in image_sizes_y:
image_sizes_y.append(image.size[1])
mode_count[mode]+=1
# Sort by count (returns tuple list of key,value)
mode_max_count = sorted(mode_count.items(), key=operator.itemgetter(1))
mode_max_count.reverse()
for key,val in mode_max_count:
print("{} = {}".format(key, val))
# Determine next mode
mode = 'NONE'
if mode_max_count[0][1] == 0:
# There are no maps
mode = texture_modes[0]
elif mode_max_count[0][0] in texture_modes:
if mode_max_count[-1][1] > 0:
# There is more than 0 of another mode, complete existing mode first
mode = mode_max_count[0][0]
else:
# Switch to next checker mode
index = texture_modes.index(mode_max_count[0][0])
if texture_modes[ index ] != 'NONE' and len(image_sizes_x) > 1 or len(image_sizes_y) > 1:
# There are multiple resolutions on selected objects
mode = texture_modes[ index ]
elif texture_modes[ index ] != 'NONE' and (len(image_sizes_x) > 0 and image_sizes_x[0] != size_x) and (len(image_sizes_y) > 0 and image_sizes_y[0] != size_y):
# The selected objects have a different resolution
mode = texture_modes[ index ]
else:
# Next mode
mode = texture_modes[ (index+1)%len(texture_modes) ]
print("Mode: "+mode)
if mode == 'NONE':
for obj in objects:
remove_material(obj)
elif mode == 'GRAVITY':
image = load_image("checker_map_gravity")
for obj in objects:
apply_image(obj, image)
else:
name = utilities_texel.get_checker_name(mode, size_x, size_y)
image = get_image(name, mode, size_x, size_y)
for obj in objects:
apply_image(obj, image)
# Restore object selection
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
for obj in objects:
obj.select_set( state = True, view_layer = None)
# Clean up images and materials
utilities_texel.checker_images_cleanup()
# Force redraw of viewport to update texture
# bpy.context.scene.update()
bpy.context.view_layer.update()
def load_image(name):
pathTexture = icons_dir = os.path.join(os.path.dirname(__file__), "resources/{}.png".format(name))
image = bpy.ops.image.open(filepath=pathTexture, relative_path=False)
if "{}.png".format(name) in bpy.data.images:
bpy.data.images["{}.png".format(name)].name = name #remove extension in name
return bpy.data.images[name];
def get_valid_objects():
# Collect Objects
objects = []
for obj in bpy.context.selected_objects:
if obj.type == 'MESH' and obj.data.uv_layers:
objects.append(obj)
return objects
def remove_material(obj):
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
obj.select_set( state = True, view_layer = None)
bpy.context.view_layer.objects.active = obj
count = len(obj.material_slots)
for i in range(count):
bpy.ops.object.material_slot_remove()
def apply_image(obj, image):
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
obj.select_set( state = True, view_layer = None)
bpy.context.view_layer.objects.active = obj
# Assign Cycles material with image
# Get Material
material = None
if image.name in bpy.data.materials:
material = bpy.data.materials[image.name]
else:
material = bpy.data.materials.new(image.name)
material.use_nodes = True
# Assign material
if len(obj.data.materials) > 0:
obj.data.materials[0] = material
else:
obj.data.materials.append(material)
# Setup Node
tree = material.node_tree
node = None
if "checker" in tree.nodes:
node = tree.nodes["checker"]
else:
node = tree.nodes.new("ShaderNodeTexImage")
node.name = "checker"
node.select = True
tree.nodes.active = node
node.image = image
# LINKANDO:
tree = obj.data.materials[0].node_tree
links = tree.links
nodo1 = tree.nodes['checker']
nodo2 = tree.nodes['Principled BSDF']
links.new(nodo1.outputs['Color'], nodo2.inputs['Base Color'])
def get_image(name, mode, size_x, size_y):
# Image already exists?
if name in bpy.data.images:
# Update texture UV checker mode
bpy.data.images[name].generated_type = mode
return bpy.data.images[name];
# Create new image instead
image = bpy.data.images.new(name, width=size_x, height=size_y)
image.generated_type = mode #UV_GRID or COLOR_GRID
image.generated_width = int(size_x)
image.generated_height = int(size_y)
return image
bpy.utils.register_class(op)