Skip to content

Commit

Permalink
use new DuplicateObject method of LuxCore scene for duplis
Browse files Browse the repository at this point in the history
  • Loading branch information
Theverat committed Jan 13, 2018
1 parent fa4fdd9 commit 6f749a9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 16 deletions.
69 changes: 60 additions & 9 deletions export/__init__.py
@@ -1,4 +1,5 @@
from time import time from time import time
from array import array
import bpy import bpy
from ..bin import pyluxcore from ..bin import pyluxcore
from .. import utils from .. import utils
Expand Down Expand Up @@ -192,16 +193,63 @@ def create_session(self, engine, scene, context=None):


if obj.is_duplicator: if obj.is_duplicator:
print("Duplicator") print("Duplicator")
start = time()

mode = 'VIEWPORT' if context else 'RENDER' mode = 'VIEWPORT' if context else 'RENDER'
obj.dupli_list_create(scene, settings=mode) obj.dupli_list_create(scene, settings=mode)
for dupli in obj.dupli_list:
dupli_name_suffix = '%s_%s_%3d' % (obj.name, dupli.object.name, dupli.index)
matrix = dupli.object.matrix_world.inverted()*dupli.matrix.copy()
#matrix = Matrix.Scale(1/obj.scale[0], 4)*dupli.matrix.copy()
self._convert_object(scene_props, dupli.object, scene, context, luxcore_scene, True, dupli_name_suffix, utils.matrix_to_list(matrix, scene, True))

obj.dupli_list_clear()


name_prefix = utils.get_unique_luxcore_name(obj)
exported = {}

class Duplis:
def __init__(self, exported_obj, matrix):
self.exported_obj = exported_obj
self.matrices = matrix
self.count = 1

def add(self, matrix):
self.matrices.extend(matrix)
self.count += 1

for dupli in obj.dupli_list:
# Use the utils functions to build names so linked objects work (libraries)
name = name_prefix + utils.get_unique_luxcore_name(dupli.object)
matrix_list = utils.matrix_to_list(dupli.matrix, scene, apply_worldscale=True)

try:
# Already exported, just update the Duplis info
exported[name].add(matrix_list)
except KeyError:
# Not yet exported
name_suffix = name_prefix + str(dupli.index)
if dupli.particle_system:
name_suffix += utils.to_luxcore_name(dupli.particle_system.name)

exported_obj = self._convert_object(scene_props, dupli.object, scene, context,
luxcore_scene, update_mesh=True,
dupli_suffix=name_suffix)
print("exported:", name)
exported[name] = Duplis(exported_obj, matrix_list)
print("exported_obj:", exported_obj.luxcore_names)

obj.dupli_list_clear()
# Need to parse so we have the dupli objects available for DuplicateObject
luxcore_scene.Parse(scene_props)

for duplis in exported.values():
# Objects might be split if they have multiple materials
for src_name in duplis.exported_obj.luxcore_names:
dst_name = src_name + "dupli"
count = duplis.count
transformations = array("f", duplis.matrices)
luxcore_scene.DuplicateObject(src_name, dst_name, count, transformations)

# TODO: support steps and times (motion blur)
# steps = 0 # TODO
# times = array("f", [])
# luxcore_scene.DuplicateObject(src_name, dst_name, count, steps, times, transformations)

print("Dupli export took %.3fs" % (time() - start))




# Objects are the most expensive to export, so they dictate the progress # Objects are the most expensive to export, so they dictate the progress
Expand Down Expand Up @@ -313,7 +361,8 @@ def update(self, context, session, changes):
# because it might have been replaced in _update_config() # because it might have been replaced in _update_config()
return session return session


def _convert_object(self, props, obj, scene, context, luxcore_scene, update_mesh=False, dupli_name_suffix="", matrix=None): def _convert_object(self, props, obj, scene, context, luxcore_scene,
update_mesh=False, dupli_suffix="", matrix=None):
key = utils.make_key(obj) key = utils.make_key(obj)
old_exported_obj = None old_exported_obj = None


Expand All @@ -326,7 +375,8 @@ def _convert_object(self, props, obj, scene, context, luxcore_scene, update_mesh
old_exported_obj = self.exported_objects[key] old_exported_obj = self.exported_objects[key]


# Note: exported_obj can also be an instance of ExportedLight, but they behave the same # Note: exported_obj can also be an instance of ExportedLight, but they behave the same
obj_props, exported_obj = blender_object.convert(obj, scene, context, luxcore_scene, old_exported_obj, update_mesh, dupli_name_suffix, matrix) obj_props, exported_obj = blender_object.convert(obj, scene, context, luxcore_scene, old_exported_obj,
update_mesh, dupli_suffix, matrix)


if exported_obj is None: if exported_obj is None:
# Object is not visible or an error happened. # Object is not visible or an error happened.
Expand All @@ -335,6 +385,7 @@ def _convert_object(self, props, obj, scene, context, luxcore_scene, update_mesh


props.Set(obj_props) props.Set(obj_props)
self.exported_objects[key] = exported_obj self.exported_objects[key] = exported_obj
return exported_obj


def _update_config(self, session, config_props): def _update_config(self, session, config_props):
renderconfig = session.GetRenderConfig() renderconfig = session.GetRenderConfig()
Expand Down
13 changes: 6 additions & 7 deletions export/blender_object.py
Expand Up @@ -6,9 +6,9 @@
from . import material from . import material
from .light import convert_lamp from .light import convert_lamp


def convert(blender_obj, scene, context, luxcore_scene, exported_object=None, update_mesh=False, dupli_name_suffix='', matrix=None): def convert(blender_obj, scene, context, luxcore_scene,
is_dupli = len(dupli_name_suffix) > 0 exported_object=None, update_mesh=False, dupli_suffix="", matrix=None):

if not utils.is_obj_visible(blender_obj, scene, context): if not utils.is_obj_visible(blender_obj, scene, context):
return pyluxcore.Properties(), None return pyluxcore.Properties(), None


Expand All @@ -18,7 +18,7 @@ def convert(blender_obj, scene, context, luxcore_scene, exported_object=None, up
try: try:
print("converting object:", blender_obj.name) print("converting object:", blender_obj.name)
# Note that his is not the final luxcore_name, as the object may be split by DefineBlenderMesh() # Note that his is not the final luxcore_name, as the object may be split by DefineBlenderMesh()
luxcore_name = utils.to_luxcore_name(blender_obj.name+dupli_name_suffix) luxcore_name = utils.get_unique_luxcore_name(blender_obj) + dupli_suffix
props = pyluxcore.Properties() props = pyluxcore.Properties()


if blender_obj.data is None: if blender_obj.data is None:
Expand All @@ -37,15 +37,14 @@ def convert(blender_obj, scene, context, luxcore_scene, exported_object=None, up
print(blender_obj.name + ": No mesh data after to_mesh()") print(blender_obj.name + ": No mesh data after to_mesh()")
return props, None return props, None


print(matrix)
mesh_definitions = _convert_mesh_to_shapes(luxcore_name, mesh, luxcore_scene, matrix) mesh_definitions = _convert_mesh_to_shapes(luxcore_name, mesh, luxcore_scene, matrix)
bpy.data.meshes.remove(mesh, do_unlink=False) bpy.data.meshes.remove(mesh, do_unlink=False)
else: else:
assert exported_object is not None assert exported_object is not None
print(blender_obj.name + ": Using cached mesh") print(blender_obj.name + ": Using cached mesh")
mesh_definitions = exported_object.mesh_definitions mesh_definitions = exported_object.mesh_definitions


transformation = utils.matrix_to_list(blender_obj.matrix_world, scene) transformation = utils.matrix_to_list(blender_obj.matrix_world, scene, apply_worldscale=True)


for lux_object_name, material_index in mesh_definitions: for lux_object_name, material_index in mesh_definitions:
if material_index < len(blender_obj.material_slots): if material_index < len(blender_obj.material_slots):
Expand Down Expand Up @@ -83,7 +82,7 @@ def _define_luxcore_object(props, lux_object_name, lux_material_name, transforma
props.Set(pyluxcore.Property(prefix + "transformation", transformation)) props.Set(pyluxcore.Property(prefix + "transformation", transformation))




def _convert_mesh_to_shapes(name, mesh, luxcore_scene, transformation = None): def _convert_mesh_to_shapes(name, mesh, luxcore_scene, transformation=None):
faces = mesh.tessfaces[0].as_pointer() faces = mesh.tessfaces[0].as_pointer()
vertices = mesh.vertices[0].as_pointer() vertices = mesh.vertices[0].as_pointer()


Expand Down

0 comments on commit 6f749a9

Please sign in to comment.