Skip to content
Permalink
Browse files

use new DuplicateObject method of LuxCore scene for duplis

  • Loading branch information...
Theverat committed Jan 13, 2018
1 parent fa4fdd9 commit 6f749a9b270856a9adbd83701adaa589341729f6
Showing with 66 additions and 16 deletions.
  1. +60 −9 export/__init__.py
  2. +6 −7 export/blender_object.py
@@ -1,4 +1,5 @@
from time import time
from array import array
import bpy
from ..bin import pyluxcore
from .. import utils
@@ -192,16 +193,63 @@ def create_session(self, engine, scene, context=None):

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

mode = 'VIEWPORT' if context else 'RENDER'
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
@@ -313,7 +361,8 @@ def update(self, context, session, changes):
# because it might have been replaced in _update_config()
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)
old_exported_obj = None

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

# 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:
# Object is not visible or an error happened.
@@ -335,6 +385,7 @@ def _convert_object(self, props, obj, scene, context, luxcore_scene, update_mesh

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

def _update_config(self, session, config_props):
renderconfig = session.GetRenderConfig()
@@ -6,9 +6,9 @@
from . import material
from .light import convert_lamp

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

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

@@ -18,7 +18,7 @@ def convert(blender_obj, scene, context, luxcore_scene, exported_object=None, up
try:
print("converting object:", blender_obj.name)
# 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()

if blender_obj.data is None:
@@ -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()")
return props, None

print(matrix)
mesh_definitions = _convert_mesh_to_shapes(luxcore_name, mesh, luxcore_scene, matrix)
bpy.data.meshes.remove(mesh, do_unlink=False)
else:
assert exported_object is not None
print(blender_obj.name + ": Using cached mesh")
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:
if material_index < len(blender_obj.material_slots):
@@ -83,7 +82,7 @@ def _define_luxcore_object(props, lux_object_name, lux_material_name, transforma
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()
vertices = mesh.vertices[0].as_pointer()

0 comments on commit 6f749a9

Please sign in to comment.
You can’t perform that action at this time.