Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to modify the built-in function #919

Closed
ccteaher opened this issue Jun 5, 2023 · 2 comments
Closed

How to modify the built-in function #919

ccteaher opened this issue Jun 5, 2023 · 2 comments
Labels
first answer provided question Question, not yet a bug ;)

Comments

@ccteaher
Copy link

ccteaher commented Jun 5, 2023

Describe the issue

Hi, I'm trying to modify the built-in methods in the blenderproc code

such as:

def write_bop() in BopWriterUtility.py 

and def enable_depth_output() in RendererUtility.py

I tried to modify them, but didn't get any results after running them.

How to modify the built-in function

Minimal code example

def enable_depth_output(activate_antialiasing: bool, output_dir: Optional[str] = None, file_prefix: str = "depth_",
                        output_key: str = "depth", antialiasing_distance_max: float = None,
                        convert_to_distance: bool = False):
    """ Enables writing depth images.

    Depth images will be written in the form of .exr files during the next rendering.

    :param activate_antialiasing: If this is True the final image will be antialiased
    :param output_dir: The directory to write files to, if this is None the temporary directory is used.
    :param file_prefix: The prefix to use for writing the files.
    :param output_key: The key to use for registering the depth output.
    :param antialiasing_distance_max: Max distance in which the distance is measured. \
                                      Only if activate_antialiasing is True.
    :param convert_to_distance: If this is true, while loading a postprocessing step is executed to convert this depth \
                                image to a distance image
    """
    if activate_antialiasing:
        return enable_distance_output(activate_antialiasing, output_dir, file_prefix, output_key, antialiasing_distance_max, convert_to_depth=True)
    if output_dir is None:
        output_dir = Utility.get_temporary_directory()

    if GlobalStorage.is_in_storage("depth_output_is_enabled"):
        msg = "The depth enable function can not be called twice. Either you called it twice or you used the " \
              "enable_distance_output with activate_antialiasing=False, which internally calls this function. This is " \
              "currently not supported, but there is an easy way to solve this, you can use the " \
              "bproc.postprocessing.dist2depth and depth2dist function on the output of the renderer and generate " \
              "the antialiased distance image yourself."
        raise Exception(msg)
    GlobalStorage.add("depth_output_is_enabled", True)


    bpy.context.scene.render.use_compositing = True
    bpy.context.scene.use_nodes = True

    tree = bpy.context.scene.node_tree
    links = tree.links
    # Use existing render layer
    render_layer_node = Utility.get_the_one_node_with_type(tree.nodes, 'CompositorNodeRLayers')
    print(111)
    # Enable z-buffer pass
    bpy.context.view_layer.use_pass_z = True

    # Build output node
    output_file = tree.nodes.new("CompositorNodeOutputFile")
    output_file.base_path = output_dir
    output_file.format.file_format = "OPEN_EXR"
    output_file.file_slots.values()[0].path = file_prefix

    # Feed the Z-Buffer output of the render layer to the input of the file IO layer
    links.new(render_layer_node.outputs["Depth"], output_file.inputs['Image'])

    Utility.add_output_entry({
        "key": output_key,
        "path": os.path.join(output_dir, file_prefix) + "%04d" + ".exr",
        "version": "2.0.0",
        "trim_redundant_channels": True,
        "convert_to_distance": convert_to_distance
    })




def write_bop(output_dir: str, target_objects: Optional[List[MeshObject]] = None, depths: Optional[List[np.ndarray]] = None, 
              colors: Optional[List[np.ndarray]] = None, color_file_format: str = "PNG", dataset: str = "", 
              append_to_existing_output: bool = True, depth_scale: float = 1.0, jpg_quality: int = 95, save_world2cam: bool = True,
              ignore_dist_thres: float = 100., m2mm: bool = True, frames_per_chunk: int = 1000):
    """Write the BOP data

    :param output_dir: Path to the output directory.
    :param target_objects: Objects for which to save ground truth poses in BOP format. Default: Save all objects or from specified dataset
    :param depths: List of depth images in m to save
    :param colors: List of color images to save
    :param color_file_format: File type to save color images. Available: "PNG", "JPEG"
    :param jpg_quality: If color_file_format is "JPEG", save with the given quality.
    :param dataset: Only save annotations for objects of the specified bop dataset. Saves all object poses if undefined.
    :param append_to_existing_output: If true, the new frames will be appended to the existing ones.
    :param depth_scale: Multiply the uint16 output depth image with this factor to get depth in mm. Used to trade-off between depth accuracy
        and maximum depth value. Default corresponds to 65.54m maximum depth and 1mm accuracy.
    :param save_world2cam: If true, camera to world transformations "cam_R_w2c", "cam_t_w2c" are saved in scene_camera.json
    :param ignore_dist_thres: Distance between camera and object after which object is ignored. Mostly due to failed physics.
    :param m2mm: Original bop annotations and models are in mm. If true, we convert the gt annotations to mm here. This
        is needed if BopLoader option mm2m is used.
    :param frames_per_chunk: Number of frames saved in each chunk (called scene in BOP)
    """
    if depths is None:
        depths = []
    if colors is None:
        colors = []

    # Output paths.
    dataset_dir = os.path.join(output_dir, dataset)
    chunks_dir = os.path.join(dataset_dir, 'train_pbr')
    camera_path = os.path.join(dataset_dir, 'camera.json')

    # Create the output directory structure.
    if not os.path.exists(dataset_dir):
        os.makedirs(dataset_dir)
        os.makedirs(chunks_dir)
    elif not append_to_existing_output:
        raise Exception("The output folder already exists: {}.".format(dataset_dir))

    # Select target objects or objects from the specified dataset or all objects
    if target_objects is not None:
        dataset_objects = [t_obj.blender_obj for t_obj in target_objects]
    elif dataset:
        dataset_objects = []
        for obj in get_all_blender_mesh_objects():
            if "bop_dataset_name" in obj and not obj.hide_render:
                if obj["bop_dataset_name"] == dataset:
                    dataset_objects.append(obj)
    else:
        dataset_objects = get_all_blender_mesh_objects()

    # Check if there is any object from the specified dataset.
    if not dataset_objects:
        raise Exception("The scene does not contain any object from the "
                        "specified dataset: {}. Either remove the dataset parameter "
                        "or assign custom property 'bop_dataset_name' to selected objects".format(dataset))

    # Save the data.
    BopWriterUtility._write_camera(camera_path, depth_scale=depth_scale)
    BopWriterUtility._write_frames(chunks_dir, dataset_objects=dataset_objects, depths=depths, colors=colors,
                                   color_file_format=color_file_format, frames_per_chunk=frames_per_chunk,
                                   m2mm=m2mm, ignore_dist_thres=ignore_dist_thres, save_world2cam=save_world2cam,
                                   depth_scale=depth_scale, jpg_quality=jpg_quality)

Files required to run the code

No response

Expected behavior

I tried to modify them, but didn't get any results after running them.

How to modify the built-in function

BlenderProc version

3.3.0

@ccteaher ccteaher added the question Question, not yet a bug ;) label Jun 5, 2023
@cornerfarmer
Copy link
Member

cornerfarmer commented Jun 5, 2023

If you install blenderproc via git (see https://github.com/DLR-RM/BlenderProc#via-git), then just change the respective files. Also make sure that in your main.py script you are not involuntarily using another blenderproc installation. You can use print(bproc.__path__) to check that

@cornerfarmer
Copy link
Member

I assume this is solved, if not, feel free to reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
first answer provided question Question, not yet a bug ;)
Projects
None yet
Development

No branches or pull requests

2 participants