Some code tidying in Psychoblend.

Specifically:
1. Factored out color exporting.
2. Switched floating point writing to use .format() with 6 digits
   of precision.  The main gain here is that numbers like 1.0
   don't print with a bunch of useless trailing zeros.
This commit is contained in:
Nathan Vegdahl 2019-11-30 23:00:07 +09:00
parent 26b5ee3b55
commit 4207c77e27
4 changed files with 56 additions and 82 deletions

View File

@ -1,6 +1,6 @@
import bpy import bpy
from .util import escape_name, mat2str, needs_def_mb, needs_xform_mb, ExportCancelled from .util import escape_name, mat2str, color2str, psycolor2str, needs_def_mb, needs_xform_mb, ExportCancelled
class Assembly: class Assembly:
def __init__(self, render_engine, objects, visible_layers, group_prefix="", translation_offset=(0,0,0)): def __init__(self, render_engine, objects, visible_layers, group_prefix="", translation_offset=(0,0,0)):
@ -180,21 +180,21 @@ class Mesh:
# Write vertices and (if it's smooth shaded) normals # Write vertices and (if it's smooth shaded) normals
for ti in range(len(self.time_meshes)): for ti in range(len(self.time_meshes)):
w.write("Vertices [") w.write("Vertices [")
w.write(" ".join([("%f" % i) for vert in self.time_meshes[ti].vertices for i in vert.co]), False) w.write(" ".join(["{:.6} {:.6} {:.6}".format(vert.co[0], vert.co[1], vert.co[2]) for vert in self.time_meshes[ti].vertices]), False)
w.write("]\n", False) w.write("]\n", False)
if self.time_meshes[0].polygons[0].use_smooth and self.ob.data.psychopath.is_subdivision_surface == False: if self.time_meshes[0].polygons[0].use_smooth and self.ob.data.psychopath.is_subdivision_surface == False:
w.write("Normals [") w.write("Normals [")
w.write(" ".join([("%f" % i) for vert in self.time_meshes[ti].vertices for i in vert.normal]), False) w.write(" ".join(["{:.6} {:.6} {:.6}".format(vert.normal[0], vert.normal[1], vert.normal[2]) for vert in self.time_meshes[ti].vertices]), False)
w.write("]\n", False) w.write("]\n", False)
# Write face vertex counts # Write face vertex counts
w.write("FaceVertCounts [") w.write("FaceVertCounts [")
w.write(" ".join([("%d" % len(p.vertices)) for p in self.time_meshes[0].polygons]), False) w.write(" ".join(["{}".format(len(p.vertices)) for p in self.time_meshes[0].polygons]), False)
w.write("]\n", False) w.write("]\n", False)
# Write face vertex indices # Write face vertex indices
w.write("FaceVertIndices [") w.write("FaceVertIndices [")
w.write(" ".join([("%d"%v) for p in self.time_meshes[0].polygons for v in p.vertices]), False) w.write(" ".join(["{}".format(v) for p in self.time_meshes[0].polygons for v in p.vertices]), False)
w.write("]\n", False) w.write("]\n", False)
# MeshSurface/SubdivisionSurface section end # MeshSurface/SubdivisionSurface section end
@ -232,14 +232,9 @@ class SphereLamp:
w.write("SphereLight $%s {\n" % self.name) w.write("SphereLight $%s {\n" % self.name)
w.indent() w.indent()
for col in self.time_col: for col in self.time_col:
if col[0] == 'Rec709': w.write(color2str(col[0], col[1]) + "\n")
w.write("Color [rec709, %f %f %f]\n" % (col[1][0], col[1][1], col[1][2]))
elif col[0] == 'Blackbody':
w.write("Color [blackbody, %f %f]\n" % (col[1], col[2]))
elif col[0] == 'ColorTemperature':
w.write("Color [color_temperature, %f %f]\n" % (col[1], col[2]))
for rad in self.time_rad: for rad in self.time_rad:
w.write("Radius [%f]\n" % rad) w.write("Radius [{:.6}]\n".format(rad))
w.unindent() w.unindent()
w.write("}\n") w.write("}\n")
@ -278,14 +273,9 @@ class RectLamp:
w.write("RectangleLight $%s {\n" % self.name) w.write("RectangleLight $%s {\n" % self.name)
w.indent() w.indent()
for col in self.time_col: for col in self.time_col:
if col[0] == 'Rec709': w.write(color2str(col[0], col[1]) + "\n")
w.write("Color [rec709, %f %f %f]\n" % (col[1][0], col[1][1], col[1][2]))
elif col[0] == 'Blackbody':
w.write("Color [blackbody, %f %f]\n" % (col[1], col[2]))
elif col[0] == 'ColorTemperature':
w.write("Color [color_temperature, %f %f]\n" % (col[1], col[2]))
for dim in self.time_dim: for dim in self.time_dim:
w.write("Dimensions [%f %f]\n" % dim) w.write("Dimensions [{:.6} {:.6}]\n".format(dim[0], dim[1]))
w.unindent() w.unindent()
w.write("}\n") w.write("}\n")
@ -338,57 +328,15 @@ class Material:
w.indent() w.indent()
if self.mat.psychopath.surface_shader_type == 'Emit': if self.mat.psychopath.surface_shader_type == 'Emit':
w.write("Type [Emit]\n") w.write("Type [Emit]\n")
if self.mat.psychopath.color_type == 'Rec709': w.write(psycolor2str(self.mat.psychopath) + "\n")
col = self.mat.psychopath.color
w.write("Color [rec709, %f %f %f]\n" % (
col[0], col[1], col[2],
))
elif self.mat.psychopath.color_type == 'Blackbody':
w.write("Color [blackbody, %f %f]\n" % (
self.mat.psychopath.color_blackbody_temp,
1.0,
))
elif self.mat.psychopath.color_type == 'ColorTemperature':
w.write("Color [color_temperature, %f %f]\n" % (
self.mat.psychopath.color_blackbody_temp,
1.0,
))
elif self.mat.psychopath.surface_shader_type == 'Lambert': elif self.mat.psychopath.surface_shader_type == 'Lambert':
w.write("Type [Lambert]\n") w.write("Type [Lambert]\n")
if self.mat.psychopath.color_type == 'Rec709': w.write(psycolor2str(self.mat.psychopath) + "\n")
col = self.mat.psychopath.color
w.write("Color [rec709, %f %f %f]\n" % (
col[0], col[1], col[2],
))
elif self.mat.psychopath.color_type == 'Blackbody':
w.write("Color [blackbody, %f %f]\n" % (
self.mat.psychopath.color_blackbody_temp,
1.0,
))
elif self.mat.psychopath.color_type == 'ColorTemperature':
w.write("Color [color_temperature, %f %f]\n" % (
self.mat.psychopath.color_blackbody_temp,
1.0,
))
elif self.mat.psychopath.surface_shader_type == 'GGX': elif self.mat.psychopath.surface_shader_type == 'GGX':
w.write("Type [GGX]\n") w.write("Type [GGX]\n")
if self.mat.psychopath.color_type == 'Rec709': w.write(psycolor2str(self.mat.psychopath) + "\n")
col = self.mat.psychopath.color w.write("Roughness [{:.6}]\n".format(self.mat.psychopath.roughness))
w.write("Color [rec709, %f %f %f]\n" % ( w.write("Fresnel [{:.6}]\n".format(self.mat.psychopath.fresnel))
col[0], col[1], col[2],
))
elif self.mat.psychopath.color_type == 'Blackbody':
w.write("Color [blackbody, %f %f]\n" % (
self.mat.psychopath.color_blackbody_temp,
1.0,
))
elif self.mat.psychopath.color_type == 'ColorTemperature':
w.write("Color [color_temperature, %f %f]\n" % (
self.mat.psychopath.color_blackbody_temp,
1.0,
))
w.write("Roughness [%f]\n" % self.mat.psychopath.roughness)
w.write("Fresnel [%f]\n" % self.mat.psychopath.fresnel)
else: else:
raise "Unsupported surface shader type '%s'" % self.mat.psychopath.surface_shader_type raise "Unsupported surface shader type '%s'" % self.mat.psychopath.surface_shader_type
w.unindent() w.unindent()

View File

@ -96,7 +96,7 @@ class PsychoExporter:
res_y = int(self.scene.render.resolution_y * (self.scene.render.resolution_percentage / 100)) res_y = int(self.scene.render.resolution_y * (self.scene.render.resolution_percentage / 100))
self.w.write('Resolution [%d %d]\n' % (res_x, res_y)) self.w.write('Resolution [%d %d]\n' % (res_x, res_y))
self.w.write("SamplesPerPixel [%d]\n" % self.scene.psychopath.spp) self.w.write("SamplesPerPixel [%d]\n" % self.scene.psychopath.spp)
self.w.write("DicingRate [%f]\n" % self.scene.psychopath.dicing_rate) self.w.write("DicingRate [{:.6}]\n".format(self.scene.psychopath.dicing_rate))
self.w.write('Seed [%d]\n' % self.fr) self.w.write('Seed [%d]\n' % self.fr)
# RenderSettings section end # RenderSettings section end

View File

@ -11,10 +11,41 @@ def mat2str(m):
s = "" s = ""
for j in range(4): for j in range(4):
for i in range(4): for i in range(4):
s += (" %f" % m[i][j]) s += " {:.6}".format(m[i][j])
return s[1:] return s[1:]
def color2str(color_type, color_data):
if color_type == 'Rec709':
return "Color [rec709, {:.6} {:.6} {:.6}]".format(
color_data[0],
color_data[1],
color_data[2],
)
elif color_type == 'Blackbody':
return "Color [blackbody, {:.6} {:.6}]".format(
color_data[0],
color_data[1],
)
elif color_type == 'ColorTemperature':
return "Color [color_temperature, {:.6} {:.6}]".format(
color_data[0],
color_data[1],
)
def psycolor2str(psymat):
color_type = psymat.color_type
color_data = psymat.color
if color_type == 'Blackbody' or color_type == 'ColorTemperature':
# TODO: add the brightness multiplier to the Psychoblend material
# settings. Here we're just defaulting it to 1.0.
color_data = [psymat.color_blackbody_temp, 1.0]
return color2str(color_type, color_data)
def needs_def_mb(ob): def needs_def_mb(ob):
""" Determines if the given object needs to be exported with """ Determines if the given object needs to be exported with
deformation motion blur or not. deformation motion blur or not.

View File

@ -3,7 +3,7 @@ import bpy
from math import degrees, tan, atan from math import degrees, tan, atan
from mathutils import Vector, Matrix from mathutils import Vector, Matrix
from .util import escape_name, mat2str, ExportCancelled from .util import escape_name, color2str, mat2str, ExportCancelled
class World: class World:
def __init__(self, render_engine, scene, visible_layers, aspect_ratio): def __init__(self, render_engine, scene, visible_layers, aspect_ratio):
@ -90,16 +90,16 @@ class Camera:
w.indent() w.indent()
for fov in self.fovs: for fov in self.fovs:
w.write("Fov [%f]\n" % fov) w.write("Fov [{:.6}]\n".format(fov))
for rad in self.aperture_radii: for rad in self.aperture_radii:
w.write("ApertureRadius [%f]\n" % rad) w.write("ApertureRadius [{:.6}]\n".format(rad))
for dist in self.focal_distances: for dist in self.focal_distances:
w.write("FocalDistance [%f]\n" % dist) w.write("FocalDistance [{:.6}]\n".format(dist))
for mat in self.xforms: for mat in self.xforms:
w.write("Transform [%s]\n" % mat2str(mat)) w.write("Transform [{}]\n".format(mat2str(mat)))
w.unindent() w.unindent()
w.write("}\n") w.write("}\n")
@ -116,7 +116,7 @@ class BackgroundShader:
w.write("BackgroundShader {\n") w.write("BackgroundShader {\n")
w.indent(); w.indent();
w.write("Type [Color]\n") w.write("Type [Color]\n")
w.write("Color [rec709, %f %f %f]\n" % self.color) w.write("Color [rec709, {:.6} {:.6} {:.6}]\n".format(self.color[0], self.color[1], self.color[2]))
w.unindent() w.unindent()
w.write("}\n") w.write("}\n")
@ -147,16 +147,11 @@ class DistantDiskLamp:
w.write("DistantDiskLight $%s {\n" % self.name) w.write("DistantDiskLight $%s {\n" % self.name)
w.indent() w.indent()
for direc in self.time_dir: for direc in self.time_dir:
w.write("Direction [%f %f %f]\n" % (direc[0], direc[1], direc[2])) w.write("Direction [{:.6} {:.6} {:.6}]\n".format(direc[0], direc[1], direc[2]))
for col in self.time_col: for col in self.time_col:
if col[0] == 'Rec709': w.write(color2str(col[0], col[1]) + "\n")
w.write("Color [rec709, %f %f %f]\n" % (col[1][0], col[1][1], col[1][2]))
elif col[0] == 'Blackbody':
w.write("Color [blackbody, %f %f]\n" % (col[1], col[2]))
elif col[0] == 'ColorTemperature':
w.write("Color [color_temperature, %f %f]\n" % (col[1], col[2]))
for rad in self.time_rad: for rad in self.time_rad:
w.write("Radius [%f]\n" % rad) w.write("Radius [{:.6}]\n".format(rad))
w.unindent() w.unindent()
w.write("}\n") w.write("}\n")