Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikhail Rachinskiy <mikhail.rachinskiy@gmail.com>2019-10-15 16:10:51 +0300
committerMikhail Rachinskiy <mikhail.rachinskiy@gmail.com>2019-10-15 16:10:51 +0300
commitf4c101d692eee45484025762d77df751538d0da4 (patch)
treed63f7d45e9ead45afb9eeb30d70b5b4fdf0954a1 /io_mesh_ply
parent71565f82b24c27dbb45eadd8ca6c494996267039 (diff)
PLY: cleanup use f-strings, with statement
F-strings are not only more readable, they also give around 10% performance increase comparing to modulo formatting. Use with statement instead of file open/close methods, safer and better practice.
Diffstat (limited to 'io_mesh_ply')
-rw-r--r--io_mesh_ply/export_ply.py106
1 files changed, 61 insertions, 45 deletions
diff --git a/io_mesh_ply/export_ply.py b/io_mesh_ply/export_ply.py
index 693d2f3d..cd50dbaf 100644
--- a/io_mesh_ply/export_ply.py
+++ b/io_mesh_ply/export_ply.py
@@ -34,9 +34,6 @@ def save_mesh(filepath, mesh, use_normals=True, use_uv_coords=True, use_colors=T
def rvec2d(v):
return round(v[0], 6), round(v[1], 6)
- file = open(filepath, "w", encoding="utf8", newline="\n")
- fw = file.write
-
has_uv = bool(mesh.uv_layers)
has_vcol = bool(mesh.vertex_colors)
@@ -125,54 +122,73 @@ def save_mesh(filepath, mesh, use_normals=True, use_uv_coords=True, use_colors=T
pf.append(pf_vidx)
- fw("ply\n")
- fw("format ascii 1.0\n")
- fw("comment Created by Blender %s - "
- "www.blender.org, source file: %r\n" %
- (bpy.app.version_string, os.path.basename(bpy.data.filepath)))
+ with open(filepath, "w", encoding="utf-8", newline="\n") as file:
+ fw = file.write
- fw("element vertex %d\n" % len(ply_verts))
+ # Header
+ # ---------------------------
- fw("property float x\n"
- "property float y\n"
- "property float z\n")
+ fw("ply\n")
+ fw("format ascii 1.0\n")
+ fw(
+ f"comment Created by Blender {bpy.app.version_string} - "
+ f"www.blender.org, source file: {os.path.basename(bpy.data.filepath)!r}\n"
+ )
+
+ fw(f"element vertex {len(ply_verts)}\n")
+
+ fw(
+ "property float x\n"
+ "property float y\n"
+ "property float z\n"
+ )
- if use_normals:
- fw("property float nx\n"
- "property float ny\n"
- "property float nz\n")
- if use_uv_coords:
- fw("property float s\n"
- "property float t\n")
- if use_colors:
- fw("property uchar red\n"
- "property uchar green\n"
- "property uchar blue\n"
- "property uchar alpha\n")
-
- fw("element face %d\n" % len(mesh.polygons))
- fw("property list uchar uint vertex_indices\n")
- fw("end_header\n")
-
- for i, v in enumerate(ply_verts):
- fw("%.6f %.6f %.6f" % mesh_verts[v[0]].co[:]) # co
if use_normals:
- fw(" %.6f %.6f %.6f" % v[1]) # no
+ fw(
+ "property float nx\n"
+ "property float ny\n"
+ "property float nz\n"
+ )
if use_uv_coords:
- fw(" %.6f %.6f" % v[2]) # uv
+ fw(
+ "property float s\n"
+ "property float t\n"
+ )
if use_colors:
- fw(" %u %u %u %u" % v[3]) # col
- fw("\n")
-
- for pf in ply_faces:
- # fw(f"{len(pf)} {' '.join(str(x) for x in pf)}\n")
- fw("%d" % len(pf))
- for v in pf:
- fw(" %d" % v)
- fw("\n")
-
- file.close()
- print("writing %r done" % filepath)
+ fw(
+ "property uchar red\n"
+ "property uchar green\n"
+ "property uchar blue\n"
+ "property uchar alpha\n"
+ )
+
+ fw(f"element face {len(mesh.polygons)}\n")
+ fw("property list uchar uint vertex_indices\n")
+ fw("end_header\n")
+
+ # Vertex data
+ # ---------------------------
+
+ for i, v in enumerate(ply_verts):
+ fw("%.6f %.6f %.6f" % mesh_verts[v[0]].co[:])
+ if use_normals:
+ fw(" %.6f %.6f %.6f" % v[1])
+ if use_uv_coords:
+ fw(" %.6f %.6f" % v[2])
+ if use_colors:
+ fw(" %u %u %u %u" % v[3])
+ fw("\n")
+
+ # Face data
+ # ---------------------------
+
+ for pf in ply_faces:
+ fw(f"{len(pf)}")
+ for v in pf:
+ fw(f" {v}")
+ fw("\n")
+
+ print(f"Writing {filepath!r} done")
return {'FINISHED'}