Skip to content
Snippets Groups Projects
Commit 6a5423cc authored by Campbell Barton's avatar Campbell Barton
Browse files

code cleanup and minor re-arrangements to raw import/export.

parent a8fd59a0
No related branches found
No related tags found
No related merge requests found
...@@ -16,6 +16,9 @@ ...@@ -16,6 +16,9 @@
# #
# ##### END GPL LICENSE BLOCK ##### # ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
bl_info = { bl_info = {
"name": "Raw mesh format (.raw)", "name": "Raw mesh format (.raw)",
"author": "Anthony D,Agostino (Scorpius), Aurel Wildfellner", "author": "Anthony D,Agostino (Scorpius), Aurel Wildfellner",
...@@ -33,23 +36,69 @@ bl_info = { ...@@ -33,23 +36,69 @@ bl_info = {
if "bpy" in locals(): if "bpy" in locals():
import imp import imp
imp.reload(import_raw) if "import_raw" in locals():
imp.reload(export_raw) imp.reload(import_raw)
if "export_raw" in locals():
imp.reload(export_raw)
else: else:
from . import import_raw import bpy
from . import export_raw
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): 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): def menu_export(self, context):
import os self.layout.operator(RawExporter.bl_idname, text="Raw Faces (.raw)")
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
def register(): def register():
......
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
# #
# ##### END GPL LICENSE BLOCK ##### # ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
__author__ = ["Aurel Wildfellner"] __author__ = ["Aurel Wildfellner"]
__version__ = '0.2' __version__ = '0.2'
__bpydoc__ = """\ __bpydoc__ = """\
...@@ -38,9 +40,9 @@ import bpy ...@@ -38,9 +40,9 @@ import bpy
def faceToTriangles(face): def faceToTriangles(face):
triangles = [] triangles = []
if (len(face) == 4): #quad if (len(face) == 4):
triangles.append( [ face[0], face[1], face[2] ] ) triangles.append([face[0], face[1], face[2]])
triangles.append( [ face[2], face[3], face[0] ] ) triangles.append([face[2], face[3], face[0]])
else: else:
triangles.append(face) triangles.append(face)
...@@ -50,28 +52,35 @@ def faceToTriangles(face): ...@@ -50,28 +52,35 @@ def faceToTriangles(face):
def faceValues(face, mesh, matrix): def faceValues(face, mesh, matrix):
fv = [] fv = []
for verti in face.vertices: for verti in face.vertices:
fv.append(mesh.vertices[verti].co * matrix) fv.append((mesh.vertices[verti].co * matrix)[:])
return fv return fv
def faceToLine(face): def faceToLine(face):
line = "" return " ".join([("%.6f %.6f %.6f" % v) for v in face] + ["\n"])
for v in face:
line += str(v[0]) + " " + str(v[1]) + " " + str(v[2]) + " "
return line[:-1] + "\n"
def export_raw(filepath, applyMods, triangulate): def write(filepath,
faces = [] applyMods=True,
for obj in bpy.context.selected_objects: triangulate=True,
if obj.type == 'MESH': ):
matrix = obj.matrix_world
if (applyMods): scene = bpy.context.scene
me = obj.to_mesh(bpy.context.scene, True, "PREVIEW")
else:
me = obj.data
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: for face in me.faces:
fv = faceValues(face, me, matrix) fv = faceValues(face, me, matrix)
if triangulate: if triangulate:
...@@ -79,34 +88,11 @@ def export_raw(filepath, applyMods, triangulate): ...@@ -79,34 +88,11 @@ def export_raw(filepath, applyMods, triangulate):
else: else:
faces.append(fv) faces.append(fv)
if is_tmp_mesh:
bpy.data.meshes.remove(me)
# write the faces to a file # write the faces to a file
file = open(filepath, "w") file = open(filepath, "w")
for face in faces: for face in faces:
file.write(faceToLine(face)) file.write(faceToLine(face))
file.close() 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
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
# #
# ##### END GPL LICENSE BLOCK ##### # ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
__author__ = ["Anthony D'Agostino (Scorpius)", "Aurel Wildfellner"] __author__ = ["Anthony D'Agostino (Scorpius)", "Aurel Wildfellner"]
__version__ = '0.2' __version__ = '0.2'
__bpydoc__ = """\ __bpydoc__ = """\
...@@ -39,11 +41,10 @@ tolerance. ...@@ -39,11 +41,10 @@ tolerance.
""" """
import bpy import bpy
# move those to a utility modul # 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): def readMesh(filename, objName):
...@@ -51,18 +52,14 @@ def readMesh(filename, objName): ...@@ -51,18 +52,14 @@ def readMesh(filename, objName):
def line_to_face(line): def line_to_face(line):
# Each triplet is an xyz float # Each triplet is an xyz float
line_split = [] line_split = line.split()
try: try:
line_split = list(map(float, line.split())) line_split_float = map(float, line_split)
except: except:
return None return None
if len(line_split) == 9: # Tri if len(line_split) in {9, 12}:
f1, f2, f3, f4, f5, f6, f7, f8, f9 = line_split return zip(*[iter(line_split_float)] * 3) # group in 3's
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)]
else: else:
return None return None
...@@ -80,7 +77,7 @@ def readMesh(filename, objName): ...@@ -80,7 +77,7 @@ def readMesh(filename, objName):
coords = {} coords = {}
index_tot = 0 index_tot = 0
faces_indices = [] faces_indices = []
for f in faces: for f in faces:
fi = [] fi = []
for i, v in enumerate(f): for i, v in enumerate(f):
...@@ -118,28 +115,8 @@ def addMeshObj(mesh, objName): ...@@ -118,28 +115,8 @@ def addMeshObj(mesh, objName):
scn.objects.active = nobj scn.objects.active = nobj
from bpy.props import * def read(filepath):
#convert the filename to an object name
class RawImporter(bpy.types.Operator): objName = bpy.path.display_name_from_filepath(filepath)
'''Load Raw triangle mesh data''' mesh = readMesh(filepath, objName)
bl_idname = "import_mesh.raw" addMeshObj(mesh, objName)
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment