-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__init__.py
executable file
·204 lines (168 loc) · 6.69 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
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
bl_info = {
"name": "Import Panda3D .egg models",
"author": "rdb, loonatic",
"version": (2, 6, 0),
"blender": (2, 80, 0),
"location": "File > Import > Panda3D (.egg)",
"description": "",
"warning": "",
"category": "Import-Export",
}
import bpy
if bpy.app.version < (2, 80):
bl_info["blender"] = (2, 74, 0) # Needed for normals_split_custom_set
if "loaded" in locals():
import imp
imp.reload(eggparser)
imp.reload(importer)
else:
from . import eggparser
from . import importer
loaded = True
import os.path
import bpy.types
from bpy import props
from bpy_extras.io_utils import ImportHelper
class EggImporterPreferences(bpy.types.AddonPreferences):
bl_idname = __name__
if bpy.app.version >= (2, 80):
backup_texpath: props.StringProperty(
name = "Texture path",
subtype = "FILE_PATH",
description = "Backup texture path to check if the texture can't be "
"found at the location of the .egg"
)
want_bsdf: props.BoolProperty(
name = "Use Principled BSDF",
default = True,
description = "Determines whether materials will automatically use Principled BSDF. "
"If false, they won't have shading"
)
else:
backup_texpath = props.StringProperty(
name="Texture path",
subtype="FILE_PATH",
description="Backup texture path to check if the texture can't be "
"found at the location of the .egg"
)
want_bsdf = props.BoolProperty(
name="Use Principled BSDF",
default=True,
description="Determines whether materials will automatically use Principled BSDF. "
"If false, they won't have shading"
)
def draw(self, context):
layout = self.layout
layout.prop(self, "backup_texpath")
layout.prop(self, "want_bsdf")
class IMPORT_OT_egg(bpy.types.Operator, ImportHelper):
"""Import .egg Operator"""
bl_idname = "import_scene.egg"
bl_label = "Import .egg"
bl_description = "Import a Panda3D .egg file"
bl_options = {'REGISTER', 'UNDO'}
filename_ext = ".egg"
filter_glob = props.StringProperty(default="*.egg;*.egg.pz;*.egg.gz", options={'HIDDEN'})
"""
Note:
Blender versions >= 2.8 use Python's type annotations (PEP 526), meaning we need to use colons instead of equals.
If we do not use colons for >= 2.8, it may still run, but can spam the console with something like:
rna_uiItemR: property not found: IMPORT_SCENE_OT_egg.auto_bind
In <2.8 versions of Blender, we must use = for assigning custom properties, otherwise we will get a SyntaxError.
"""
if bpy.app.version < (2, 80):
directory = props.StringProperty(name="Directory", options={'HIDDEN'})
files = props.CollectionProperty(type=bpy.types.OperatorFileListElement, options={'HIDDEN'})
else:
# Blender 2.79 still wants to compile this, however this syntax is considered invalid for this version.
# To avoid a measly crash, let's just shrug off the syntax error for older versions.
try:
directory: props.StringProperty(name="Directory", options={'HIDDEN'})
files: props.CollectionProperty(type=bpy.types.OperatorFileListElement, options={'HIDDEN'})
except SyntaxError:
pass
if bpy.app.version >= (2, 80):
load_external: props.BoolProperty(
name = "Load external references",
description = "Loads other .egg files referenced by this file as separate scenes, "
"and instantiates them using DupliGroups."
)
auto_bind: props.BoolProperty(
name = "Auto bind",
default = True,
description = "Automatically tries to bind actions to armatures."
)
else:
load_external = props.BoolProperty(
name="Load external references",
description="Loads other .egg files referenced by this file as separate scenes, "
"and instantiates them using DupliGroups."
)
auto_bind = props.BoolProperty(
name="Auto bind",
default=True,
description="Automatically tries to bind actions to armatures."
)
def execute(self, context):
context = importer.EggContext()
context.info = lambda msg: self.report({'INFO'}, context.prefix_message(msg))
context.warn = lambda msg: self.report({'WARNING'}, context.prefix_message(msg))
context.error = lambda msg: self.report({'ERROR'}, context.prefix_message(msg))
context.search_dir = self.directory
roots = []
for file in self.files:
path = os.path.join(self.directory, file.name)
root = context.read_file(path)
roots.append(root)
for root in roots:
root.build_tree(context)
context.assign_vertex_groups()
if self.load_external:
context.load_external_references()
if self.auto_bind:
context.auto_bind()
context.final_report()
return {'FINISHED'}
def invoke(self, context, event):
wm = context.window_manager
wm.fileselect_add(self)
return {'RUNNING_MODAL'}
def draw(self, context):
layout = self.layout
row = layout.row()
row.prop(self, "load_external")
row = layout.row()
row.prop(self, "auto_bind")
def menu_func(self, context):
self.layout.operator(IMPORT_OT_egg.bl_idname, text = "Panda3D (.egg)")
def make_annotations(cls):
"""Converts class fields to annotations if running with Blender 2.8"""
if bpy.app.version < (2, 80):
return cls
bl_props = {k: v for k, v in cls.__dict__.items() if isinstance(v, tuple)}
if bl_props:
if '__annotations__' not in cls.__dict__:
setattr(cls, '__annotations__', {})
annotations = cls.__dict__['__annotations__']
for k, v in bl_props.items():
annotations[k] = v
delattr(cls, k)
return cls
classes = (IMPORT_OT_egg, EggImporterPreferences)
def register():
make_annotations(IMPORT_OT_egg)
for cls in classes:
bpy.utils.register_class(cls)
if bpy.app.version >= (2, 80):
bpy.types.TOPBAR_MT_file_import.append(menu_func)
else:
bpy.types.INFO_MT_file_import.append(menu_func)
def unregister():
if bpy.app.version >= (2, 80):
bpy.types.TOPBAR_MT_file_import.remove(menu_func)
else:
bpy.types.INFO_MT_file_import.remove(menu_func)
for cls in classes:
bpy.utils.unregister_class(cls)
if __name__ == "__main__":
register()