コード例 #1
0
    def _infuse_material(material: Material, config: Config):
        """
        Infuse a material inside of another material. The given material, will be adapted and the used material, will
        be added, depending on the mode either as add or as mix. This change is applied to all outputs of the material,
        this include the Surface (Color) and also the displacement and volume. For displacement mix means multiply.

        :param material: Used material
        :param config: Used config
        """
        # determine the mode
        used_mode = config.get_string("mode", "mix")

        mix_strength = 0.0
        if used_mode == "mix":
            mix_strength = config.get_float("mix_strength", 0.5)
        elif used_mode == "add" and config.has_param("mix_strength"):
            raise Exception(
                "The mix_strength only works in the mix mode not in the add mode!"
            )

        # get the material, which will be used to infuse the given material
        used_materials = config.get_list("used_material")
        used_material = random.choice(used_materials)

        material.infuse_material(material=Material(used_material),
                                 mode=used_mode,
                                 mix_strength=mix_strength)
コード例 #2
0
    def _infuse_texture(material: Material, config: Config):
        """
        Overlays the selected material with a texture, this can be either a color texture like for example dirt or
        it can be a texture, which is used as an input to the Principled BSDF of the given material.

        :param material: Material, which will be changed
        :param config: containing the config information
        """
        used_mode = config.get_string("mode", "overlay")
        used_textures = config.get_list("used_texture")
        invert_texture = config.get_bool("invert_texture", False)
        used_connector = config.get_string("connection", "Base Color")
        texture_scale = config.get_float("texture_scale", 0.05)
        strength = config.get_float("strength", 0.5)

        if config.has_param("strength") and used_mode == "set":
            raise Exception(
                "The strength can only be used if the mode is not \"set\"!")

        if len(used_textures) == 0:
            raise Exception(
                f"You have to select a texture, which is {used_mode} over the material!"
            )
        texture = random.choice(used_textures)

        material.infuse_texture(texture=texture,
                                mode=used_mode,
                                invert_texture=invert_texture,
                                connection=used_connector,
                                texture_scale=texture_scale,
                                strength=strength)
コード例 #3
0
    def _cam2world_matrix_from_cam_extrinsics(self,
                                              config: Config) -> np.ndarray:
        """ Determines camera extrinsics by using the given config and returns them in form of a cam to world frame transformation matrix.

        :param config: The configuration object.
        :return: The 4x4 cam to world transformation matrix.
        """
        if not config.has_param("cam2world_matrix"):
            # Print warning if local_frame_change is used with other attributes than cam2world_matrix
            if self.local_frame_change != ["X", "Y", "Z"]:
                print(
                    "Warning: The local_frame_change parameter is at the moment only supported when setting the cam2world_matrix attribute."
                )

            position = change_coordinate_frame_of_point(
                config.get_vector3d("location", [0, 0, 0]),
                self.world_frame_change)

            # Rotation
            rotation_format = config.get_string("rotation/format", "euler")
            value = config.get_vector3d("rotation/value", [0, 0, 0])
            # Transform to blender coord frame
            value = change_coordinate_frame_of_point(value,
                                                     self.world_frame_change)

            if rotation_format == "euler":
                # Rotation, specified as euler angles
                rotation_matrix = Euler(value, 'XYZ').to_matrix()
            elif rotation_format == "forward_vec":
                # Convert forward vector to euler angle (Assume Up = Z)
                rotation_matrix = CameraUtility.rotation_from_forward_vec(
                    value)
            elif rotation_format == "look_at":
                # Convert forward vector to euler angle (Assume Up = Z)
                rotation_matrix = CameraUtility.rotation_from_forward_vec(
                    value - position)
            else:
                raise Exception("No such rotation format:" +
                                str(rotation_format))

            if rotation_format == "look_at" or rotation_format == "forward_vec":
                inplane_rot = config.get_float("rotation/inplane_rot", 0.0)
                rotation_matrix = np.matmul(
                    rotation_matrix,
                    Euler((0.0, 0.0, inplane_rot)).to_matrix())

            cam2world_matrix = build_transformation_mat(
                position, rotation_matrix)
        else:
            cam2world_matrix = np.array(
                config.get_list("cam2world_matrix")).reshape(4, 4).astype(
                    np.float32)
            cam2world_matrix = change_source_coordinate_frame_of_transformation_matrix(
                cam2world_matrix, self.local_frame_change)
            cam2world_matrix = change_target_coordinate_frame_of_transformation_matrix(
                cam2world_matrix, self.world_frame_change)
        return cam2world_matrix
コード例 #4
0
    def _set_cam_intrinsics(self, cam, config):
        """ Sets camera intrinsics from a source with following priority

           1. from config function parameter if defined
           2. from custom properties of cam if set in Loader
           3. default config:
                resolution_x/y: 512
                pixel_aspect_x: 1
                clip_start:   : 0.1
                clip_end      : 1000
                fov           : 0.691111

        :param cam: The camera which contains only camera specific attributes.
        :param config: A configuration object with cam intrinsics.
        """
        if config.is_empty():
            return

        width = config.get_int("resolution_x",
                               bpy.context.scene.render.resolution_x)
        height = config.get_int("resolution_y",
                                bpy.context.scene.render.resolution_y)

        # Clipping
        clip_start = config.get_float("clip_start", cam.clip_start)
        clip_end = config.get_float("clip_end", cam.clip_end)

        if config.has_param("cam_K"):
            if config.has_param("fov"):
                print(
                    'WARNING: FOV defined in config is ignored. Mutually exclusive with cam_K'
                )
            if config.has_param("pixel_aspect_x"):
                print(
                    'WARNING: pixel_aspect_x defined in config is ignored. Mutually exclusive with cam_K'
                )

            cam_K = np.array(config.get_list("cam_K")).reshape(3, 3).astype(
                np.float32)

            CameraUtility.set_intrinsics_from_K_matrix(cam_K, width, height,
                                                       clip_start, clip_end)
        else:
            # Set FOV
            fov = config.get_float("fov", cam.angle)

            # Set Pixel Aspect Ratio
            pixel_aspect_x = config.get_float(
                "pixel_aspect_x", bpy.context.scene.render.pixel_aspect_x)
            pixel_aspect_y = config.get_float(
                "pixel_aspect_y", bpy.context.scene.render.pixel_aspect_y)

            # Set camera shift
            shift_x = config.get_float("shift_x", cam.shift_x)
            shift_y = config.get_float("shift_y", cam.shift_y)

            CameraUtility.set_intrinsics_from_blender_params(fov,
                                                             width,
                                                             height,
                                                             clip_start,
                                                             clip_end,
                                                             pixel_aspect_x,
                                                             pixel_aspect_y,
                                                             shift_x,
                                                             shift_y,
                                                             lens_unit="FOV")

        CameraUtility.set_stereo_parameters(
            config.get_string("stereo_convergence_mode",
                              cam.stereo.convergence_mode),
            config.get_float("convergence_distance",
                             cam.stereo.convergence_distance),
            config.get_float("interocular_distance",
                             cam.stereo.interocular_distance))
        if config.has_param("depth_of_field"):
            depth_of_field_config = Config(
                config.get_raw_dict("depth_of_field"))
            fstop_value = depth_of_field_config.get_float("fstop", 2.4)
            aperture_blades = depth_of_field_config.get_int(
                "aperture_blades", 0)
            aperture_ratio = depth_of_field_config.get_float(
                "aperture_ratio", 1.0)
            aperture_rotation = depth_of_field_config.get_float(
                "aperture_rotation_in_rad", 0.0)
            if depth_of_field_config.has_param(
                    "depth_of_field_dist") and depth_of_field_config.has_param(
                        "focal_object"):
                raise RuntimeError(
                    "You can only use either depth_of_field_dist or a focal_object but not both!"
                )
            if depth_of_field_config.has_param("depth_of_field_dist"):
                depth_of_field_dist = depth_of_field_config.get_float(
                    "depth_of_field_dist")
                CameraUtility.add_depth_of_field(cam, None, fstop_value,
                                                 aperture_blades,
                                                 aperture_rotation,
                                                 aperture_ratio,
                                                 depth_of_field_dist)
            elif depth_of_field_config.has_param("focal_object"):
                focal_object = depth_of_field_config.get_list("focal_object")
                if len(focal_object) != 1:
                    raise RuntimeError(
                        f"There has to be exactly one focal object, use 'random_samples: 1' or change "
                        f"the selector. Found {len(focal_object)}.")
                CameraUtility.add_depth_of_field(Entity(focal_object[0]),
                                                 fstop_value, aperture_blades,
                                                 aperture_rotation,
                                                 aperture_ratio)
            else:
                raise RuntimeError(
                    "The depth_of_field dict must contain either a focal_object definition or "
                    "a depth_of_field_dist")