-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path_runtime.py
More file actions
140 lines (112 loc) · 4.15 KB
/
_runtime.py
File metadata and controls
140 lines (112 loc) · 4.15 KB
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
import json
import bpy
from pathlib import Path
import sys
from .public_path import AssetDir, get_AssetDir_path
from .wrap_handle import gen_import_op, gen_import_handle
G_ops = {}
G_handles = {}
def ensure_op_handles():
datas = {}
# get configs dict from path
for file in get_AssetDir_path(AssetDir.CONFIG).iterdir():
with open(file, 'r', encoding='utf-8') as f:
data = json.load(f)
datas.update(data)
# register in op
for index, (label, values) in enumerate(datas.items()):
new_idname = f'cdi.import_{index}'
op = gen_import_op(
bl_label=label,
bl_idname=new_idname,
bl_import_operator=values['bl_import_operator'],
bl_file_extensions=values['bl_file_extensions'],
operator_context=values.get('operator_context', 'INVOKE_DEFAULT'),
)
# external scripts
op.pre_script = values.get('pre_script')
op.post_script = values.get('post_script')
op.foreach_pre_script = values.get('foreach_pre_script')
op.foreach_post_script = values.get('foreach_post_script')
# handle
handle = gen_import_handle(
bl_label=label,
bl_import_operator=new_idname,
bl_idname=f"CDI_FH_handle{index}",
bl_file_extensions=values['bl_file_extensions'],
poll_area=values['poll_area']
)
handle.poll_area = values['poll_area']
G_ops.update({label: op})
G_handles.update({label: handle})
# print(G_ops, G_handles)
class CDI_OT_popup_operator(bpy.types.Operator):
bl_label = "CDI Popup Operator"
bl_idname = "cdi.popup_operator"
def filter_files(self, files) -> list[Path]:
# get the most common extension
exts = [file.suffix for file in files]
ext = max(set(exts), key=exts.count)
return [file for file in files if file.suffix == ext]
def filter_operator(self, context, bl_file_extensions) -> list[str]:
split_exts = lambda exts: exts.split(';')
ops = []
for handle in G_handles.values():
if context.area.type != handle.poll_area: continue
if bl_file_extensions in split_exts(handle.bl_file_extensions):
ops.append(handle.bl_import_operator)
return ops
def execute(self, context):
from . import clipboard
wm = context.window_manager
with clipboard.clipboard():
try:
files = clipboard.get_FILEPATHS()
except clipboard.ClipetteWin32ClipboardError:
self.report({'ERROR'}, 'Clipboard is empty!')
return {'CANCELLED'}
files = [Path(file) for file in files]
files = self.filter_files(files)
if not files:
return {'CANCELLED'}
directory = files[0].parent
bl_file_extensions = files[0].suffix
clipboard_files = ';'.join([file.name for file in files])
def draw(_self, _context):
_self.layout.operator_context = 'INVOKE_DEFAULT'
_self.layout.label(text=f"Import {bl_file_extensions} files")
for bl_idname in self.filter_operator(context, bl_file_extensions):
op = _self.layout.operator(bl_idname)
op.directory = str(directory)
op.clipboard_files = clipboard_files
wm.popup_menu(draw)
return {'FINISHED'}
def register():
try:
unregister()
except:
pass
ensure_op_handles()
import bpy
for op in G_ops.values():
bpy.utils.register_class(op)
for handle in G_handles.values():
bpy.utils.register_class(handle)
if sys.platform == 'win32':
bpy.utils.register_class(CDI_OT_popup_operator)
def unregister():
import bpy
for op in G_ops.values():
try:
bpy.utils.unregister_class(op)
except RuntimeError:
pass
for handle in G_handles.values():
try:
bpy.utils.unregister_class(handle)
except RuntimeError:
pass
if sys.platform == 'win32':
bpy.utils.unregister_class(CDI_OT_popup_operator)
G_ops.clear()
G_handles.clear()