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, triangulate=True) 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 create_entities(self, depsgraph, num_def_times): logger.debug(f"appleseed: Creating mesh entity for {self.orig_name}") self.__mesh_params = self.__get_mesh_params() self.__as_mesh = asr.MeshObject(self.orig_name, self.__mesh_params) self.__as_mesh_inst_params = self.__get_mesh_inst_params() self.__front_materials, self.__back_materials = self.__get_material_mappings( ) eval_object = self._bl_obj.evaluated_get(depsgraph) me = eval_object.to_mesh() self.__convert_mesh(me) if self.__export_mode == ProjectExportMode.PROJECT_EXPORT: logger.debug( f"appleseed: Writing mesh file object {self.orig_name}, time = 0" ) self.__write_mesh(self.orig_name) eval_object.to_mesh_clear() if self.__is_deforming: self.__as_mesh.set_motion_segment_count(num_def_times - 1)
def create_entities(self, bl_scene, textures_to_add, as_texture_translators): logger.debug("Creating entity for %s", self.appleseed_name) mesh_name = f"{self.appleseed_name}_obj" mesh_params = self.__get_mesh_params(textures_to_add, as_texture_translators) self.__as_mesh = asr.MeshObject(mesh_name, mesh_params) self.__as_mesh_inst_params = self.__get_mesh_inst_params() self.__front_materials, self.__back_materials = self.__get_material_mappings( )
def create_entities(self, scene): lamp_data = self.bl_lamp.data as_lamp_data = lamp_data.appleseed # Create area light mesh shape shape_params = {'primitive': as_lamp_data.area_shape} if as_lamp_data.area_shape == 'grid': shape_params['width'] = self.bl_lamp.data.size shape_params['height'] = self.bl_lamp.data.size if lamp_data.shape == 'RECTANGLE': shape_params['height'] = self.bl_lamp.data.size_y elif as_lamp_data.area_shape == 'disk': shape_params['radius'] = self.bl_lamp.data.size / 2 else: shape_params['radius'] = self.bl_lamp.data.size / 2 shape_params['resolution_u'] = 4 shape_params['resolution_v'] = 4 mesh_name = self.bl_lamp.name + "_mesh" if self.__export_mode == ProjectExportMode.PROJECT_EXPORT: self.__as_area_mesh = asr.MeshObject(mesh_name, shape_params) else: self.__as_area_mesh = asr.create_primitive_mesh( mesh_name, shape_params) # Create area light object instance, set visibility flags lamp_inst_params = { 'visibility': { 'camera': False } } if not as_lamp_data.area_visibility else {} self.__as_area_mesh_inst = asr.ObjectInstance( self.bl_lamp.name + '_inst', lamp_inst_params, mesh_name, self._convert_matrix(self.bl_lamp.matrix_world), {"default": lamp_data.name + "_mat"}, {"default": "__null_material"}) # Emit basic lamp shader group if lamp_data.appleseed.osl_node_tree is None: self.__create_material(as_lamp_data, lamp_data)
def build_project(): # Create an empty project. project = asr.Project('test project') # Add default configurations to the project. project.add_default_configurations() # Set the number of samples. This is basically the quality parameter: the higher the number # of samples, the smoother the image but the longer the rendering time. # todo: fix. conf = project.configurations()['final'] conf.insert_path('uniform_pixel_renderer.samples', 16) # Create a scene. scene = asr.Scene() # Create an assembly. assembly = asr.Assembly("assembly") #------------------------------------------------------------------------ # Materials #------------------------------------------------------------------------ for i in range(0, 10): assembly.bsdfs().insert( asr.BSDF( "glossy_brdf", "glossy" + str(i), { "mdf": "ggx", "reflectance": 1.0, "roughness": i / 9.0, "energy_compensation": 0.0 })) assembly.bsdfs().insert( asr.BSDF( "glossy_brdf", "glossy_ec" + str(i), { "mdf": "ggx", "reflectance": 1.0, "roughness": i / 9.0, "energy_compensation": 1.0 })) for i in range(0, 10): assembly.materials().insert( asr.Material("generic_material", "mat" + str(i), {"bsdf": "glossy" + str(i)})) assembly.materials().insert( asr.Material("generic_material", "mat_ec" + str(i), {"bsdf": "glossy_ec" + str(i)})) #------------------------------------------------------------------------ # Geometry #------------------------------------------------------------------------ object_name = "sphere" object = asr.MeshObject(object_name, { "primitive": "sphere", "radius": 0.4 }) assembly.objects().insert(object) obj_instance_params = {'visibility': {"glossy": False, "shadow": False}} for i in range(0, 10): instance_name = object_name + "_inst" + str(i) material_names = {"default": "mat" + str(i)} mat = asr.Matrix4d.make_translation(asr.Vector3d(-5.0 + i, -0.5, 0.0)) instance = asr.ObjectInstance(instance_name, obj_instance_params, object_name, asr.Transformd(mat), material_names) assembly.object_instances().insert(instance) for i in range(0, 10): instance_name = object_name + "_ec_inst" + str(i) material_names = {"default": "mat_ec" + str(i)} mat = asr.Matrix4d.make_translation(asr.Vector3d(-5.0 + i, 0.5, 0.0)) instance = asr.ObjectInstance(instance_name, obj_instance_params, object_name, asr.Transformd(mat), material_names) assembly.object_instances().insert(instance) #------------------------------------------------------------------------ # Assembly instance #------------------------------------------------------------------------ # Create an instance of the assembly and insert it into the scene. assembly_inst = asr.AssemblyInstance("assembly_inst", {}, assembly.get_name()) assembly_inst.transform_sequence().set_transform( 0.0, asr.Transformd(asr.Matrix4d.identity())) scene.assembly_instances().insert(assembly_inst) # Insert the assembly into the scene. scene.assemblies().insert(assembly) #------------------------------------------------------------------------ # Environment #------------------------------------------------------------------------ # Create a color called "gray" and insert it into the scene. Gray = [0.5, 0.5, 0.5] scene.colors().insert( asr.ColorEntity("gray", { 'color_space': 'linear_rgb', 'multiplier': 1.0 }, Gray)) # Create an environment EDF called "gray_edf" and insert it into the scene. scene.environment_edfs().insert( asr.EnvironmentEDF("constant_environment_edf", "gray_edf", {'radiance': 'gray'})) # Create an environment shader called "gray_shader" and insert it into the scene. scene.environment_shaders().insert( asr.EnvironmentShader("edf_environment_shader", "gray_shader", {'environment_edf': 'gray_edf'})) # Create an environment called "sky" and bind it to the scene. scene.set_environment( asr.Environment("sky", { "environment_edf": "gray_edf", "environment_shader": "gray_shader" })) #------------------------------------------------------------------------ # Camera #------------------------------------------------------------------------ params = { 'film_dimensions': asr.Vector2f(0.0640, 0.0200), 'focal_length': 0.035 } camera = asr.Camera("pinhole_camera", "camera", params) mat = asr.Matrix4d.make_translation( asr.Vector3d(-0.444315058060864, -0.071277492791890, 5.674764299781837)) camera.transform_sequence().set_transform(0.0, asr.Transformd(mat)) # Bind the camera to the scene. scene.cameras().insert(camera) #------------------------------------------------------------------------ # Frame #------------------------------------------------------------------------ # Create a frame and bind it to the project. params = {'camera': 'camera', 'resolution': asr.Vector2i(640, 200)} project.set_frame(asr.Frame("beauty", params)) # Bind the scene to the project. project.set_scene(scene) return project
def flush_entities(self, as_assembly, as_project): logger.debug("Flushing entity for %s", self.appleseed_name) for instance in self.__instances.values(): instance.optimize() mesh_name = f"{self.appleseed_name}_obj" 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.__as_mesh = asr.MeshObject(mesh_name, params) mesh_name = self.__object_instance_mesh_name( f"{self.appleseed_name}_obj") if self.__export_mode == ProjectExportMode.INTERACTIVE_RENDER or len( self.__instances) > 1: self.__has_assembly = True else: for instance in self.__instances.values(): xform_seq = instance if xform_seq.size() > 1: self.__has_assembly = True if self.__has_assembly: self.__as_mesh_inst = asr.ObjectInstance( self.appleseed_name, self.__as_mesh_inst_params, mesh_name, asr.Transformd(asr.Matrix4d().identity()), self.__front_materials, self.__back_materials) ass_name = f"{self.appleseed_name}_ass" self.__ass = asr.Assembly(ass_name) self.__ass.objects().insert(self.__as_mesh) self.__as_mesh = self.__ass.objects().get_by_name(mesh_name) self.__ass.object_instances().insert(self.__as_mesh_inst) self.__as_mesh_inst = self.__ass.object_instances().get_by_name( self.appleseed_name) as_assembly.assemblies().insert(self.__ass) self.__ass = as_assembly.assemblies().get_by_name(ass_name) for key, transform_matrix in self.__instances.items(): ass_inst_name = f"{key}_ass_inst" ass_inst = asr.AssemblyInstance(ass_inst_name, {}, ass_name) ass_inst.set_transform_sequence(transform_matrix) as_assembly.assembly_instances().insert(ass_inst) self.__instances[key] = as_assembly.assembly_instances( ).get_by_name(ass_inst_name) else: self.__as_mesh_inst = asr.ObjectInstance( self.appleseed_name, self.__as_mesh_inst_params, mesh_name, xform_seq.get_earliest_transform(), self.__front_materials, self.__back_materials) as_assembly.objects().insert(self.__as_mesh) self.__as_mesh = as_assembly.objects().get_by_name(mesh_name) as_assembly.object_instances().insert(self.__as_mesh_inst) self.__as_mesh_inst = as_assembly.object_instances().get_by_name( self.appleseed_name)
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, '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 flush_entities(self, as_scene, as_main_assembly, as_project): logger.debug( "appleseed: Flusing mesh entity for {self.orig_name} into project") 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'] = f"_geometry/{self.__mesh_filenames[0]}" else: # Motion blur. Write one filename per motion pose. params['filename'] = dict() for i, f in enumerate(self.__mesh_filenames): params['filename'][str(i)] = f"_geometry/{f}" self.__as_mesh = asr.MeshObject(self.orig_name, params) mesh_name = self.__object_instance_mesh_name(self.orig_name) self.__obj_inst_name = f"{self.orig_name}_inst" self.__instance_lib.optimize_xforms() needs_assembly = self.__export_mode == ProjectExportMode.INTERACTIVE_RENDER or self.__instance_lib.needs_assembly( ) if needs_assembly: self.__ass_name = f"{self.orig_name}_ass" self.__ass = asr.Assembly(self.__ass_name) self.__as_mesh_inst = asr.ObjectInstance( self.__obj_inst_name, self.__as_mesh_inst_params, mesh_name, asr.Transformd(asr.Matrix4d().identity()), self.__front_materials, self.__back_materials) self.__ass.objects().insert(self.__as_mesh) self.__as_mesh = self.__ass.objects().get_by_name(mesh_name) self.__ass.object_instances().insert(self.__as_mesh_inst) self.__as_mesh_inst = self.__ass.object_instances().get_by_name( self.__obj_inst_name) as_main_assembly.assemblies().insert(self.__ass) self.__ass = as_main_assembly.assemblies().get_by_name( self.__ass_name) self.flush_instances(as_main_assembly) else: self.__as_mesh_inst = asr.ObjectInstance( self.__obj_inst_name, self.__as_mesh_inst_params, mesh_name, self.__instance_lib.get_single_transform(), self.__front_materials, self.__back_materials) as_main_assembly.objects().insert(self.__as_mesh) self.__as_mesh = as_main_assembly.objects().get_by_name(mesh_name) as_main_assembly.object_instances().insert(self.__as_mesh_inst) self.__as_mesh_inst = as_main_assembly.object_instances( ).get_by_name(self.__obj_inst_name)