diff --git a/io_mesh_raw/__init__.py b/io_mesh_raw/__init__.py index 67109d2101115ab3e7e3ef81435710d8be3f26ee..0003378f04aa47a61b9bd6424df8d5559979a53c 100644 --- a/io_mesh_raw/__init__.py +++ b/io_mesh_raw/__init__.py @@ -16,6 +16,9 @@ # # ##### END GPL LICENSE BLOCK ##### +# <pep8 compliant> + + bl_info = { "name": "Raw mesh format (.raw)", "author": "Anthony D,Agostino (Scorpius), Aurel Wildfellner", @@ -33,23 +36,69 @@ bl_info = { if "bpy" in locals(): import imp - imp.reload(import_raw) - imp.reload(export_raw) + if "import_raw" in locals(): + imp.reload(import_raw) + if "export_raw" in locals(): + imp.reload(export_raw) else: - from . import import_raw - from . import export_raw + import bpy + +from bpy.props import StringProperty, BoolProperty + + +class RawImporter(bpy.types.Operator): + '''Load Raw triangle mesh data''' + bl_idname = "import_mesh.raw" + bl_label = "Import RAW" + + filepath = StringProperty(name="File Path", description="Filepath used for importing the RAW file", maxlen=1024, default="", subtype='FILE_PATH') + filter_glob = StringProperty(default="*.raw", options={'HIDDEN'}) + + def execute(self, context): + from . import import_raw + import_raw.read(self.filepath) + return {'FINISHED'} + + def invoke(self, context, event): + wm = context.window_manager + wm.fileselect_add(self) + return {'RUNNING_MODAL'} + + +class RawExporter(bpy.types.Operator): + '''Save Raw triangle mesh data''' + bl_idname = "export_mesh.raw" + bl_label = "Export RAW" + + filepath = StringProperty(name="File Path", description="Filepath used for exporting the RAW file", maxlen=1024, default= "", subtype='FILE_PATH') + check_existing = BoolProperty(name="Check Existing", description="Check and warn on overwriting existing files", default=True, options={'HIDDEN'}) + + apply_modifiers = BoolProperty(name="Apply Modifiers", description="Use transformed mesh data from each object", default=True) + triangulate = BoolProperty(name="Triangulate", description="Triangulate quads.", default=True) + + def execute(self, context): + from . import export_raw + export_raw.write(self.filepath, + self.apply_modifiers, + self.triangulate, + ) + + return {'FINISHED'} + def invoke(self, context, event): + if not self.filepath: + self.filepath = bpy.path.ensure_ext(bpy.data.filepath, ".raw") + wm = context.window_manager + wm.fileselect_add(self) + return {'RUNNING_MODAL'} -import bpy def menu_import(self, context): - self.layout.operator(import_raw.RawImporter.bl_idname, text="Raw Faces (.raw)").filepath = "*.raw" + self.layout.operator(RawImporter.bl_idname, text="Raw Faces (.raw)") def menu_export(self, context): - import os - default_path = os.path.splitext(bpy.data.filepath)[0] + ".raw" - self.layout.operator(export_raw.RawExporter.bl_idname, text="Raw Faces (.raw)").filepath = default_path + self.layout.operator(RawExporter.bl_idname, text="Raw Faces (.raw)") def register(): diff --git a/io_mesh_raw/export_raw.py b/io_mesh_raw/export_raw.py index 3bc794610252f7c3db121325bda5a1e344e2ec37..6a332fd5431461fb23da5521387c4455dc751818 100644 --- a/io_mesh_raw/export_raw.py +++ b/io_mesh_raw/export_raw.py @@ -16,6 +16,8 @@ # # ##### END GPL LICENSE BLOCK ##### +# <pep8 compliant> + __author__ = ["Aurel Wildfellner"] __version__ = '0.2' __bpydoc__ = """\ @@ -38,9 +40,9 @@ import bpy def faceToTriangles(face): triangles = [] - if (len(face) == 4): #quad - triangles.append( [ face[0], face[1], face[2] ] ) - triangles.append( [ face[2], face[3], face[0] ] ) + if (len(face) == 4): + triangles.append([face[0], face[1], face[2]]) + triangles.append([face[2], face[3], face[0]]) else: triangles.append(face) @@ -50,28 +52,35 @@ def faceToTriangles(face): def faceValues(face, mesh, matrix): fv = [] for verti in face.vertices: - fv.append(mesh.vertices[verti].co * matrix) + fv.append((mesh.vertices[verti].co * matrix)[:]) return fv def faceToLine(face): - line = "" - for v in face: - line += str(v[0]) + " " + str(v[1]) + " " + str(v[2]) + " " - return line[:-1] + "\n" + return " ".join([("%.6f %.6f %.6f" % v) for v in face] + ["\n"]) -def export_raw(filepath, applyMods, triangulate): - faces = [] - for obj in bpy.context.selected_objects: - if obj.type == 'MESH': - matrix = obj.matrix_world +def write(filepath, + applyMods=True, + triangulate=True, + ): - if (applyMods): - me = obj.to_mesh(bpy.context.scene, True, "PREVIEW") - else: - me = obj.data + scene = bpy.context.scene + faces = [] + for obj in bpy.context.selected_objects: + if applyMods or obj.type != 'MESH': + try: + me = obj.to_mesh(scene, True, "PREVIEW") + except: + me = None + is_tmp_mesh = True + else: + me = obj.data + is_tmp_mesh = False + + if me is not None: + matrix = obj.matrix_world.copy() for face in me.faces: fv = faceValues(face, me, matrix) if triangulate: @@ -79,34 +88,11 @@ def export_raw(filepath, applyMods, triangulate): else: faces.append(fv) + if is_tmp_mesh: + bpy.data.meshes.remove(me) + # write the faces to a file file = open(filepath, "w") for face in faces: file.write(faceToLine(face)) file.close() - - -from bpy.props import * - - -class RawExporter(bpy.types.Operator): - '''Save Raw triangle mesh data''' - bl_idname = "export_mesh.raw" - bl_label = "Export RAW" - - filepath = StringProperty(name="File Path", description="Filepath used for exporting the RAW file", maxlen= 1024, default= "", subtype='FILE_PATH') - check_existing = BoolProperty(name="Check Existing", description="Check and warn on overwriting existing files", default=True, options={'HIDDEN'}) - - apply_modifiers = BoolProperty(name="Apply Modifiers", description="Use transformed mesh data from each object", default=True) - triangulate = BoolProperty(name="Triangulate", description="Triangulate quads.", default=True) - - def execute(self, context): - export_raw(self.filepath, self.apply_modifiers, self.triangulate) - return {'FINISHED'} - - def invoke(self, context, event): - wm = context.window_manager - wm.fileselect_add(self) - return {'RUNNING_MODAL'} - -# package manages registering diff --git a/io_mesh_raw/import_raw.py b/io_mesh_raw/import_raw.py index 047245449292eeefa4ac569094fd0793b8f7d397..deda75e2608c808b2d355a3de97221efec9cd336 100644 --- a/io_mesh_raw/import_raw.py +++ b/io_mesh_raw/import_raw.py @@ -16,6 +16,8 @@ # # ##### END GPL LICENSE BLOCK ##### +# <pep8 compliant> + __author__ = ["Anthony D'Agostino (Scorpius)", "Aurel Wildfellner"] __version__ = '0.2' __bpydoc__ = """\ @@ -39,11 +41,10 @@ tolerance. """ - import bpy # move those to a utility modul -from bpy_extras.io_utils import unpack_face_list, unpack_list # TODO, make generic +from bpy_extras.io_utils import unpack_face_list, unpack_list def readMesh(filename, objName): @@ -51,18 +52,14 @@ def readMesh(filename, objName): def line_to_face(line): # Each triplet is an xyz float - line_split = [] + line_split = line.split() try: - line_split = list(map(float, line.split())) + line_split_float = map(float, line_split) except: return None - if len(line_split) == 9: # Tri - f1, f2, f3, f4, f5, f6, f7, f8, f9 = line_split - return [(f1, f2, f3), (f4, f5, f6), (f7, f8, f9)] - elif len(line_split) == 12: # Quad - f1, f2, f3, f4, f5, f6, f7, f8, f9, A, B, C = line_split - return [(f1, f2, f3), (f4, f5, f6), (f7, f8, f9), (A, B, C)] + if len(line_split) in {9, 12}: + return zip(*[iter(line_split_float)] * 3) # group in 3's else: return None @@ -80,7 +77,7 @@ def readMesh(filename, objName): coords = {} index_tot = 0 faces_indices = [] - + for f in faces: fi = [] for i, v in enumerate(f): @@ -118,28 +115,8 @@ def addMeshObj(mesh, objName): scn.objects.active = nobj -from bpy.props import * - -class RawImporter(bpy.types.Operator): - '''Load Raw triangle mesh data''' - bl_idname = "import_mesh.raw" - bl_label = "Import RAW" - - filepath = StringProperty(name="File Path", description="Filepath used for importing the RAW file", maxlen=1024, default="", subtype='FILE_PATH') - - def execute(self, context): - - #convert the filename to an object name - objName = bpy.path.display_name(self.filepath.split("\\")[-1].split("/")[-1]) - - mesh = readMesh(self.filepath, objName) - addMeshObj(mesh, objName) - - return {'FINISHED'} - - def invoke(self, context, event): - wm = context.window_manager - wm.fileselect_add(self) - return {'RUNNING_MODAL'} - -# package manages registering +def read(filepath): + #convert the filename to an object name + objName = bpy.path.display_name_from_filepath(filepath) + mesh = readMesh(filepath, objName) + addMeshObj(mesh, objName)