class_docstr: An implementation of a rendering backend in Blender/Cycles.
class_docstr_tok: ["An","implementation","of","a","rendering","backend","in","Blender\/Cycles","."]
docstr: Args:
    scene: the kubric scene this class will observe
    scratch_dir: Blender always writes the rendered images to disk. The scratch_dir is the
      (temporary) directory used for that. The results are read into memory by kubric,
      immediately after the rendering is done, so the contents of this directory can
      be discarded afterwards.
    adaptive_sampling: Adjust the number of rays cast based on the complexity of the patch
      (see https://docs.blender.org/manual/en/latest/render/cycles/render_settings/sampling.html)
    use_denoising: Use the blender denoiser to improve the image quality.
      (see https://docs.blender.org/manual/en/latest/render/layers/denoising.html#denoising)
    samples_per_pixel: Number of rays cast per pixel
      (see https://docs.blender.org/manual/en/latest/render/cycles/render_settings/sampling.html)
    background_transparency: Render the background transparent.
      (see https://docs.blender.org/manual/en/latest/render/cycles/render_settings/film.html)
    verbose: when False, blender stdout is redirected to stdnull
    custom_scene: By default (None) Blender is initialized with an empty scene.
      If this argument is set to the path for a `.blend` file, then that scene is loaded instead.
      Note that this scene only affects the rendering output. It is not accessible from Kubric and
      not taken into account by the simulator.
docstr_tok: ["Args",":","scene",":","the","kubric","scene","this","class","will","observe","scratch_dir",":","Blender","always","writes","the","rendered","images","to","disk",".","The","scratch_dir","is","the","(","temporary",")","directory","used","for","that",".","The","results","are","read","into","memory","by","kubric",",","immediately","after","the","rendering","is","done",",","so","the","contents","of","this","directory","can","be","discarded","afterwards",".","adaptive_sampling",":","Adjust","the","number","of","rays","cast","based","on","the","complexity","of","the","patch","(","see","https",":","\/\/docs.blender.org\/manual\/en\/latest\/render\/cycles\/render_settings\/sampling.html",")","use_denoising",":","Use","the","blender","denoiser","to","improve","the","image","quality",".","(","see","https",":","\/\/docs.blender.org\/manual\/en\/latest\/render\/layers\/denoising.html","#","denoising",")","samples_per_pixel",":","Number","of","rays","cast","per","pixel","(","see","https",":","\/\/docs.blender.org\/manual\/en\/latest\/render\/cycles\/render_settings\/sampling.html",")","background_transparency",":","Render","the","background","transparent",".","(","see","https",":","\/\/docs.blender.org\/manual\/en\/latest\/render\/cycles\/render_settings\/film.html",")","verbose",":","when","False",",","blender","stdout","is","redirected","to","stdnull","custom_scene",":","By","default","(","None",")","Blender","is","initialized","with","an","empty","scene",".","If","this","argument","is","set","to","the","path","for","a","`",".blend","`","file",",","then","that","scene","is","loaded","instead",".","Note","that","this","scene","only","affects","the","rendering","output",".","It","is","not","accessible","from","Kubric","and","not","taken","into","account","by","the","simulator","."]
code: def __init__(
      self,
      scene: core.Scene,
      scratch_dir=None,
      adaptive_sampling=False,
      use_denoising=True,
      samples_per_pixel=128,
      background_transparency=False,
      verbose: bool = False,
      custom_scene: Optional[str] = None,
      motion_blur: Optional[float] = None,
  ):
      """
      Args:
        scene: the kubric scene this class will observe
        scratch_dir: Blender always writes the rendered images to disk. The scratch_dir is the
          (temporary) directory used for that. The results are read into memory by kubric,
          immediately after the rendering is done, so the contents of this directory can
          be discarded afterwards.
        adaptive_sampling: Adjust the number of rays cast based on the complexity of the patch
          (see https://docs.blender.org/manual/en/latest/render/cycles/render_settings/sampling.html)
        use_denoising: Use the blender denoiser to improve the image quality.
          (see https://docs.blender.org/manual/en/latest/render/layers/denoising.html#denoising)
        samples_per_pixel: Number of rays cast per pixel
          (see https://docs.blender.org/manual/en/latest/render/cycles/render_settings/sampling.html)
        background_transparency: Render the background transparent.
          (see https://docs.blender.org/manual/en/latest/render/cycles/render_settings/film.html)
        verbose: when False, blender stdout is redirected to stdnull
        custom_scene: By default (None) Blender is initialized with an empty scene.
          If this argument is set to the path for a `.blend` file, then that scene is loaded instead.
          Note that this scene only affects the rendering output. It is not accessible from Kubric and
          not taken into account by the simulator.
      """
      self.scratch_dir = (
          tempfile.mkdtemp() if scratch_dir is None else scratch_dir
      )
      self.ambient_node = None
      self.ambient_hdri_node = None
      self.illum_mapping_node = None
      self.bg_node = None
      self.bg_hdri_node = None
      self.bg_mapping_node = None
      self.verbose = verbose
  
      # blender has a default scene on load, so we clear everything first
      self.clear_and_reset_blender_scene(
          self.verbose, custom_scene=custom_scene
      )
      self.blender_scene = bpy.context.scene
  
      # the ray-tracing engine is set here because it affects the availability of some features
      bpy.context.scene.render.engine = "CYCLES"
      self.use_gpu = os.getenv("KUBRIC_USE_GPU", "False").lower() in (
          "true",
          "1",
          "t",
      )
  
      blender_utils.activate_render_passes(
          normal=True, optical_flow=True, segmentation=True, uv=True
      )
      self._setup_scene_shading()
  
      self.adaptive_sampling = adaptive_sampling  # speeds up rendering
      self.use_denoising = use_denoising  # improves the output quality
      self.samples_per_pixel = samples_per_pixel
      self.background_transparency = background_transparency
  
      self.exr_output_node = blender_utils.set_up_exr_output_node(
          motion_blur=motion_blur
      )
  
      self.post_processors = {
          "backward_flow": blender_utils.process_backward_flow,
          "forward_flow": blender_utils.process_forward_flow,
          "depth": blender_utils.process_depth,
          "z": blender_utils.process_z,
          "uv": blender_utils.process_uv,
          "normal": blender_utils.process_normal,
          "object_coordinates": blender_utils.process_object_coordinates,
          "segmentation": blender_utils.process_segementation,
          "rgb": blender_utils.process_rgb,
          "rgba": blender_utils.process_rgba,
      }
  
      super().__init__(
          scene,
          scene_observers={
              "frame_start": [
                  AttributeSetter(self.blender_scene, "frame_start")
              ],
              "frame_end": [
                  AttributeSetter(self.blender_scene, "frame_end")
              ],
              "frame_rate": [
                  AttributeSetter(self.blender_scene.render, "fps")
              ],
              "resolution": [
                  AttributeSetter(
                      self.blender_scene.render,
                      "resolution_x",
                      converter=lambda x: x[0],
                  ),
                  AttributeSetter(
                      self.blender_scene.render,
                      "resolution_y",
                      converter=lambda x: x[1],
                  ),
              ],
              "camera": [
                  AttributeSetter(
                      self.blender_scene,
                      "camera",
                      converter=self._convert_to_blender_object,
                  )
              ],
              "ambient_illumination": [
                  lambda change: self._set_ambient_light_color(
                      change.new
                  )
              ],
              "background": [
                  lambda change: self._set_background_color(change.new)
              ],
          },
      )
  
imports: import collections
  import io
  import logging
  import os
  import sys
  from contextlib import redirect_stdout
  from typing import Any, Dict, Optional, Sequence, Union
  import tempfile
  from kubric.safeimport.bpy import bpy
  import numpy
  import tensorflow
  from singledispatchmethod import singledispatchmethod
  import kubric
  from kubric import core
  from kubric.core.assets import UndefinedAsset
  from kubric.redirect_io import RedirectStream
  from kubric.renderer import blender_utils
  from kubric import file_io
  from kubric.file_io import PathLike
label_desc: Use image node_id 1 to create a new Blender object from inherited base classes: core with example: obj = Blender(scene, scratch_dir, adaptive_sampling, use_denoising, samples_per_pixel, background_transparency, verbose, custom_scene, motion_blur)