forked from franMarz/TexTools-Blender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities_ui.py
170 lines (115 loc) · 4.9 KB
/
utilities_ui.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
import bpy
import bpy.utils.previews
import os
from bpy.types import Panel, EnumProperty, WindowManager
from bpy.props import StringProperty
from . import settings
from . import utilities_bake
from . import op_bake
preview_collections = {}
size_textures = [
('32', '32', ''),
('64', '64', ''),
('128', '128', ''),
('256', '256', ''),
('512', '512', ''),
('1024', '1024', ''),
('2048', '2048', ''),
('4096', '4096', ''),
('8192', '8192', '')
]
preview_icons = bpy.utils.previews.new()
def icon_get(name):
return preview_icons[name].icon_id
def GetContextView3D():
for window in bpy.context.window_manager.windows:
screen = window.screen
for area in screen.areas:
if area.type == 'VIEW_3D':
for region in area.regions:
if region.type == 'WINDOW':
override = {'window': window, 'screen': screen, 'area': area, 'region': region, 'scene': bpy.context.scene, 'edit_object': bpy.context.edit_object, 'active_object': bpy.context.active_object, 'selected_objects': bpy.context.selected_objects} # Stuff the override context with very common requests by operators. MORE COULD BE NEEDED!
return override
return None
def GetContextViewUV():
for window in bpy.context.window_manager.windows:
screen = window.screen
for area in screen.areas:
if area.type == 'IMAGE_EDITOR':
for region in area.regions:
if region.type == 'WINDOW':
override = {'window': window, 'screen': screen, 'area': area, 'region': region, 'scene': bpy.context.scene, 'edit_object': bpy.context.edit_object, 'active_object': bpy.context.active_object, 'selected_objects': bpy.context.selected_objects} # Stuff the override context with very common requests by operators. MORE COULD BE NEEDED!
return override
return None
def icon_register(fileName):
name = fileName.split('.')[0] # Don't include file extension
icons_dir = os.path.join(os.path.dirname(__file__), "icons")
preview_icons.load(name, os.path.join(icons_dir, fileName), 'IMAGE')
def get_padding():
size_min = min(bpy.context.scene.texToolsSettings.size[0],bpy.context.scene.texToolsSettings.size[1])
return bpy.context.scene.texToolsSettings.padding / size_min
def generate_bake_mode_previews():
# We are accessing all of the information that we generated in the register function below
preview_collection = preview_collections["thumbnail_previews"]
image_location = preview_collection.images_location
VALID_EXTENSIONS = ('.png', '.jpg', '.jpeg')
enum_items = []
# Generate the thumbnails
for i, image in enumerate(os.listdir(image_location)):
mode = image[0:-4]
print(".. .{}".format(mode))
if image.endswith(VALID_EXTENSIONS) and mode in op_bake.modes:
filepath = os.path.join(image_location, image)
thumb = preview_collection.load(filepath, filepath, 'IMAGE')
enum_items.append((image, mode, "", thumb.icon_id, i))
return enum_items
def get_bake_mode():
return str(bpy.context.scene.TT_bake_mode).replace(".png","").lower()
class op_popup(bpy.types.Operator):
bl_idname = "ui.textools_popup"
bl_label = "Message"
message : StringProperty()
def execute(self, context):
self.report({'INFO'}, self.message)
print(self.message)
return {'FINISHED'}
def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_popup(self, width=200, height=200)
def draw(self, context):
self.layout.label(text=self.message)
def on_bakemode_set(self, context):
print("Set '{}'".format(bpy.context.scene.TT_bake_mode))
utilities_bake.on_select_bake_mode(get_bake_mode())
def register():
from bpy.types import Scene
from bpy.props import StringProperty, EnumProperty
print("_______REgister previews")
# Operators
# bpy.utils.register_class(op_popup)
# global preview_icons
# preview_icons = bpy.utils.previews.new()
# Create a new preview collection (only upon register)
preview_collection = bpy.utils.previews.new()
preview_collection.images_location = os.path.join(os.path.dirname(__file__), "resources/bake_modes")
preview_collections["thumbnail_previews"] = preview_collection
# This is an EnumProperty to hold all of the images
# You really can save it anywhere in bpy.types.* Just make sure the location makes sense
bpy.types.Scene.TT_bake_mode = EnumProperty(
items=generate_bake_mode_previews(),
update = on_bakemode_set,
default = 'normal_tangent.png'
)
def unregister():
print("_______UNregister previews")
from bpy.types import WindowManager
for preview_collection in preview_collections.values():
bpy.utils.previews.remove(preview_collection)
preview_collections.clear()
# Unregister icons
# global preview_icons
bpy.utils.previews.remove(preview_icons)
del bpy.types.Scene.TT_bake_mode
if __name__ == "__main__":
register()
bpy.utils.register_class(op_popup)