-
Notifications
You must be signed in to change notification settings - Fork 63
/
DUV_ApplyMaterial.py
59 lines (47 loc) · 1.94 KB
/
DUV_ApplyMaterial.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
import bpy
import bmesh
import math
from mathutils import Vector
from . import DUV_Utils
class DREAMUV_OT_apply_material(bpy.types.Operator):
"""Unwrap and attempt to fit to a square shape"""
bl_idname = "view3d.dreamuv_apply_material"
bl_label = "unwrap to square shape if possible"
def execute(self, context):
#check for object or edit mode:
objectmode = False
if bpy.context.object.mode == 'OBJECT':
objectmode = True
#switch to edit and select all
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')
obj = bpy.context.view_layer.objects.active
bm = bmesh.from_edit_mesh(obj.data)
uv_layer = bm.loops.layers.uv.verify()
HSfaces = list()
#MAKE FACE LIST
for face in bm.faces:
if face.select:
HSfaces.append(face)
#ADD MATERIAL
#check if we want to add material, then check if it needs to be added, and keep index for later
if context.scene.duv_hotspotmaterial is not None:
matindex = 0
doesmatexist = False
for m in obj.data.materials:
if m == context.scene.duv_hotspotmaterial:
doesmatexist = True
break
matindex += 1
if doesmatexist is False:
obj.data.materials.append(context.scene.duv_hotspotmaterial)
#apply material from index
if context.scene.duv_hotspotmaterial is not None:
for face in HSfaces:
face.material_index = matindex
bmesh.update_edit_mesh(obj.data)
bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='FACE')
if objectmode is True:
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.editmode_toggle()
return {'FINISHED'}