-
Notifications
You must be signed in to change notification settings - Fork 46
/
light_preview_list.py
107 lines (81 loc) · 3.15 KB
/
light_preview_list.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
import os
from time import time
import bpy
from . common import getLightMesh, isFamily
_ = os.sep
script_file = os.path.realpath(__file__)
dir = os.path.dirname(script_file)
directory=os.path.join(dir,"textures_real_lights")
def enum_previews_from_directory_items(self, context):
"""EnumProperty callback"""
enum_items = []
if context is None:
return enum_items
wm = context.window_manager
script_file = os.path.realpath(__file__)
dir = os.path.dirname(script_file)
directory=os.path.join(dir,"textures_real_lights"+_)
# Get the preview collection (defined in register func).
pcoll = preview_collections["main"]
dir_up = os.path.getmtime(directory)
if pcoll.initiated and dir_up <= pcoll.dir_update_time:
return pcoll.tex_previews
pcoll.dir_update_time = dir_up
pcoll.clear()
print("Scanning directory: %s" % directory)
if directory and os.path.exists(directory):
# Scan the directory for png files
image_paths = []
for fn in os.listdir(directory):
if os.path.splitext(fn)[1] in (".tif", ".exr", ".hdr"):
image_paths.append(fn)
for i, name in enumerate(image_paths):
# generates a thumbnail preview for a file.
filepath = os.path.join(directory, name)
thumb = pcoll.load(filepath, filepath, 'IMAGE', True)
basename = os.path.splitext(name)[0]
enum_items.append((name, basename, name, thumb.icon_id, i))
pcoll.tex_previews = enum_items
pcoll.initiated = True
return pcoll.tex_previews
# We can store multiple preview collections here,
# however in this example we only store "main"
preview_collections = {}
def preview_enum_get(wm):
nodes = getLightMesh().active_material.node_tree.nodes
if not "Light Texture" in nodes:
return -1
tex = nodes["Light Texture"].image.filepath
tex = os.path.split(tex)[1]
names = (p[0] for p in preview_collections["main"].tex_previews)
for i, name in enumerate(names):
if name == tex:
return i
return -1
def preview_enum_set(wm, context):
print("Set preview = %s" % context)
name = preview_collections["main"].tex_previews[context][0]
light = getLightMesh()
light.active_material.node_tree.nodes["Light Texture"].image = bpy.data.images.load(os.path.join(directory, name), check_existing=True)
return None
def register():
from bpy.types import WindowManager
from bpy.props import EnumProperty
WindowManager.lls_tex_previews = EnumProperty(
items=enum_previews_from_directory_items,
get=preview_enum_get,
set=preview_enum_set,
)
import bpy.utils.previews
pcoll = bpy.utils.previews.new()
pcoll.lls_tex_previews = ()
pcoll.initiated = False
pcoll.dir_update_time = os.path.getmtime(directory)
preview_collections["main"] = pcoll
def unregister():
from bpy.types import WindowManager
del WindowManager.lls_tex_previews
for pcoll in preview_collections.values():
bpy.utils.previews.remove(pcoll)
pcoll.clear()
preview_collections.clear()