|
25 | 25 | from bpy.props import StringProperty |
26 | 26 | from bpy_extras.io_utils import ImportHelper, ExportHelper |
27 | 27 |
|
28 | | -from os.path import basename |
29 | | - |
30 | | -from .FileIO import FileBuffer |
31 | | -from .SCG.SCGReader import SCGReader |
32 | | -from .SC2.SC2Reader import SC2Reader |
| 28 | +from .FileIO.StreamBuffer import StreamBuffer |
| 29 | +from .FileIO.SCG import readSCG |
| 30 | +from .Geometry.PolygonGroup import PrimitiveTypes, PolygonGroup |
33 | 31 |
|
34 | 32 | ''' |
35 | 33 | Operators |
36 | 34 | ''' |
37 | | -class ImportSCG(Operator, ImportHelper): |
| 35 | +class ImportDAVA(Operator, ImportHelper): |
38 | 36 | bl_idname = "import_scene.scg" |
39 | 37 | bl_label = "Import DAVA geometry" |
40 | | - bl_description = "Import a DAVA geometry file" |
| 38 | + bl_description = "Import a DAVA scene file" |
41 | 39 |
|
42 | 40 | filter_glob: StringProperty(default="*.scg", options={'HIDDEN'}) |
43 | 41 |
|
44 | 42 | def invoke(self, context, event): |
45 | 43 | return ImportHelper.invoke(self, context, event) |
46 | 44 |
|
| 45 | + # Just import SCG whilst we get proper full imports working |
47 | 46 | def execute(self, context): |
48 | 47 | filepath = self.filepath |
49 | | - filename = basename(filepath).split(".")[0] |
50 | | - print(f"Importing DAVA geometry from {filepath}") |
51 | | - |
52 | | - with open(filepath, "rb") as f: |
53 | | - stream = FileBuffer(f) |
54 | | - geometry = SCGReader.readFromBuffer(stream) |
55 | | - |
56 | | - # Add geometry to scene |
57 | | - collection = bpy.data.collections.new(filename) |
58 | | - for groupId, polyGroup in geometry.polygonGroups.items(): |
| 48 | + print(f"Importing DAVA scene from {filepath}") |
| 49 | + |
| 50 | + with open(filepath, "rb") as scg: |
| 51 | + polyGroups = readSCG( |
| 52 | + StreamBuffer(scg) |
| 53 | + ) |
| 54 | + |
| 55 | + # Parse polygon groups |
| 56 | + for groupID in polyGroups.keys(): |
| 57 | + polyGroups[groupID] = PolygonGroup( polyGroups[groupID] ) |
| 58 | + |
| 59 | + # Add polygon groups to scene |
| 60 | + collection = bpy.data.collections.new("DAVAMesh") |
| 61 | + for groupID in polyGroups.keys(): |
| 62 | + group = polyGroups[groupID] |
59 | 63 | mesh = bpy.data.meshes.new("mesh") |
60 | | - mesh.from_pydata(polyGroup.vertices.VERTEX, polyGroup.edges, polyGroup.faces) |
| 64 | + |
| 65 | + if group.primitiveType == PrimitiveTypes.TRIANGLELIST: |
| 66 | + mesh.from_pydata(group.vertices, [], group.getTriangleList()) |
| 67 | + elif group.primitiveType == PrimitiveTypes.TRIANGLESTRIP: |
| 68 | + mesh.from_pydata(group.vertices, [], group.getTriangleStrip()) |
| 69 | + elif group.primitiveType == PrimitiveTypes.LINELIST: |
| 70 | + mesh.from_pydata(group.vertices, group.getLineList(), []) |
61 | 71 | mesh.update() |
62 | 72 |
|
63 | | - obj = bpy.data.objects.new(f"PolygonGroup{groupId}", mesh) |
| 73 | + obj = bpy.data.objects.new(f"PolygonGroup{groupID}", mesh) |
64 | 74 | collection.objects.link(obj) |
65 | 75 | bpy.context.scene.collection.children.link(collection) |
66 | | - self.report({"INFO"}, f"Loaded {len(geometry.polygonGroups)} polygon groups") |
| 76 | + self.report({"INFO"}, f"Loaded {len(polyGroups)} polygon groups") |
67 | 77 |
|
68 | 78 | return {"FINISHED"} |
69 | 79 |
|
70 | | -class ExportSCG(Operator, ExportHelper): |
71 | | - bl_idname = "export_scene.scg" |
| 80 | +class ExportDAVA(Operator, ExportHelper): |
| 81 | + bl_idname = "export_scene.sc2" |
72 | 82 | bl_label = "Export DAVA geometry" |
73 | | - bl_description = "Export a DAVA geometry file" |
| 83 | + bl_description = "Export a scene file" |
74 | 84 |
|
75 | | - filter_glob: StringProperty(default="*.scg", options={'HIDDEN'}) |
76 | | - filename_ext: StringProperty(default=".scg", options={'HIDDEN'}) |
| 85 | + filter_glob: StringProperty(default="*.sc2", options={'HIDDEN'}) |
| 86 | + filename_ext: StringProperty(default=".sc2", options={'HIDDEN'}) |
77 | 87 |
|
78 | 88 | def invoke(self, context, event): |
79 | 89 | return ExportHelper.invoke(self, context, event) |
80 | 90 |
|
81 | 91 | def execute(self, context): |
82 | 92 | filepath = self.filepath |
83 | | - print(f"Exporting DAVA geometry to {filepath}") |
| 93 | + print(f"Exporting DAVA scene to {filepath}") |
84 | 94 | return {'FINISHED'} |
85 | 95 |
|
86 | | -class ImportSC2(Operator, ImportHelper): |
87 | | - bl_idname = "import_scene.sc2" |
88 | | - bl_label = "Import DAVA scene" |
89 | | - bl_description = "Import a DAVA scene file" |
90 | | - |
91 | | - filter_glob: StringProperty(default="*.sc2", options={'HIDDEN'}) |
92 | | - |
93 | | - def invoke(self, context, event): |
94 | | - return ImportHelper.invoke(self, context, event) |
95 | | - |
96 | | - def execute(self, context): |
97 | | - filepath = self.filepath |
98 | | - print(f"Importing DAVA scene file from {filepath}") |
99 | | - |
100 | | - with open(filepath, "rb") as f: |
101 | | - stream = FileBuffer(f) |
102 | | - SC2Reader.readFromBuffer(stream) |
103 | | - |
104 | | - return {"FINISHED"} |
105 | | - |
106 | 96 | ''' |
107 | 97 | Menu |
108 | 98 | ''' |
109 | | -def menu_func_import_scg(self, context): |
110 | | - self.layout.operator(ImportSCG.bl_idname, text="DAVA scene geometry (.scg)") |
111 | | - |
112 | | -def menu_func_export_scg(self, context): |
113 | | - self.layout.operator(ExportSCG.bl_idname, text="DAVA scene geometry (.scg)") |
| 99 | +def menu_func_import_dava(self, context): |
| 100 | + self.layout.operator(ImportDAVA.bl_idname, text="DAVA scene (.sc2/.scg)") |
114 | 101 |
|
115 | | -def menu_func_import_sc2(self, context): |
116 | | - self.layout.operator(ImportSC2.bl_idname, text="DAVA scene file (.sc2)") |
| 102 | +def menu_func_export_dava(self, context): |
| 103 | + self.layout.operator(ExportDAVA.bl_idname, text="DAVA scene (.sc2/.scg)") |
117 | 104 |
|
118 | 105 | ''' |
119 | 106 | Register |
120 | 107 | ''' |
121 | 108 | classes = { |
122 | | - ExportSCG, |
123 | | - ImportSCG, |
124 | | - ImportSC2 |
| 109 | + ExportDAVA, |
| 110 | + ImportDAVA |
125 | 111 | } |
126 | 112 |
|
127 | 113 | def register(): |
128 | 114 | # Register classes |
129 | 115 | for c in classes: |
130 | 116 | bpy.utils.register_class(c) |
131 | 117 | # File > Import-Export |
132 | | - bpy.types.TOPBAR_MT_file_import.append(menu_func_import_scg) |
133 | | - bpy.types.TOPBAR_MT_file_export.append(menu_func_export_scg) |
134 | | - bpy.types.TOPBAR_MT_file_import.append(menu_func_import_sc2) |
| 118 | + bpy.types.TOPBAR_MT_file_import.append(menu_func_import_dava) |
| 119 | +# bpy.types.TOPBAR_MT_file_export.append(menu_func_export_dava) |
135 | 120 |
|
136 | 121 | def unregister(): |
137 | 122 | # Unregister classes |
138 | 123 | for c in classes: |
139 | 124 | bpy.utils.unregister_class(c) |
140 | 125 | # Remove `File > Import-Export` |
141 | | - bpy.types.TOPBAR_MT_file_import.remove(menu_func_import_scg) |
142 | | - bpy.types.TOPBAR_MT_file_export.remove(menu_func_export_scg) |
143 | | - bpy.types.TOPBAR_MT_file_import.remove(menu_func_import_sc2) |
| 126 | + bpy.types.TOPBAR_MT_file_import.remove(menu_func_import_dava) |
| 127 | +# bpy.types.TOPBAR_MT_file_export.remove(menu_func_export_dava) |
0 commit comments