-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path__init__.py
144 lines (120 loc) · 4.57 KB
/
__init__.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
"""
IO PDX Mesh Python module.
Supports Maya 2018 and up, supports Blender 2.83 and up.
author : ross-g
"""
from __future__ import unicode_literals
import inspect
import json
import logging
import os.path as path
import sys
import traceback
import zipfile
from collections import OrderedDict
from imp import reload
# vendored package imports
from .external import tomllib
from .external.appdirs import user_data_dir # user settings directory
from .settings import PDXsettings
bl_info = { # legacy support: Blender < 4.2
"author": "ross-g",
"name": "IO PDX Mesh",
"description": "Import/Export Paradox asset files for the Clausewitz game engine.",
"location": "3D Viewport: View > Sidebar (N to toggle)",
"category": "Import-Export",
"support": "COMMUNITY",
"blender": (3, 6, 4),
}
root_path = path.abspath(path.dirname(inspect.getfile(inspect.currentframe())))
with open(path.join(root_path, "blender_manifest.toml"), "rb") as fh:
IO_PDX_INFO = tomllib.load(fh)
""" ====================================================================================================================
Setup.
========================================================================================================================
"""
# setup module logging
log_name = "io_pdx"
log_format = "[%(name)s] %(levelname)s: %(message)s"
log_lvl = logging.INFO
# setup module preferences
config_path = path.join(user_data_dir(IO_PDX_INFO["id"], False), "settings.json")
IO_PDX_SETTINGS = PDXsettings(config_path)
# setup engine/export settings
export_settings = path.join(root_path, "clausewitz.json")
ENGINE_SETTINGS = {}
try:
if ".zip" in export_settings:
zipped = export_settings.split(".zip")[0] + ".zip"
with zipfile.ZipFile(zipped, "r") as z:
f = z.open("io_pdx_mesh/clausewitz.json")
ENGINE_SETTINGS = json.loads(f.read(), object_pairs_hook=OrderedDict)
else:
with open(export_settings, "rt") as f:
ENGINE_SETTINGS = json.load(f, object_pairs_hook=OrderedDict)
except Exception as err:
print(err)
msg = (
"CRITICAL ERROR! Your 'clausewitz.json' settings file has errors and is unreadable."
"Some functions of the tool will not work without these settings."
)
raise RuntimeError(msg) # noqa: B904
""" ====================================================================================================================
Startup.
========================================================================================================================
"""
IO_PDX_LOG, running_from, version = None, None, None
environment = sys.executable.lower()
# check if running from Blender
try:
import bpy # type: ignore
running_from, version = bpy.app.binary_path.lower(), bpy.app.version
except ImportError:
pass
else:
logging.basicConfig(level=log_lvl, format=log_format)
IO_PDX_LOG = logging.getLogger(log_name)
min_version = tuple(IO_PDX_INFO["blender_support_min"])
if version < min_version:
IO_PDX_LOG.warning("UNSUPPORTED VERSION! Update to Blender {0}".format(min_version))
IO_PDX_INFO["unsupported_version"] = True
try:
# register the Blender addon
from .pdx_blender import register, unregister # noqa
except Exception as e:
traceback.print_exc()
raise e
# or running from Maya
try:
import maya.cmds # noqa
running_from, version = sys.executable.lower(), int(maya.cmds.about(version=True))
except ImportError:
pass
else:
IO_PDX_LOG = logging.getLogger(log_name)
IO_PDX_LOG.setLevel(log_lvl)
IO_PDX_LOG.propagate = False
IO_PDX_LOG.handlers = []
console = logging.StreamHandler(sys.stdout)
console.setFormatter(logging.Formatter(log_format))
IO_PDX_LOG.addHandler(console)
min_version = tuple(IO_PDX_INFO["maya_support_min"])[0]
if version < min_version:
IO_PDX_LOG.warning("UNSUPPORTED VERSION! Update to Maya {0}".format(min_version))
IO_PDX_INFO["unsupported_version"] = True
try:
# launch the Maya UI
from .pdx_maya import maya_ui
reload(maya_ui)
maya_ui.main()
except Exception as e:
traceback.print_exc()
raise e
if running_from is not None:
IO_PDX_LOG.info("Running {0} from {1} ({2})".format(__package__, running_from, version))
IO_PDX_LOG.info(root_path)
# otherwise, we don't support running with UI setup
else:
logging.basicConfig(level=logging.DEBUG, format=log_format)
IO_PDX_LOG = logging.getLogger(log_name)
IO_PDX_LOG.warning('Running without UI from environment "{0}"'.format(sys.executable))