1
0
mirror of https://github.com/drewcassidy/TexTools-Blender synced 2024-09-01 14:54:44 +00:00
Blender-TexTools/utilities_ui.py

166 lines
5.4 KiB
Python
Raw Normal View History

2019-06-08 23:42:50 +00:00
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 = [
2019-12-24 19:59:46 +00:00
('32', '32', ''),
('64', '64', ''),
('128', '128', ''),
('256', '256', ''),
('512', '512', ''),
('1024', '1024', ''),
('2048', '2048', ''),
('4096', '4096', ''),
('8192', '8192', '')
]
2019-06-08 23:42:50 +00:00
preview_icons = bpy.utils.previews.new()
2019-12-24 19:59:46 +00:00
2019-06-08 23:42:50 +00:00
def icon_get(name):
2019-12-18 20:53:16 +00:00
return preview_icons[name].icon_id
2019-06-08 23:42:50 +00:00
def GetContextView3D():
2019-12-18 20:53:16 +00:00
for window in bpy.context.window_manager.windows:
screen = window.screen
for area in screen.areas:
2019-12-24 19:59:46 +00:00
if area.type == 'VIEW_3D':
2019-12-18 20:53:16 +00:00
for region in area.regions:
if region.type == 'WINDOW':
2019-12-24 19:59:46 +00:00
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
2019-12-18 20:53:16 +00:00
return None
2019-06-08 23:42:50 +00:00
def GetContextViewUV():
2019-12-18 20:53:16 +00:00
for window in bpy.context.window_manager.windows:
screen = window.screen
for area in screen.areas:
2019-12-24 19:59:46 +00:00
if area.type == 'IMAGE_EDITOR':
2019-12-18 20:53:16 +00:00
for region in area.regions:
2019-12-24 19:59:46 +00:00
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
2019-12-18 20:53:16 +00:00
return None
2019-06-08 23:42:50 +00:00
def icon_register(fileName):
2019-12-18 20:53:16 +00:00
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')
2019-06-08 23:42:50 +00:00
def get_padding():
2019-12-24 19:59:46 +00:00
size_min = min(
bpy.context.scene.texToolsSettings.size[0], bpy.context.scene.texToolsSettings.size[1])
2019-12-18 20:53:16 +00:00
return bpy.context.scene.texToolsSettings.padding / size_min
2019-06-08 23:42:50 +00:00
def generate_bake_mode_previews():
2019-12-18 20:53:16 +00:00
# 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')
2019-12-24 19:59:46 +00:00
2019-12-18 20:53:16 +00:00
enum_items = []
2019-12-24 19:59:46 +00:00
2019-12-18 20:53:16 +00:00
# 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))
2019-12-24 19:59:46 +00:00
2019-12-18 20:53:16 +00:00
return enum_items
2019-12-24 19:59:46 +00:00
2019-06-08 23:42:50 +00:00
def get_bake_mode():
2019-12-24 19:59:46 +00:00
return str(bpy.context.scene.TT_bake_mode).replace(".png", "").lower()
2019-06-08 23:42:50 +00:00
class op_popup(bpy.types.Operator):
2019-12-18 20:53:16 +00:00
bl_idname = "ui.textools_popup"
bl_label = "Message"
2019-06-08 23:42:50 +00:00
2019-12-24 19:59:46 +00:00
message: StringProperty()
2019-12-18 20:53:16 +00:00
def execute(self, context):
self.report({'INFO'}, self.message)
print(self.message)
return {'FINISHED'}
2019-12-24 19:59:46 +00:00
2019-12-18 20:53:16 +00:00
def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_popup(self, width=200, height=200)
2019-12-24 19:59:46 +00:00
2019-12-18 20:53:16 +00:00
def draw(self, context):
self.layout.label(text=self.message)
2019-06-08 23:42:50 +00:00
def on_bakemode_set(self, context):
2019-12-18 20:53:16 +00:00
print("Set '{}'".format(bpy.context.scene.TT_bake_mode))
utilities_bake.on_select_bake_mode(get_bake_mode())
2019-06-08 23:42:50 +00:00
def register():
2019-12-18 20:53:16 +00:00
from bpy.types import Scene
from bpy.props import StringProperty, EnumProperty
2019-12-24 19:59:46 +00:00
2019-12-18 20:53:16 +00:00
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()
2019-12-24 19:59:46 +00:00
preview_collection.images_location = os.path.join(
os.path.dirname(__file__), "resources/bake_modes")
2019-12-18 20:53:16 +00:00
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(),
2019-12-24 19:59:46 +00:00
update=on_bakemode_set,
default='normal_tangent.png'
2019-12-18 20:53:16 +00:00
)
2019-12-24 19:59:46 +00:00
2019-06-08 23:42:50 +00:00
def unregister():
2019-12-18 20:53:16 +00:00
print("_______UNregister previews")
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
from bpy.types import WindowManager
for preview_collection in preview_collections.values():
bpy.utils.previews.remove(preview_collection)
preview_collections.clear()
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
# Unregister icons
# global preview_icons
bpy.utils.previews.remove(preview_icons)
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
del bpy.types.Scene.TT_bake_mode
2019-12-24 19:59:46 +00:00
2019-06-08 23:42:50 +00:00
if __name__ == "__main__":
2019-12-18 20:53:16 +00:00
register()
2019-12-24 19:59:46 +00:00
bpy.utils.register_class(op_popup)