/
mesh.py
405 lines (314 loc) · 16.7 KB
/
mesh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#
# This source file is part of appleseed.
# Visit http://appleseedhq.net/ for additional information and resources.
#
# This software is released under the MIT license.
#
# Copyright (c) 2014-2018 The appleseedhq Organization
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
import os
import bmesh
import bpy
import appleseed as asr
from .assethandlers import AssetType
from .object import ObjectTranslator
from .translator import ObjectKey, ProjectExportMode
from ..logger import get_logger
from ..utils.util import is_object_deforming
logger = get_logger()
class MeshTranslator(ObjectTranslator):
#
# Constructor.
#
def __init__(self, obj, export_mode, asset_handler):
super(MeshTranslator, self).__init__(obj, asset_handler)
self.__export_mode = export_mode
if self.__export_mode == ProjectExportMode.PROJECT_EXPORT:
self.__geom_dir = self.asset_handler.geometry_dir
self.__mesh_filenames = []
# Motion blur
self.__key_index = 0
self.__deforming = obj.appleseed.use_deformation_blur and is_object_deforming(obj)
# Materials
self.__front_materials = {}
self.__back_materials = {}
self.__alpha_tex = None
self.__alpha_tex_inst = None
#
# Entity translation.
#
def create_entities(self, scene):
logger.debug("Creating mesh entities for object %s", self.bl_obj.name)
# Materials
mesh_key = str(ObjectKey(self.bl_obj.data)) + "_obj"
mesh_name = mesh_key
asr_obj_props = self.bl_obj.appleseed
self.__obj_params = {'alpha_map': asr_obj_props.object_alpha}
if self.bl_obj.appleseed.object_alpha_texture is not None:
filename = self.asset_handler.process_path(asr_obj_props.object_alpha_texture.filepath, AssetType.TEXTURE_ASSET)
tex_inst_params = {'addressing_mode': asr_obj_props.object_alpha_texture_wrap_mode,
'filtering_mode': 'bilinear',
'alpha_mode': asr_obj_props.object_alpha_mode}
self.__alpha_tex = asr.Texture('disk_texture_2d', mesh_name + "_tex",
{'filename': filename,
'color_space': asr_obj_props.object_alpha_texture_colorspace}, [])
self.__alpha_tex_inst = asr.TextureInstance(mesh_name + "_tex_inst", tex_inst_params, mesh_name + "_tex",
asr.Transformf(asr.Matrix4f.identity()))
self.__obj_params['alpha_map'] = mesh_name + "_tex_inst"
material_slots = self.bl_obj.material_slots
if len(material_slots) > 1:
for i, m in enumerate(material_slots):
if m.material.appleseed.osl_node_tree is not None:
mat_key = str(ObjectKey(m.material)) + "_mat"
self.__front_materials["slot-%s" % i] = mat_key
else:
self.__front_materials["slot-%s" % i] = "__default_material"
else:
if len(material_slots) == 1:
if material_slots[0].material.appleseed.osl_node_tree is not None:
mat_key = str(ObjectKey(material_slots[0].material)) + "_mat"
self.__front_materials["default"] = mat_key
else:
self.__front_materials["default"] = "__default_material"
else:
logger.debug("Mesh %s has no materials, assigning default material instead", mesh_name)
self.__front_materials["default"] = "__default_material"
double_sided_materials = False if self.bl_obj.appleseed.double_sided is False else True
if double_sided_materials:
self.__back_materials = self.__front_materials
def set_deform_key(self, scene, time, key_times):
# Don't save keys for non deforming meshes.
if not self.__deforming and self.__key_index > 0:
logger.debug("Skipping mesh key for non deforming object %s", self.bl_obj.name)
return
mesh_key = str(ObjectKey(self.bl_obj.data)) + "_obj"
mesh_name = mesh_key
me = self.__get_blender_mesh(scene)
if self.__export_mode == ProjectExportMode.PROJECT_EXPORT:
# Write a mesh file for the mesh key.
logger.debug("Writing mesh file object %s, time = %s", self.bl_obj.name, time)
self.__mesh_object = asr.MeshObject(mesh_name, self.__obj_params)
self.__convert_mesh(me)
self.__write_mesh(mesh_key)
else:
if self.__key_index == 0:
# First key, convert the mesh and reserve keys.
logger.debug("Converting mesh object %s", self.bl_obj.name)
self.__mesh_object = asr.MeshObject(mesh_name, self.__obj_params)
self.__convert_mesh(me)
if self.__deforming:
self.__mesh_object.set_motion_segment_count(len(key_times) - 1)
else:
# Set vertex and normal poses.
logger.debug("Setting mesh key for object %s, time = %s", self.bl_obj.name, time)
self.__set_mesh_key(me, self.__key_index)
bpy.data.meshes.remove(me)
self.__key_index += 1
def flush_entities(self, assembly):
# Compute tangents if needed.
if self.__export_mode != ProjectExportMode.PROJECT_EXPORT:
if self.bl_obj.data.appleseed.smooth_tangents and self.bl_obj.data.appleseed.export_uvs:
asr.compute_smooth_vertex_tangents(self.__mesh_object)
asr_obj_props = self.bl_obj.appleseed
mesh_name = self.__mesh_object.get_name()
object_instance_params = {'visibility': {'camera': asr_obj_props.camera_visible,
'light': asr_obj_props.light_visible,
'shadow': asr_obj_props.shadow_visible,
'diffuse': asr_obj_props.diffuse_visible,
'glossy': asr_obj_props.glossy_visible,
'specular': asr_obj_props.specular_visible,
'transparency': asr_obj_props.transparency_visible},
'medium_priority': asr_obj_props.medium_priority,
'photon_target': asr_obj_props.photon_target,
'ray_bias_method': asr_obj_props.object_ray_bias_method,
'ray_bias_distance': asr_obj_props.object_ray_bias_distance}
if asr_obj_props.object_sss_set != "":
object_instance_params['sss_set_id'] = asr_obj_props.object_sss_set
if self.__export_mode == ProjectExportMode.PROJECT_EXPORT:
# Replace the MeshObject by an empty one referencing
# the binarymesh files we saved before.
params = {}
if len(self.__mesh_filenames) == 1:
# No motion blur. Write a single filename.
params['filename'] = "_geometry/" + self.__mesh_filenames[0]
else:
# Motion blur. Write one filename per motion pose.
params['filename'] = {}
for i, f in enumerate(self.__mesh_filenames):
params['filename'][str(i)] = "_geometry/" + f
self.__mesh_object = asr.MeshObject(mesh_name, params)
self._xform_seq.optimize()
logger.debug(
"Flushing object %s, num instances = %s, num xform keys = %s",
self.appleseed_name,
self._num_instances,
self._xform_seq.size())
if self.__export_mode == ProjectExportMode.INTERACTIVE_RENDER:
# We always create assemblies when doing IPR to allow quick xform edits.
needs_assembly = True
else:
# Only create an assembly if the object is instanced or has xform motion blur.
needs_assembly = self._num_instances > 1 or self._xform_seq.size() > 1
if needs_assembly:
logger.debug("Creating assembly for object %s, name: %s", mesh_name, self.assembly_name)
ass = asr.Assembly(self.assembly_name)
logger.debug("Creating object instance for object %s, name: %s", mesh_name, self.appleseed_name)
obj_inst = asr.ObjectInstance(
self.appleseed_name,
object_instance_params,
self.__object_instance_mesh_name(mesh_name),
asr.Transformd(asr.Matrix4d().identity()),
self.__front_materials,
self.__back_materials)
ass.objects().insert(self.__mesh_object)
self.__mesh_object = ass.objects().get_by_name(mesh_name)
obj_inst_name = obj_inst.get_name()
ass.object_instances().insert(obj_inst)
self.__obj_inst = ass.object_instances().get_by_name(obj_inst_name)
assembly_instance_name = self.assembly_name + "_inst"
logger.debug("Creating assembly instance for object %s, name: %s", mesh_name, assembly_instance_name)
ass_name = self._insert_entity_with_unique_name(assembly.assemblies(), ass, ass.get_name())
self.__ass = assembly.assemblies().get_by_name(ass_name)
ass_inst = asr.AssemblyInstance(
assembly_instance_name,
{},
ass_name)
ass_inst.set_transform_sequence(self._xform_seq)
ass_inst_name = self._insert_entity_with_unique_name(assembly.assembly_instances(), ass_inst, ass_inst.get_name())
self.__ass_inst = assembly.assembly_instances().get_by_name(ass_inst_name)
if self.__alpha_tex is not None:
self.__ass.textures().insert(self.__alpha_tex)
if self.__alpha_tex_inst is not None:
self.__ass.texture_instances().insert(self.__alpha_tex_inst)
else:
logger.debug("Creating object instance for object %s, name: %s", mesh_name, self.appleseed_name)
mesh_name = self._insert_entity_with_unique_name(assembly.objects(), self.__mesh_object, mesh_name)
self.__mesh_object = assembly.objects().get_by_name(mesh_name)
obj_inst = asr.ObjectInstance(
self.appleseed_name,
object_instance_params,
self.__object_instance_mesh_name(mesh_name),
self._xform_seq.get_earliest_transform(),
self.__front_materials,
self.__back_materials)
obj_inst_name = self._insert_entity_with_unique_name(assembly.object_instances(), obj_inst, obj_inst.get_name())
self.__obj_inst = assembly.object_instances().get_by_name(obj_inst_name)
if self.__alpha_tex is not None:
assembly.textures().insert(self.__alpha_tex)
if self.__alpha_tex_inst is not None:
assembly.texture_instances().insert(self.__alpha_tex_inst)
def update_transform(self, time, matrix):
self.__ass_inst.transform_sequence().set_transform(time, self._convert_matrix(matrix))
#
# Internal methods.
#
def __get_blender_mesh(self, scene):
settings = 'RENDER' if self.__export_mode != ProjectExportMode.INTERACTIVE_RENDER else 'PREVIEW'
me = self.bl_obj.to_mesh(
scene,
apply_modifiers=True,
settings=settings,
calc_tessface=False)
bm = bmesh.new()
bm.from_mesh(me)
bmesh.ops.triangulate(bm, faces=bm.faces)
bm.to_mesh(me)
bm.free()
return me
def __convert_mesh(self, me):
material_slots = self.bl_obj.material_slots
self.__mesh_object.reserve_material_slots(len(material_slots))
if len(material_slots) > 1:
for i, m in enumerate(material_slots):
self.__mesh_object.push_material_slot("slot-%s" % i)
else:
self.__mesh_object.push_material_slot("default")
do_normals = self.bl_obj.data.appleseed.export_normals
if do_normals is True and not self.bl_obj.data.has_custom_normals:
me.calc_normals()
me.split_faces()
me.calc_tessface()
vertex_pointer = me.vertices[0].as_pointer()
tessface_pointer = me.tessfaces[0].as_pointer()
vertices_length = len(me.vertices)
tessface_length = len(me.tessfaces)
do_uvs = False
uv_layer_pointer = 0
if self.bl_obj.data.appleseed.export_uvs and len(me.uv_textures) > 0:
do_uvs = True
uv_textures = me.tessface_uv_textures
for uv in uv_textures:
if uv.active_render:
active_uv = uv
break
uv_layer_pointer = active_uv.data[0].as_pointer()
asr.export_mesh_blender79(self.__mesh_object,
vertices_length,
vertex_pointer,
tessface_length,
tessface_pointer,
uv_layer_pointer,
do_normals,
do_uvs)
def __set_mesh_key(self, me, key_index):
pose = key_index - 1
do_normals = self.bl_obj.data.appleseed.export_normals
if do_normals is True and not self.bl_obj.data.has_custom_normals:
me.calc_normals()
me.split_faces()
me.calc_tessface()
vertex_pointer = me.vertices[0].as_pointer()
vertices_length = len(me.vertices)
asr.export_mesh_blender79_pose(self.__mesh_object,
pose,
vertices_length,
vertex_pointer,
do_normals)
def __write_mesh(self, mesh_name):
# Compute tangents if needed.
if self.bl_obj.data.appleseed.smooth_tangents and self.bl_obj.data.appleseed.export_uvs:
asr.compute_smooth_vertex_tangents(self.__mesh_object)
# Compute the mesh signature and the mesh filename.
hash = asr.MurmurHash()
asr.compute_signature(hash, self.__mesh_object)
logger.debug("Mesh info:")
logger.debug(" get_triangle_count %s", self.__mesh_object.get_triangle_count())
logger.debug(" get_material_slot_count %s", self.__mesh_object.get_material_slot_count())
logger.debug(" get_vertex_count %s", self.__mesh_object.get_vertex_count())
logger.debug(" get_tex_coords_count %s", self.__mesh_object.get_tex_coords_count())
logger.debug(" get_vertex_normal_count %s", self.__mesh_object.get_vertex_normal_count())
logger.debug(" get_vertex_tangent_count %s", self.__mesh_object.get_vertex_tangent_count())
logger.debug(" get_motion_segment_count %s", self.__mesh_object.get_motion_segment_count())
logger.debug("Computed mesh signature for object %s, hash: %s", self.appleseed_name, hash)
# Save the mesh filename for later use.
mesh_filename = str(hash) + ".binarymesh"
self.__mesh_filenames.append(mesh_filename)
# Write the binarymesh file.
mesh_abs_path = os.path.join(self.__geom_dir, mesh_filename)
if not os.path.exists(mesh_abs_path):
logger.debug("Writing mesh for object %s to %s", mesh_name, mesh_abs_path)
asr.MeshObjectWriter.write(self.__mesh_object, "mesh", mesh_abs_path)
else:
logger.debug("Skipping already saved mesh file for mesh %s", mesh_name)
def __object_instance_mesh_name(self, mesh_name):
if self.__export_mode == ProjectExportMode.PROJECT_EXPORT:
return mesh_name + ".mesh"
return mesh_name