def convert_disk_shape(scene, assembly, element):
    # Radius.
    radius_element = element.find("float[@name='radius']")
    radius = float(radius_element.attrib["value"]) if radius_element is not None else 1.0

    # Object.
    object_name = make_new_object_name(assembly)
    object = asr.create_primitive_mesh(object_name, {
        "primitive": "disk",
        "resolution_u": 1,
        "resolution_v": 32,
        "radius": radius
    })

    # Instance transform.
    matrix = get_matrix(element.find("transform[@name='toWorld']/matrix"))
    rotx = asr.Matrix4d.make_rotation(asr.Vector3d(1.0, 0.0, 0.0), math.radians(90.0))
    matrix = matrix * rotx
    transform = asr.Transformd(matrix)

    # Instance material.
    material_name = process_shape_material(scene, assembly, object_name, element)

    instance = make_object_instance(assembly, object, material_name, transform)
    assembly.object_instances().insert(instance)
    assembly.objects().insert(object)
def convert_sphere_shape(scene, assembly, element):
    # Radius.
    radius_element = element.find("float[@name='radius']")
    radius = float(radius_element.attrib["value"]) if radius_element is not None else 1.0

    # Center.
    center_element = element.find("point[@name='center']")
    center = asr.Vector3d(get_vector(center_element)) if center_element is not None else asr.Vector3d(0.0)

    # Object.
    object_name = make_new_object_name(assembly)
    object = asr.create_primitive_mesh(object_name, {
        "primitive": "sphere",
        "resolution_u": 32,
        "resolution_v": 16,
        "radius": radius
    })

    # Instance transform.
    matrix = asr.Matrix4d.make_translation(center)
    matrix_element = element.find("transform[@name='toWorld']/matrix")
    if matrix_element is not None:
        # todo: no idea what is the right multiplication order, untested.
        matrix = matrix * get_matrix(matrix_element)
    transform = asr.Transformd(matrix)

    # Instance material.
    material_name = process_shape_material(scene, assembly, object_name, element)

    instance = make_object_instance(assembly, object, material_name, transform)
    assembly.object_instances().insert(instance)
    assembly.objects().insert(object)
def convert_disk_shape(scene, assembly, element):
    # Radius.
    radius_element = element.find("float[@name='radius']")
    radius = float(
        radius_element.attrib["value"]) if radius_element is not None else 1.0

    # Object.
    object_name = make_new_object_name(assembly)
    object = asr.create_primitive_mesh(
        object_name, {
            "primitive": "disk",
            "resolution_u": 1,
            "resolution_v": 32,
            "radius": radius
        })

    # Instance transform.
    matrix = get_matrix(element.find("transform[@name='toWorld']/matrix"))
    rotx = asr.Matrix4d.make_rotation(asr.Vector3d(1.0, 0.0, 0.0),
                                      math.radians(90.0))
    matrix = matrix * rotx
    transform = asr.Transformd(matrix)

    # Instance material.
    material_name = process_shape_material(scene, assembly, object_name,
                                           element)

    instance = make_object_instance(assembly, object, material_name, transform)
    assembly.object_instances().insert(instance)
    assembly.objects().insert(object)
def convert_rectangle_shape(scene, assembly, element):
    # Object.
    object_name = make_new_object_name(assembly)
    object = asr.create_primitive_mesh(
        object_name, {
            "primitive": "grid",
            "resolution_u": 1,
            "resolution_v": 1,
            "width": 2.0,
            "height": 2.0
        })

    # Instance transform.
    matrix = get_matrix(element.find("transform[@name='toWorld']/matrix"))
    rotx = asr.Matrix4d.make_rotation(asr.Vector3d(1.0, 0.0, 0.0),
                                      math.radians(90.0))
    matrix = matrix * rotx
    transform = asr.Transformd(matrix)

    # Instance material.
    material_name = process_shape_material(scene, assembly, object_name,
                                           element)

    instance = make_object_instance(assembly, object, material_name, transform)
    assembly.object_instances().insert(instance)
    assembly.objects().insert(object)
Beispiel #5
0
def convert_sphere_shape(scene, assembly, element):
    # Radius.
    radius_element = element.find("float[@name='radius']")
    radius = float(radius_element.attrib["value"]) if radius_element is not None else 1.0

    # Center.
    center_element = element.find("point[@name='center']")
    center = asr.Vector3d(get_vector(center_element)) if center_element is not None else asr.Vector3d(0.0)

    # Object.
    object_name = make_new_object_name(assembly)
    object = asr.create_primitive_mesh(object_name, {
        "primitive": "sphere",
        "resolution_u": 32,
        "resolution_v": 16,
        "radius": radius
    })

    # Instance transform.
    matrix = asr.Matrix4d.make_translation(center)
    matrix_element = element.find("transform[@name='toWorld']/matrix")
    if matrix_element is not None:
        # todo: no idea what is the right multiplication order, untested.
        matrix = matrix * get_matrix(matrix_element)
    transform = asr.Transformd(matrix)

    # Instance material.
    material_name = process_shape_material(scene, assembly, object_name, element)

    instance = make_object_instance(assembly, object, material_name, transform)
    assembly.object_instances().insert(instance)
    assembly.objects().insert(object)
Beispiel #6
0
    def create_entities(self, depsgraph, deforms_length):
        logger.debug(f"appleseed: Creating lamp entity for {self.orig_name}")
        as_lamp_data = self.bl_lamp.data.appleseed

        self.__lamp_model = self.__get_lamp_model()

        if self.bl_lamp.data.type != 'AREA':
            self.__radiance = self._convert_color(as_lamp_data.radiance)
            lamp_radiance_name = f"{self.orig_name}_radiance"

            self.__as_lamp_radiance = asr.ColorEntity(
                lamp_radiance_name, {'color_space': 'linear_rgb'},
                self.__radiance)

            if self.__lamp_model == 'point_light':
                self.__as_lamp_params = self.__get_point_lamp_params()
            if self.__lamp_model == 'spot_light':
                self.__as_lamp_params = self.__get_spot_lamp_params()
            if self.__lamp_model == 'directional_light':
                self.__as_lamp_params = self.__get_directional_lamp_params()
            if self.__lamp_model == 'sun_light':
                self.__as_lamp_params = self.__get_sun_lamp_params()

            self.__as_lamp = asr.Light(self.__lamp_model, self.orig_name,
                                       self.__as_lamp_params)

        else:
            shape_params = self._get_area_mesh_params()
            mesh_name = f"{self.orig_name}_mesh"

            self.__as_area_lamp_mesh = asr.create_primitive_mesh(
                mesh_name, shape_params)

            mat_name = f"{self.orig_name}_mat"

            shader_name = f"{self.orig_name}_tree"

            self.__instance_params = self._get_area_mesh_instance_params()

            if not self.bl_lamp.data.use_nodes:
                shader_name = f"{self.orig_name}_tree"

                self.__as_area_lamp_shadergroup = asr.ShaderGroup(shader_name)
                self._set_shadergroup()
            else:
                self.__node_tree = NodeTreeTranslator(
                    self.bl_lamp.data.node_tree, self._asset_handler,
                    self.orig_name)
                self.__node_tree.create_entities(depsgraph.scene_eval)

            self.__as_area_lamp_material = asr.Material(
                'osl_material', mat_name, {'osl_surface': shader_name})
def convert_cube_shape(scene, assembly, element):
    # Object.
    object_name = make_new_object_name(assembly)
    object = asr.create_primitive_mesh(object_name, {"primitive": "cube"})

    # Instance transform.
    matrix_element = element.find("transform[@name='toWorld']/matrix")
    matrix = get_matrix(matrix_element) if matrix_element is not None else asr.Matrix4d.identity()
    transform = asr.Transformd(matrix)

    # Instance material.
    material_name = process_shape_material(scene, assembly, object_name, element)

    instance = make_object_instance(assembly, object, material_name, transform)
    assembly.object_instances().insert(instance)
    assembly.objects().insert(object)
Beispiel #8
0
def convert_cube_shape(scene, assembly, element):
    # Object.
    object_name = make_new_object_name(assembly)
    object = asr.create_primitive_mesh(object_name, {"primitive": "cube"})

    # Instance transform.
    matrix_element = element.find("transform[@name='toWorld']/matrix")
    matrix = get_matrix(matrix_element) if matrix_element is not None else asr.Matrix4d.identity()
    transform = asr.Transformd(matrix)

    # Instance material.
    material_name = process_shape_material(scene, assembly, object_name, element)

    instance = make_object_instance(assembly, object, material_name, transform)
    assembly.object_instances().insert(instance)
    assembly.objects().insert(object)
Beispiel #9
0
    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)
Beispiel #10
0
    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)
Beispiel #11
0
def convert_rectangle_shape(scene, assembly, element):
    # Object.
    object_name = make_new_object_name(assembly)
    object = asr.create_primitive_mesh(object_name, {
        "primitive": "grid",
        "resolution_u": 1,
        "resolution_v": 1,
        "width": 2.0,
        "height": 2.0
    })

    # Instance transform.
    matrix = get_matrix(element.find("transform[@name='toWorld']/matrix"))
    rotx = asr.Matrix4d.make_rotation(asr.Vector3d(1.0, 0.0, 0.0), math.radians(90.0))
    matrix = matrix * rotx
    transform = asr.Transformd(matrix)

    # Instance material.
    material_name = process_shape_material(scene, assembly, object_name, element)

    instance = make_object_instance(assembly, object, material_name, transform)
    assembly.object_instances().insert(instance)
    assembly.objects().insert(object)
Beispiel #12
0
    def update_lamp(self, depsgraph, as_main_assembly, as_scene, as_project):
        logger.debug(f"appleseed: Updating lamp entity for {self.orig_name}")
        as_lamp_data = self.bl_lamp.data.appleseed

        current_model = self.__lamp_model

        self.__lamp_model = self.__get_lamp_model()

        if current_model == self.__lamp_model:
            if self.__lamp_model != 'area_lamp':
                # Check if the radiance has changed.
                current_radiance = self.__radiance
                self.__radiance = self._convert_color(as_lamp_data.radiance)

                if current_radiance != self.__radiance:
                    as_main_assembly.colors().remove(self.__as_lamp_radiance)

                    lamp_radiance_name = f"{self.orig_name}_radiance"

                    self.__as_lamp_radiance = asr.ColorEntity(
                        lamp_radiance_name, {'color_space': 'linear_rgb'},
                        self.__radiance)

                    as_main_assembly.colors().insert(self.__as_lamp_radiance)
                    self.__as_lamp_radiance = as_main_assembly.colors(
                    ).get_by_name(lamp_radiance_name)

                # Update lamp parameters.
                if self.__lamp_model == 'point_light':
                    self.__as_lamp_params = self.__get_point_lamp_params()
                if self.__lamp_model == 'spot_light':
                    self.__as_lamp_params = self.__get_spot_lamp_params()
                if self.__lamp_model == 'directional_light':
                    self.__as_lamp_params = self.__get_directional_lamp_params(
                    )
                if self.__lamp_model == 'sun_light':
                    self.__as_lamp_params = self.__get_sun_lamp_params()

                self.__as_lamp.set_parameters(self.__as_lamp_params)
            else:
                self.__ass.object_instances().remove(self.__as_area_lamp_inst)
                self.__ass.objects().remove(self.__as_area_lamp_mesh)

                shape_params = self._get_area_mesh_params()
                mesh_name = f"{self.orig_name}_mesh"
                mat_name = f"{self.orig_name}_mat"

                self.__as_area_lamp_mesh = asr.create_primitive_mesh(
                    mesh_name, shape_params)

                self.__instance_params = self._get_area_mesh_instance_params()

                self.__as_area_lamp_inst = asr.ObjectInstance(
                    self.__as_area_lamp_inst_name, self.__instance_params,
                    mesh_name, asr.Transformd(asr.Matrix4d().identity()),
                    {"default": mat_name}, {"default": "__null_material"})

                self.__ass.objects().insert(self.__as_area_lamp_mesh)
                self.__as_area_lamp_mesh = self.__ass.objects().get_by_name(
                    mesh_name)

                self.__ass.object_instances().insert(self.__as_area_lamp_inst)
                self.__as_area_lamp_inst = self.__ass.object_instances(
                ).get_by_name(self.__as_area_lamp_inst_name)

                if self.bl_lamp.data.use_nodes:
                    if self.__as_area_lamp_shadergroup is not None:
                        as_main_assembly.shader_groups().remove(
                            self.__as_area_lamp_shadergroup)
                        self.__as_area_lamp_shadergroup = None
                        self.__node_tree = NodeTreeTranslator(
                            self.bl_lamp.data.node_tree, self._asset_handler,
                            self.orig_name)
                        self.__node_tree.create_entities(depsgraph.scene_eval)
                    else:
                        self.__node_tree.update_nodetree(depsgraph.scene_eval)
                else:
                    if self.__node_tree is not None:
                        self.__node_tree.delete_nodetree(as_main_assembly)
                        self.__node_tree = None

                        shader_name = f"{self.orig_name}_tree"

                        self.__as_area_lamp_shadergroup = asr.ShaderGroup(
                            shader_name)
                        self._set_shadergroup()
                    else:
                        self._set_shadergroup()

        else:
            # Delete current light.
            if current_model != 'area_lamp':
                self.__ass.lights().remove(self.__as_lamp)

                as_main_assembly.colors().remove(self.__as_lamp_radiance)
            else:
                self.__ass.objects().remove(self.__as_area_lamp_mesh)
                self.__ass.object_instances().remove(self.__as_area_lamp_inst)
                as_main_assembly.materials().remove(
                    self.__as_area_lamp_material)

                if self.__as_area_lamp_shadergroup is not None:
                    as_main_assembly.shader_groups().remove(
                        self.__as_area_lamp_shadergroup)
                    self.__as_area_lamp_shadergroup = None
                else:
                    self.__node_tree.delete_nodetree(as_main_assembly)

            # Create new light.
            self.create_entities(depsgraph, 0)

            if self.__lamp_model != 'area_lamp':
                radiance_name = self.__as_lamp_radiance.get_name()
                as_main_assembly.colors().insert(self.__as_lamp_radiance)
                self.__as_lamp_radiance = as_main_assembly.colors(
                ).get_by_name(radiance_name)

                self.__ass.lights().insert(self.__as_lamp)
                self.__as_lamp = self.__ass.lights().get_by_name(
                    self.orig_name)
            else:
                mat_name = f"{self.orig_name}_mat"

                mesh_name = f"{self.orig_name}_mesh"

                self.__as_area_lamp_inst_name = f"{self.orig_name}_inst"

                as_main_assembly.materials().insert(
                    self.__as_area_lamp_material)
                self.__as_area_lamp_material = as_main_assembly.materials(
                ).get_by_name(mat_name)

                if self.__as_area_lamp_shadergroup is not None:
                    shadergroup_name = self.__as_area_lamp_shadergroup.get_name(
                    )
                    as_main_assembly.shader_groups().insert(
                        self.__as_area_lamp_shadergroup)
                    self.__as_area_lamp_shadergroup = as_main_assembly.shader_groups(
                    ).get_by_name(shadergroup_name)
                else:
                    self.__node_tree.flush_entities(as_scene, as_main_assembly,
                                                    as_project)

                self.__as_area_lamp_inst = asr.ObjectInstance(
                    self.__as_area_lamp_inst_name, self.__instance_params,
                    mesh_name, asr.Transformd(asr.Matrix4d().identity()),
                    {"default": mat_name}, {"default": "__null_material"})

                self.__ass.objects().insert(self.__as_area_lamp_mesh)
                self.__as_area_lamp_mesh = self.__ass.objects().get_by_name(
                    mesh_name)

                self.__ass.object_instances().insert(self.__as_area_lamp_inst)
                self.__as_area_lamp_inst = self.__ass.object_instances(
                ).get_by_name(self.__as_area_lamp_inst_name)