[Bf-extensions-cvs] [2d8e9ac2] master: io_mesh_raw: moved to contrib: T63750

meta-androcto noreply at git.blender.org
Fri May 24 04:19:26 CEST 2019


Commit: 2d8e9ac2ff2904bb78635787ad88dd637a6d8743
Author: meta-androcto
Date:   Fri May 24 12:19:06 2019 +1000
Branches: master
https://developer.blender.org/rBAC2d8e9ac2ff2904bb78635787ad88dd637a6d8743

io_mesh_raw: moved to contrib: T63750

===================================================================

A	io_mesh_raw/__init__.py
A	io_mesh_raw/export_raw.py
A	io_mesh_raw/import_raw.py

===================================================================

diff --git a/io_mesh_raw/__init__.py b/io_mesh_raw/__init__.py
new file mode 100644
index 00000000..9219efa2
--- /dev/null
+++ b/io_mesh_raw/__init__.py
@@ -0,0 +1,121 @@
+# ##### BEGIN GPL LICENSE BLOCK #####
+#
+#  This program is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU General Public License
+#  as published by the Free Software Foundation; either version 2
+#  of the License, or (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software Foundation,
+#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+# <pep8-80 compliant>
+
+
+bl_info = {
+    "name": "Raw mesh format (.raw)",
+    "author": "Anthony D,Agostino (Scorpius), Aurel Wildfellner",
+    "version": (0, 2),
+    "blender": (2, 57, 0),
+    "location": "File > Import-Export > Raw Faces (.raw) ",
+    "description": "Import-Export Raw Faces",
+    "warning": "",
+    "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
+                "Scripts/Import-Export/Raw_Mesh_IO",
+    "category": "Import-Export",
+}
+
+if "bpy" in locals():
+    import importlib
+    if "import_raw" in locals():
+        importlib.reload(import_raw)
+    if "export_raw" in locals():
+        importlib.reload(export_raw)
+else:
+    import bpy
+
+from bpy.props import StringProperty, BoolProperty
+from bpy_extras.io_utils import ExportHelper
+
+
+class RawImporter(bpy.types.Operator):
+    """Load Raw triangle mesh data"""
+    bl_idname = "import_mesh.raw"
+    bl_label = "Import RAW"
+    bl_options = {'UNDO'}
+
+    filepath: StringProperty(
+            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, ExportHelper):
+    """Save Raw triangle mesh data"""
+    bl_idname = "export_mesh.raw"
+    bl_label = "Export RAW"
+
+    filename_ext = ".raw"
+    filter_glob: StringProperty(default="*.raw", 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 menu_import(self, context):
+    self.layout.operator(RawImporter.bl_idname, text="Raw Faces (.raw)")
+
+
+def menu_export(self, context):
+    self.layout.operator(RawExporter.bl_idname, text="Raw Faces (.raw)")
+
+
+def register():
+    bpy.utils.register_module(__name__)
+
+    bpy.types.TOPBAR_MT_file_import.append(menu_import)
+    bpy.types.TOPBAR_MT_file_export.append(menu_export)
+
+
+def unregister():
+    bpy.utils.unregister_module(__name__)
+
+    bpy.types.TOPBAR_MT_file_import.remove(menu_import)
+    bpy.types.TOPBAR_MT_file_export.remove(menu_export)
+
+if __name__ == "__main__":
+    register()
diff --git a/io_mesh_raw/export_raw.py b/io_mesh_raw/export_raw.py
new file mode 100644
index 00000000..ed34e3ae
--- /dev/null
+++ b/io_mesh_raw/export_raw.py
@@ -0,0 +1,101 @@
+# ##### BEGIN GPL LICENSE BLOCK #####
+#
+#  This program is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU General Public License
+#  as published by the Free Software Foundation; either version 2
+#  of the License, or (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software Foundation,
+#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+# <pep8-80 compliant>
+
+"""
+This script exports a Mesh to a RAW triangle format file.
+
+The raw triangle format is very simple; it has no verts or faces lists.
+It's just a simple ascii text file with the vertices of each triangle
+listed on each line. In addition, also quads can be exported as a line
+of 12 values (this was the default before blender 2.5). Now default
+settings will triangulate the mesh.
+
+Usage:
+Execute this script from the "File->Export" menu. You can select
+whether modifiers should be applied and if the mesh is triangulated.
+
+"""
+
+import bpy
+
+
+def faceToTriangles(face):
+    triangles = []
+    if len(face) == 4:
+        triangles.append([face[0], face[1], face[2]])
+        triangles.append([face[2], face[3], face[0]])
+    else:
+        triangles.append(face)
+
+    return triangles
+
+
+def faceValues(face, mesh, matrix):
+    fv = []
+    for verti in face.vertices:
+        fv.append((matrix * mesh.vertices[verti].co)[:])
+    return fv
+
+
+def faceToLine(face):
+    return " ".join([("%.6f %.6f %.6f" % v) for v in face] + ["\n"])
+
+
+def write(filepath,
+          applyMods=True,
+          triangulate=True,
+          ):
+
+    scene = bpy.context.scene
+    depsgraph = bpy.context.evaluated_depsgraph_get()
+
+    faces = []
+    for obj in bpy.context.selected_objects:
+        obj_eval = None
+        if applyMods or obj.type != 'MESH':
+            try:
+                obj_eval = obj.evaluated_get(depsgraph)
+                me = obj_eval.to_mesh()
+            except:
+                me = None
+            is_tmp_mesh = True
+        else:
+            me = obj.data
+            if not me.tessfaces and me.polygons:
+                me.calc_tessface()
+            is_tmp_mesh = False
+
+        if me is not None:
+            matrix = obj.matrix_world.copy()
+            for face in me.tessfaces:
+                fv = faceValues(face, me, matrix)
+                if triangulate:
+                    faces.extend(faceToTriangles(fv))
+                else:
+                    faces.append(fv)
+
+            if is_tmp_mesh:
+                obj_eval.to_mesh_clear()
+
+    # write the faces to a file
+    file = open(filepath, "w")
+    for face in faces:
+        file.write(faceToLine(face))
+    file.close()
diff --git a/io_mesh_raw/import_raw.py b/io_mesh_raw/import_raw.py
new file mode 100644
index 00000000..093ee7a9
--- /dev/null
+++ b/io_mesh_raw/import_raw.py
@@ -0,0 +1,116 @@
+# ##### BEGIN GPL LICENSE BLOCK #####
+#
+#  This program is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU General Public License
+#  as published by the Free Software Foundation; either version 2
+#  of the License, or (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software Foundation,
+#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+# <pep8-80 compliant>
+
+"""
+This script imports Raw Triangle File format files to Blender.
+
+The raw triangle format is very simple; it has no verts or faces lists.
+It's just a simple ascii text file with the vertices of each triangle
+listed on each line. In addition, a line with 12 values will be
+imported as a quad. This may be in conflict with some other
+applications, which use a raw format, but this is how it was
+implemented back in blender 2.42.
+
+Usage:
+Execute this script from the "File->Import" menu and choose a Raw file to
+open.
+
+Notes:
+Generates the standard verts and faces lists, but without duplicate
+verts. Only *exact* duplicates are removed, there is no way to specify a
+tolerance.
+"""
+
+
+import bpy
+
+
+def readMesh(filename, objName):
+    filehandle = open(filename, "rb")
+
+    def line_to_face(line):
+        # Each triplet is an xyz float
+        line_split = line.split()
+        try:
+            line_split_float = map(float, line_split)
+        except:
+            return None
+
+        if len(line_split) in {9, 12}:
+            return zip(*[iter(line_split_float)] * 3)  # group in 3's
+        else:
+            return None
+
+    faces = []
+    for line in filehandle.readlines():
+        face = line_to_face(line)
+        if face:
+            faces.append(face)
+
+    filehandle.close()
+
+    # Generate verts and faces lists, without duplicates
+    verts = []
+    coords = {}
+    index_tot = 0
+    faces_indices = []
+
+    for f in faces:
+        fi = []
+        for i, v in enumerate(f):
+            index = coords.get(v)
+
+            if index is None:
+                index = coords[v] = index_tot
+                index_tot += 1
+                verts.append(v)
+
+            fi.append(index)
+
+        faces_indices.append(fi)
+
+    mesh = bpy.data.meshes.new(objName)
+    mesh.from_pydata(verts, [], faces_indices)
+
+    return mesh
+
+
+def addMeshObj(mesh, objName):
+    scn = bpy.context.scene
+
+    for o in scn.objects:
+        o.select_set(False)
+
+    mesh.update()
+   

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list