-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
160 lines (140 loc) · 5.05 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
Init for GFBANM/TRANM Importer addon.
"""
import os
import sys
import subprocess
import bpy
from bpy.props import *
from bpy.utils import register_class, unregister_class
from bpy_extras.io_utils import ImportHelper
bl_info = {
"name": "GFBANM/TRANM Import",
"author": "Shararamosh, Mvit & ElChicoEevee",
"blender": (2, 80, 0),
"version": (1, 0, 0),
"location": "File > Import-Export",
"description": "Import GFBANM/TRANM data",
"category": "Import-Export",
}
class ImportGfbanm(bpy.types.Operator, ImportHelper):
"""
Class for operator that imports GFBANM files.
"""
bl_idname = "import.gfbanm"
bl_label = "Import GFBANM/TRANM"
bl_description = "Import one or multiple GFBANM/TRANM files"
directory: StringProperty()
filter_glob: StringProperty(default="*.gfbanm;*.tranm", options={"HIDDEN"})
files: CollectionProperty(type=bpy.types.PropertyGroup)
ignore_origin_location: BoolProperty(
name="Ignore Origin Location",
description="Ignore Origin Location",
default=False
)
def execute(self, context: bpy.types.Context):
"""
Executing import menu.
:param context: Blender's context.
"""
if not attempt_install_flatbuffers(self):
self.report({"ERROR"}, "Failed to install flatbuffers library using pip. "
"To use this addon, put Python flatbuffers library folder "
f"to this path: {get_site_packages_path()}.")
return {"CANCELLED"}
from .gfbanm_importer import import_animation # pylint: disable=import-outside-toplevel
if self.files:
b = False
for file in self.files:
file_path = os.path.join(str(self.directory), file.name)
try:
import_animation(context, file_path, self.ignore_origin_location)
except OSError as e:
self.report({"INFO"}, f"Failed to import {file_path}. {str(e)}")
else:
b = True
finally:
pass
if b:
return {"FINISHED"}
return {"CANCELLED"}
try:
import_animation(context, self.filepath, self.ignore_origin_location)
except OSError as e:
self.report({"ERROR"}, f"Failed to import {self.filepath}. {str(e)}")
return {"CANCELLED"}
return {"FINISHED"}
@classmethod
def poll(cls, _context: bpy.types.Context):
"""
Checking if operator can be active.
:param _context: Blender's Context.
:return: True if active, False otherwise.
"""
return True
def draw(self, _context: bpy.types.Context):
"""
Drawing importer's menu.
:param _context: Blender's context.
"""
box = self.layout.box()
box.prop(self, "ignore_origin_location", text="Ignore Origin Location")
def menu_func_import(operator: bpy.types.Operator, _context: bpy.types.Context):
"""
Function that adds GFBANM import operator.
:param operator: Blender's operator.
:param _context: Blender's Context.
:return:
"""
operator.layout.operator(ImportGfbanm.bl_idname, text="GFBANM/TRANM (.gfbanm, .tranm)")
def register():
"""
Registering addon.
"""
register_class(ImportGfbanm)
bpy.types.TOPBAR_MT_file_import.append(menu_func_import)
def unregister():
"""
Unregistering addon.
:return:
"""
unregister_class(ImportGfbanm)
bpy.types.TOPBAR_MT_file_import.remove(menu_func_import)
def attempt_install_flatbuffers(operator: bpy.types.Operator = None) -> bool:
"""
Attempts installing flatbuffers library if it's not installed using pip.
:return: True if flatbuffers was found or successfully installed, False otherwise.
"""
if are_flatbuffers_installed():
return True
target = get_site_packages_path()
subprocess.call([sys.executable, "-m", 'ensurepip'])
subprocess.call([sys.executable, "-m", "pip", "install", "--upgrade", "pip"])
subprocess.call(
[sys.executable, "-m", "pip", "install", "--upgrade", "flatbuffers", "-t", target])
if are_flatbuffers_installed():
if operator is not None:
operator.report({"INFO"},
"Successfully installed flatbuffers library to " + target + ".")
else:
print("Successfully installed flatbuffers library to " + target + ".")
return True
return False
def are_flatbuffers_installed() -> bool:
"""
Checks if flatbuffers library is installed.
:return: True or False.
"""
try:
import flatbuffers # pylint: disable=import-outside-toplevel, unused-import
except ModuleNotFoundError:
return False
return True
def get_site_packages_path():
"""
Returns file path to lib/site-packages folder.
:return: File path to lib/site-packages folder.
"""
return os.path.join(sys.prefix, "lib", "site-packages")
if __name__ == "__main__":
register()