forked from drewcassidy/TexTools-Blender
-
Notifications
You must be signed in to change notification settings - Fork 1
/
op_texture_preview.py
105 lines (81 loc) · 3.19 KB
/
op_texture_preview.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
import bpy
import bmesh
import operator
import math
from mathutils import Vector
from collections import defaultdict
from . import settings
from . import utilities_color
from . import utilities_bake
material_prefix = "TT_atlas_"
gamma = 2.2
class op(bpy.types.Operator):
bl_idname = "uv.textools_texture_preview"
bl_label = "Preview Texture"
bl_description = "Preview the current UV image view background image on the selected object."
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
if not bpy.context.active_object:
return False
if len(settings.sets) == 0:
return False
# Only when we have a background image
for area in bpy.context.screen.areas:
if area.type == 'IMAGE_EDITOR':
return area.spaces[0].image
return False
def execute(self, context):
print("PREVIEW TEXTURE????")
preview_texture(self, context)
return {'FINISHED'}
def preview_texture(self, context):
# Collect all low objects from bake sets
objects = [
obj for s in settings.sets for obj in s.objects_low if obj.data.uv_layers]
# Get view 3D area
view_area = None
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
view_area = area
# Exit existing local view
# if view_area and view_area.spaces[0].local_view:
# bpy.ops.view3d.localview({'area': view_area})
# return
# Get background image
image = None
for area in bpy.context.screen.areas:
if area.type == 'IMAGE_EDITOR':
image = area.spaces[0].image
break
if image:
material = utilities_bake.get_image_material(image)
for obj in objects:
print("Map {}".format(obj.name))
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
for i in range(len(obj.material_slots)):
bpy.ops.object.material_slot_remove()
# Create material with image
bpy.ops.object.material_slot_add()
obj.material_slots[0].material = material
obj.display_type = 'TEXTURED'
# Re-Select objects
bpy.ops.object.select_all(action='DESELECT')
for obj in objects:
obj.select_set(state=True, view_layer=None)
if view_area:
# Change View mode to TEXTURED
for space in view_area.spaces:
if space.type == 'VIEW_3D':
if bpy.context.scene.render.engine == 'BLENDER_WORKBENCH':
space.shading.type = 'SOLID'
space.shading.color_type = 'TEXTURE'
else:
space.shading.type = 'MATERIAL'
# Enter local view
# bpy.ops.view3d.localview({'area': view_area})
# bpy.ops.ui.textools_popup('INVOKE_DEFAULT', message="Object is in isolated view")
bpy.utils.register_class(op)