forked from franMarz/TexTools-Blender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
op_texture_preview.py
112 lines (90 loc) · 3.64 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
106
107
108
109
110
111
112
import bpy
import os
from . import settings
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 background Image of the UV Editor as a Material override on the appropriate 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
for area in bpy.context.screen.areas:
if area.ui_type == 'UV':
return area.spaces[0].image
return False
def execute(self, context):
premode = bpy.context.active_object.mode
preview_texture(self, context)
bpy.ops.object.mode_set(mode=premode)
# 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':
if space.shading.type == 'SOLID':
space.shading.color_type = 'TEXTURE'
# Force redraw of viewport to update texture
bpy.context.view_layer.update()
return {'FINISHED'}
def preview_texture(self, context):
# Collect all low objects from bake sets
objects = {ob for s in settings.sets for ob in s.objects_low if ob.data.uv_layers and ob.select_get()}
if objects:
# Get background image
image = None
for area in bpy.context.screen.areas:
if area.ui_type == 'UV':
image = area.spaces[0].image
break
if image:
bpy.ops.object.mode_set(mode='OBJECT')
base_nodegroup = get_override_nodegroup()
base_material = bpy.data.materials.get('TT_material_override')
preactiv_name = bpy.context.view_layer.objects.active.name
for obj in objects:
bpy.context.view_layer.objects.active = obj
material_name = 'TT_material_override-' + obj.name
if material_name in bpy.data.materials:
material = bpy.data.materials[material_name]
else:
material = base_material.copy()
material.name = 'TT_material_override-' + obj.name
node_image = material.node_tree.nodes["image"]
node_image.image = image
if obj.modifiers:
for m in obj.modifiers:
if 'TT-material-override' in m.name:
if m.node_group.name == 'TT-material-override-' + obj.name:
break
else:
obj.modifiers.remove(m)
else:
obj.modifiers.new(name='TT-material-override', type='NODES')
nodegroup = base_nodegroup.copy()
nodegroup.name = 'TT-material-override-' + obj.name
node_material = nodegroup.nodes["material"]
node_material.inputs[2].default_value = material
obj.modifiers.active.node_group = nodegroup
obj.modifiers.active.show_render = False
else:
obj.modifiers.new(name='TT-material-override', type='NODES')
nodegroup = base_nodegroup.copy()
nodegroup.name = 'TT-material-override-' + obj.name
node_material = nodegroup.nodes["material"]
node_material.inputs[2].default_value = material
obj.modifiers.active.node_group = nodegroup
obj.modifiers.active.show_render = False
bpy.ops.object.modifier_move_to_index(modifier='TT-material-override', index=len(obj.modifiers)-1)
bpy.context.view_layer.objects.active = bpy.data.objects[preactiv_name]
def get_override_nodegroup():
if bpy.data.node_groups.get('TT-material-override') is None:
path = os.path.join(os.path.dirname(__file__), "resources/materials_3.0.blend", "NodeTree")
bpy.ops.wm.append(filename='TT-material-override', directory=path, link=False, autoselect=False)
return bpy.data.node_groups.get('TT-material-override')