-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnightsMappingTools.py
467 lines (360 loc) · 13.9 KB
/
nightsMappingTools.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
import bpy
import bpy.utils.previews
import bmesh
import os
from math import cos, sin, radians
from random import randint
# For custom icons.
icons_dict = bpy.utils.previews.new()
# When using as addon
#icons_dir = os.path.join(os.path.dirname(__file__), "icons")
# When using in the editor:
script_path = bpy.context.space_data.text.filepath
icons_dir = os.path.join(os.path.dirname(script_path), "icons")
# Keep track of created materials.
materials_dict = {}
# Load icons :)
iconFilenames = [f for f in os.listdir(icons_dir) if os.path.isfile(os.path.join(icons_dir, f))]
for filename in iconFilenames:
icons_dict.load(os.path.splitext(filename)[0], os.path.join(icons_dir, filename), 'IMAGE')
def getIcon(name):
return icons_dict[name].icon_id
def getSelectedTexture(op, context):
browsers = [x for x in bpy.context.window.screen.areas if x.type == 'FILE_BROWSER']
if len(browsers) == 0:
op.report({"ERROR"}, "You must have a file browser window active")
elif len(browsers) != 1:
op.report({"ERROR"}, "You must have exactly ONE file browser window active")
selection = browsers[0].spaces[0].params
return os.path.join(selection.directory, selection.filename)
def getSelectedObject(op, context):
objs = bpy.context.selected_editable_objects
if len(objs) != 1:
activeObj = bpy.context.active_object
if activeObj == None:
op.report({"ERROR"}, "You must have a face selected inside an editable object")
else:
objs = [ activeObj ]
return objs[0]
# Register existing materials.
def findExistingMaterial(textureFilename):
for mat in bpy.data.materials:
for texture_slot in mat.texture_slots:
if texture_slot == None or not hasattr(texture_slot, 'texture'):
continue
texture = texture_slot.texture
if not hasattr(texture, 'image') or texture.image is None:
continue
if not hasattr(texture.image, 'filepath') or texture.image.filepath is None:
continue
if texture.image.filepath == textureFilename:
return mat
return None
def getFaceMaterial(textureFilename, hasAlpha, object):
textureFile = os.path.split(textureFilename)[1]
# Try to find the material in the local list
materialName = "FaceMaterial_{}".format(os.path.splitext(textureFile)[0])
if materialName in materials_dict:
print("Using existing material {}".format(materialName))
return materials_dict[ materialName ]
existingMaterial = findExistingMaterial(textureFilename)
if existingMaterial is not None:
materials_dict[ materialName ] = existingMaterial
print("Registered existing material {} as {} for mapping.".format(existingMaterial.name, materialName))
return existingMaterial
# Couldn't find material anywhere, register a new one.
newMaterial = bpy.data.materials.new(materialName)
newMaterial.use_shadeless = True
newMaterial.use_transparency = hasAlpha
newMaterial.transparency_method = 'Z_TRANSPARENCY'
newMaterial.alpha = 0
newTextureName = "FaceTexture_{}".format(os.path.splitext(textureFile)[0])
newTexture = bpy.data.textures.new(newTextureName, "IMAGE")
newTexture.image = bpy.data.images.load(textureFilename)
newMaterial.texture_slots.add()
newMaterial.texture_slots[0].texture = newTexture
newMaterial.texture_slots[0].use_map_alpha = hasAlpha
newMaterial.texture_slots[0].alpha_factor = 1.0
materials_dict[ materialName ] = newMaterial
print("Registered new material for mapping: {}".format(materialName))
return newMaterial
def doApplyBrowserTextureToFace(hasAlpha, op, context):
textureFilename = getSelectedTexture(op, context)
object = getSelectedObject(op, context)
if len(object.data.uv_layers) == 0:
bpy.ops.mesh.uv_texture_add()
material = getFaceMaterial(textureFilename, hasAlpha, object)
if material.name not in object.data.materials:
object.data.materials.append(material)
materialIndex = object.data.materials.keys().index(material.name)
mesh = bmesh.from_edit_mesh(object.data)
selectedFaces = [f for f in mesh.faces if f.select]
if len(selectedFaces) == 0:
op.report({"ERROR"}, "You must have a face selected inside an editable object")
# Deselect
for f in mesh.faces:
f.select = False
# Create UV
for f in selectedFaces:
f.select = True
bpy.ops.uv.unwrap()
bpy.ops.uv.reset()
f.material_index = materialIndex
f.select = False
# Reselect
for f in selectedFaces:
f.select = True
bmesh.update_edit_mesh(object.data)
def transformUV(function, op, context):
print("Rotating uv...")
object = getSelectedObject(op, context)
mesh = bmesh.from_edit_mesh(object.data)
uvRef = mesh.loops.layers.uv.active
selectedFaces = [f for f in mesh.faces if f.select]
if len(selectedFaces) == 0:
op.report({"ERROR"}, "You must have a face selected inside an editable object")
for f in selectedFaces:
for loop in f.loops:
print("Coord {}".format(loop[uvRef].uv))
loop[uvRef].uv = function(loop[uvRef].uv)
print("Transformed Coord {}".format(loop[uvRef].uv))
bmesh.update_edit_mesh(object.data)
def rotateBy(angle, point, anchor):
cAngle = cos(radians(angle))
sAngle = sin(radians(angle))
x,y = point[0] - anchor[0], point[1] - anchor[1]
return (x * cAngle - sAngle * y + anchor[0],
sAngle * x + cAngle * y + anchor[1])
centerAnchor = (0.5, 0.5)
def doRotateUV(op, context):
func = lambda uv: rotateBy(90, uv, centerAnchor)
transformUV(func, op, context)
def doScaleUVHelper(uv, scale, anchor):
x, y = uv[0] - anchor[0], uv[1] - anchor[1]
x, y = x * scale[0] + anchor[0], y * scale[1] + anchor[1]
return (x,y)
def doShrinkUV(op, context):
func = lambda uv: doScaleUVHelper(uv, (1.1, 1.1), centerAnchor)
transformUV(func, op, context)
def doShrinkHorizontalUV(op, context):
func = lambda uv: doScaleUVHelper(uv, (1.1, 1.0), centerAnchor)
transformUV(func, op, context)
def doShrinkVerticalUV(op, context):
func = lambda uv: doScaleUVHelper(uv, (1.0, 1.1), centerAnchor)
transformUV(func, op, context)
def doExpandUV(op, context):
func = lambda uv: doScaleUVHelper(uv, (0.9, 0.9), centerAnchor)
transformUV(func, op, context)
def doExpandHorizontalUV(op, context):
func = lambda uv: doScaleUVHelper(uv, (0.9, 1.0), centerAnchor)
transformUV(func, op, context)
def doExpandVerticalUV(op, context):
func = lambda uv: doScaleUVHelper(uv, (1.0, 0.9), centerAnchor)
transformUV(func, op, context)
def doMoveUVHelper(uv, move):
x, y = uv[0] + move[0], uv[1] + move[1]
return (x,y)
def doMoveUV_Up(op, context):
func = lambda uv: doMoveUVHelper(uv, (0, 0.1))
transformUV(func, op, context)
def doMoveUV_Down(op, context):
func = lambda uv: doMoveUVHelper(uv, (0, -0.1))
transformUV(func, op, context)
def doMoveUV_Left(op, context):
func = lambda uv: doMoveUVHelper(uv, (-0.1, 0))
transformUV(func, op, context)
def doMoveUV_Right(op, context):
func = lambda uv: doMoveUVHelper(uv, (0.1, 0))
transformUV(func, op, context)
class ApplyTextureFaceOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.apply_texture_face_operator"
bl_label = "Selected Texture to Face"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
doApplyBrowserTextureToFace(False, self, context)
return {'FINISHED'}
class ApplyTextureFaceAlphaOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.apply_texture_face_alpha_operator"
bl_label = "Selected Texture to Face Alpha"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
doApplyBrowserTextureToFace(True, self, context)
return {'FINISHED'}
class RotateUVOperator(bpy.types.Operator):
"""Rotate UV"""
bl_idname = "object.rotate_uv_operator"
bl_label = "Rotate UV 90º"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
doRotateUV(self, context)
return {'FINISHED'}
class ShrinkUVOperator(bpy.types.Operator):
"""Shrink UV"""
bl_idname = "object.shrink_uv_operator"
bl_label = "Shrink UV"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
doShrinkUV(self, context)
return {'FINISHED'}
class ShrinkUVHorizontalOperator(bpy.types.Operator):
"""Shrink UV Horizontally"""
bl_idname = "object.shrink_uv_horizontal_operator"
bl_label = "Shrink UV Horizontally"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
doShrinkHorizontalUV(self, context)
return {'FINISHED'}
class ShrinkUVVerticalOperator(bpy.types.Operator):
"""Shrink UV Vertically"""
bl_idname = "object.shrink_uv_vertical_operator"
bl_label = "Shrink UV Vertically"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
doShrinkVerticalUV(self, context)
return {'FINISHED'}
class ExpandUVOperator(bpy.types.Operator):
"""Expand UV"""
bl_idname = "object.expand_uv_operator"
bl_label = "Expand UV"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
doExpandUV(self, context)
return {'FINISHED'}
class MoveUVUpOperator(bpy.types.Operator):
"""Expand UV"""
bl_idname = "object.move_uv_up_operator"
bl_label = "Move UV Up"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
doMoveUV_Up(self, context)
return {'FINISHED'}
class MoveUVDownOperator(bpy.types.Operator):
"""Expand UV"""
bl_idname = "object.move_uv_down_operator"
bl_label = "Move UV Down"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
doMoveUV_Down(self, context)
return {'FINISHED'}
class MoveUVLeftOperator(bpy.types.Operator):
"""Expand UV"""
bl_idname = "object.move_uv_left_operator"
bl_label = "Move UV Left"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
doMoveUV_Left(self, context)
return {'FINISHED'}
class MoveUVRightOperator(bpy.types.Operator):
"""Expand UV"""
bl_idname = "object.move_uv_right_operator"
bl_label = "Move UV Right"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
doMoveUV_Right(self, context)
return {'FINISHED'}
class ExpandUVHorizontalOperator(bpy.types.Operator):
"""Expand UV Horizontally"""
bl_idname = "object.expand_uv_horizontal_operator"
bl_label = "Expand UV Horizontally"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
doExpandHorizontalUV(self, context)
return {'FINISHED'}
class ExpandUVVerticalOperator(bpy.types.Operator):
"""Expand UV Vertically"""
bl_idname = "object.expand_uv_vertical_operator"
bl_label = "Expand UV Vertically"
@classmethod
def poll(cls, context):
return context.active_object is not None
def execute(self, context):
doExpandVerticalUV(self, context)
return {'FINISHED'}
class NightzMapperToolsPanel(bpy.types.Panel):
"""Creates a Panel in the Object properties window"""
bl_label = "Nightz Mapper Tools"
bl_idname = "OBJECT_PT_hello"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_context = "object"
def draw(self, context):
layout = self.layout
obj = context.object
row = layout.row()
row.label(text="Active object is: " + obj.name)
row = layout.row()
row.operator("object.apply_texture_face_operator")
row = layout.row()
row.operator("object.apply_texture_face_alpha_operator")
row = layout.row()
row.operator("object.rotate_uv_operator", text="Rotate UV", icon_value=getIcon("arrow_rotate_clockwise"))
row = layout.row()
row.operator("object.shrink_uv_operator", text="", icon_value=getIcon("arrow_in"))
row.operator("object.shrink_uv_horizontal_operator", text="", icon_value=getIcon("shrink_horizontal"))
row.operator("object.expand_uv_horizontal_operator", text="", icon_value=getIcon("arrow_grow_horizontal"))
row.operator("object.move_uv_left_operator", text="", icon_value=getIcon("arrow_left"))
row.operator("object.move_uv_right_operator", text="", icon_value=getIcon("arrow_right"))
row = layout.row()
row.operator("object.expand_uv_operator", text="", icon_value=getIcon("arrow_out"))
row.operator("object.shrink_uv_vertical_operator", text="", icon_value=getIcon("shrink_vertical"))
row.operator("object.expand_uv_vertical_operator", text="", icon_value=getIcon("arrow_grow_vertical"))
row.operator("object.move_uv_up_operator", text="", icon_value=getIcon("arrow_down"))
row.operator("object.move_uv_down_operator", text="", icon_value=getIcon("arrow_up"))
def register():
bpy.utils.register_class(ApplyTextureFaceOperator)
bpy.utils.register_class(ApplyTextureFaceAlphaOperator)
bpy.utils.register_class(RotateUVOperator)
bpy.utils.register_class(ShrinkUVOperator)
bpy.utils.register_class(ShrinkUVHorizontalOperator)
bpy.utils.register_class(ShrinkUVVerticalOperator)
bpy.utils.register_class(ExpandUVOperator)
bpy.utils.register_class(ExpandUVHorizontalOperator)
bpy.utils.register_class(ExpandUVVerticalOperator)
bpy.utils.register_class(MoveUVUpOperator)
bpy.utils.register_class(MoveUVDownOperator)
bpy.utils.register_class(MoveUVLeftOperator)
bpy.utils.register_class(MoveUVRightOperator)
bpy.utils.register_class(NightzMapperToolsPanel)
def unregister():
bpy.utils.unregister_class(ApplyTextureFaceOperator)
bpy.utils.unregister_class(ApplyTextureFaceAlphaOperator)
bpy.utils.unregister_class(RotateUVOperator)
bpy.utils.unregister_class(ShrinkUVOperator)
bpy.utils.unregister_class(ShrinkUVHorizontalOperator)
bpy.utils.unregister_class(ShrinkUVVerticalOperator)
bpy.utils.unregister_class(ExpandUVOperator)
bpy.utils.unregister_class(ExpandUVHorizontalOperator)
bpy.utils.unregister_class(ExpandUVVerticalOperator)
bpy.utils.unregister_class(MoveUVUpOperator)
bpy.utils.unregister_class(MoveUVDownOperator)
bpy.utils.unregister_class(MoveUVLeftOperator)
bpy.utils.unregister_class(MoveUVRightOperator)
bpy.utils.unregister_class(NightzMapperToolsPanel)
if __name__ == "__main__":
register()