-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathblender_utils.py
220 lines (175 loc) · 7.2 KB
/
blender_utils.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# Copyright vanous
#
# This file is part of BlenderDMX.
#
# BlenderDMX is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# BlenderDMX is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
import glob
import importlib
import os
import shutil
import sys
import threading
from types import SimpleNamespace
import bpy
import requests
from .i18n import DMX_Lang
_ = DMX_Lang._
def get_version_json(url, callback, context):
try:
response = requests.get(url)
except Exception as e:
callback({"error": f"{e.__class__.__name__} {e}"}, context)
return
if response.ok:
callback(response.json(), context)
else:
callback({"error": response.reason}, context)
def version_compare(current_version, new_version):
cur_ver = current_version
new_ver = new_version.split(".")
n = max(len(cur_ver), len(new_ver))
for i in range(n):
cur_ver_num = int(cur_ver[i]) if i < len(cur_ver) else 0
new_ver_num = int(new_ver[i]) if i < len(new_ver) else 0
if cur_ver_num > new_ver_num:
return 1
elif cur_ver_num < new_ver_num:
return -1
return 0
def export_custom_data(directory_name, file_name):
dmx = bpy.context.scene.dmx
folder_path = dmx.get_addon_path()
export_dir = os.path.join(folder_path, "export")
export_filename = os.path.join(directory_name, file_name)
if os.path.exists(export_dir):
shutil.rmtree(export_dir)
os.mkdir(export_dir)
models_path = os.path.join(folder_path, "assets", "models")
mvrs_path = os.path.join(folder_path, "assets", "mvrs")
profiles_path = os.path.join(folder_path, "assets", "profiles")
# do not export (and import) BlenderDMX profiles as they might be improved in the future
# and this would prevent that
ignore = shutil.ignore_patterns("BlenderDMX*")
try:
shutil.copytree(models_path, os.path.join(export_dir, "models"))
if os.path.exists(mvrs_path):
shutil.copytree(mvrs_path, os.path.join(export_dir, "mvrs"))
shutil.copytree(
profiles_path, os.path.join(export_dir, "profiles"), ignore=ignore
)
shutil.make_archive(export_filename, "zip", export_dir)
shutil.rmtree(export_dir)
except Exception as e:
return SimpleNamespace(ok=False, error=str(e))
return SimpleNamespace(ok=True, error="")
def import_custom_data(directory_name, file_name):
import_filename = os.path.join(directory_name, file_name)
dmx = bpy.context.scene.dmx
folder_path = dmx.get_addon_path()
import_dir = os.path.join(folder_path, "assets")
if not os.path.exists(import_filename):
return SimpleNamespace(ok=False, error=_("File doesn't exist"))
try:
shutil.unpack_archive(import_filename, import_dir)
except Exception as e:
return SimpleNamespace(ok=False, error=str(e))
return SimpleNamespace(ok=True, error="")
def clear_custom_data():
dmx = bpy.context.scene.dmx
folder_path = dmx.get_addon_path()
models_path = os.path.join(folder_path, "assets", "models", "*")
mvrs_path = os.path.join(folder_path, "assets", "mvrs", "*")
profiles_path = os.path.join(folder_path, "assets", "profiles", "*")
rm_dirs = [
(models_path, "models"),
(mvrs_path, "mvrs"),
(profiles_path, "profiles"),
]
try:
for rm_dir, name in rm_dirs:
for file in glob.glob(rm_dir):
if name == "profiles":
if "BlenderDMX" in file and file.endswith(".gdtf"):
continue
if os.path.isdir(file):
shutil.rmtree(file)
continue
if os.path.isfile(file):
os.remove(file)
except Exception as e:
return SimpleNamespace(ok=False, error=str(e))
return SimpleNamespace(ok=True, error="")
def copy_custom_data():
dmx = bpy.context.scene.dmx
folder_path = dmx.get_addon_path()
addon_path = os.path.dirname(os.path.realpath(__file__))
models_path_new = os.path.join(folder_path, "assets", "models")
mvrs_path_new = os.path.join(folder_path, "assets", "mvrs")
profiles_path_new = os.path.join(folder_path, "assets", "profiles")
models_path_old = os.path.join(addon_path, "assets", "models")
mvrs_path_old = os.path.join(addon_path, "assets", "mvrs")
profiles_path_old = os.path.join(addon_path, "assets", "profiles")
try:
if os.path.exists(models_path_old):
shutil.copytree(models_path_old, models_path_new, dirs_exist_ok=True)
if os.path.exists(mvrs_path_old):
shutil.copytree(mvrs_path_old, mvrs_path_new, dirs_exist_ok=True)
if os.path.exists(profiles_path_old):
shutil.copytree(profiles_path_old, profiles_path_new, dirs_exist_ok=True)
except Exception as e:
return SimpleNamespace(ok=False, error=str(e))
return SimpleNamespace(ok=True, error="")
def old_custom_data_exists():
addon_path = os.path.dirname(os.path.realpath(__file__))
models_path_old = os.path.join(addon_path, "assets", "models")
if os.path.exists(models_path_old):
return len(glob.glob(os.path.join(models_path_old, "*"))) > 0
return False
def copy_blender_profiles():
dmx = bpy.context.scene.dmx
folder_path = dmx.get_addon_path()
addon_path = os.path.dirname(os.path.realpath(__file__))
profiles_path_user = os.path.join(folder_path, "assets", "profiles")
profiles_path_addon = os.path.join(addon_path, "assets", "profiles", "*")
for file in glob.glob(profiles_path_addon):
if os.path.isfile(file):
if "BlenderDMX" in file and file.endswith(".gdtf"):
dest_path = os.path.join(profiles_path_user, os.path.basename(file))
if not os.path.exists(dest_path):
shutil.copy(file, profiles_path_user)
def reload_addon():
try:
module = sys.modules.get(__package__)
if not module:
raise Exception("DMX module could not be loaded")
module.unregister()
importlib.reload(module)
module.register()
except Exception as e:
return SimpleNamespace(ok=False, error=str(e))
return SimpleNamespace(ok=True, error="")
def get_extension_manifest():
import toml
folder_path = os.path.dirname(os.path.realpath(__file__))
toml_path = os.path.join(folder_path, "blender_manifest.toml")
data = toml.load(toml_path)
return data
def get_application_version():
if bpy.app.version >= (4, 2):
data = get_extension_manifest()
version = data["version"]
return tuple(version.split("."))
else:
from . import bl_info as application_info
return application_info["version"]