Skip to content

Commit

Permalink
PLY: cleanup use f-strings, with statement
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mrachinskiy committed Oct 15, 2019
1 parent 71565f8 commit f4c101d
Showing 1 changed file with 61 additions and 45 deletions.
106 changes: 61 additions & 45 deletions io_mesh_ply/export_ply.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ def rvec3d(v):
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)

Expand Down Expand Up @@ -125,54 +122,73 @@ def rvec2d(v):

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'}

Expand Down

0 comments on commit f4c101d

Please sign in to comment.