def _write_distance_to_file(self): """ Configures the renderer, s.t. the z-values computed for the next rendering are directly written to file. """ # z-buffer/mist settings distance_start = self.config.get_float("distance_start", 0.1) distance_range = self.config.get_float("distance_range", 25.0) GlobalStorage.add("renderer_distance_end", distance_start + distance_range) 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') # use either mist rendering or the z-buffer # mists uses an interpolation during the sample per pixel # while the z buffer only returns the closest object per pixel use_mist_as_distance = self.config.get_bool("use_mist_distance", True) if use_mist_as_distance: bpy.context.scene.world.mist_settings.start = distance_start bpy.context.scene.world.mist_settings.depth = distance_range bpy.context.scene.world.mist_settings.falloff = self.config.get_string( "distance_falloff", "LINEAR") bpy.context.view_layer.use_pass_mist = True # Enable distance pass # Create a mapper node to map from 0-1 to SI units mapper_node = tree.nodes.new("CompositorNodeMapRange") links.new(render_layer_node.outputs["Mist"], mapper_node.inputs['Value']) # map the values 0-1 to range distance_start to distance_range mapper_node.inputs['To Min'].default_value = distance_start mapper_node.inputs[ 'To Max'].default_value = distance_start + distance_range final_output = mapper_node.outputs['Value'] else: bpy.context.view_layer.use_pass_z = True # add min and max nodes to perform the clipping to the desired range min_node = tree.nodes.new("CompositorNodeMath") min_node.operation = "MINIMUM" min_node.inputs[1].default_value = distance_start + distance_range links.new(render_layer_node.outputs["Depth"], min_node.inputs[0]) max_node = tree.nodes.new("CompositorNodeMath") max_node.operation = "MAXIMUM" max_node.inputs[1].default_value = distance_start links.new(min_node.outputs["Value"], max_node.inputs[0]) final_output = max_node.outputs["Value"] output_file = tree.nodes.new("CompositorNodeOutputFile") output_file.base_path = self._determine_output_dir() output_file.format.file_format = "OPEN_EXR" output_file.file_slots.values()[0].path = self.config.get_string( "distance_output_file_prefix", "distance_") # Feed the Z-Buffer or Mist output of the render layer to the input of the file IO layer links.new(final_output, output_file.inputs['Image'])
def enable_distance_output(output_dir: Union[str, None] = None, file_prefix: str = "distance_", output_key: str = "distance", distance_start: float = 0.1, distance_range: float = 25.0, distance_falloff: str = "LINEAR"): """ Enables writing distance images. Distance images will be written in the form of .exr files during the next rendering. :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 distance output. :param distance_start: Starting distance of the distance, measured from the camera. :param distance_range: Total distance in which the distance is measured. \ distance_end = distance_start + distance_range. :param distance_falloff: Type of transition used to fade distance. Available: [LINEAR, QUADRATIC, INVERSE_QUADRATIC] """ if output_dir is None: output_dir = Utility.get_temporary_directory() bpy.context.scene.render.use_compositing = True bpy.context.scene.use_nodes = True GlobalStorage.add("renderer_distance_end", distance_start + distance_range) 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') # Set mist pass limits bpy.context.scene.world.mist_settings.start = distance_start bpy.context.scene.world.mist_settings.depth = distance_range bpy.context.scene.world.mist_settings.falloff = distance_falloff bpy.context.view_layer.use_pass_mist = True # Enable distance pass # Create a mapper node to map from 0-1 to SI units mapper_node = tree.nodes.new("CompositorNodeMapRange") links.new(render_layer_node.outputs["Mist"], mapper_node.inputs['Value']) # map the values 0-1 to range distance_start to distance_range mapper_node.inputs['To Min'].default_value = distance_start mapper_node.inputs['To Max'].default_value = distance_start + distance_range final_output = mapper_node.outputs['Value'] # 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 or Mist output of the render layer to the input of the file IO layer links.new(final_output, 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 })
def _write_depth_to_file(self): """ Configures the renderer, s.t. the z-values computed for the next rendering are directly written to file. """ # Mist settings depth_start = self.config.get_float("depth_start", 0.1) depth_range = self.config.get_float("depth_range", 25.0) GlobalStorage.add("renderer_depth_end", depth_start + depth_range) bpy.context.scene.world.mist_settings.start = depth_start bpy.context.scene.world.mist_settings.depth = depth_range bpy.context.scene.world.mist_settings.falloff = self.config.get_string( "depth_falloff", "LINEAR") bpy.context.scene.render.use_compositing = True bpy.context.scene.use_nodes = True bpy.context.view_layer.use_pass_mist = True # Enable depth pass tree = bpy.context.scene.node_tree links = tree.links # Use existing render layer render_layer_node = tree.nodes.get('Render Layers') # Create a mapper node to map from 0-1 to SI units mapper_node = tree.nodes.new("CompositorNodeMapRange") links.new(render_layer_node.outputs["Mist"], mapper_node.inputs['Value']) # map the values 0-1 to range depth_start to depth_range mapper_node.inputs['To Min'].default_value = depth_start mapper_node.inputs['To Max'].default_value = depth_start + depth_range output_file = tree.nodes.new("CompositorNodeOutputFile") output_file.base_path = self._determine_output_dir() output_file.format.file_format = "OPEN_EXR" output_file.file_slots.values()[0].path = self.config.get_string( "depth_output_file_prefix", "depth_") # Feed the Mist output of the render layer to the input of the file IO layer links.new(mapper_node.outputs['Value'], output_file.inputs['Image'])
def enable_distance_output(output_dir, file_prefix="distance_", output_key="distance", use_mist_as_distance=True, distance_start=0.1, distance_range=25.0, distance_falloff="LINEAR"): """ Enables writing distance images. Distance images will be written in the form of .exr files during the next rendering. :param output_dir: The directory to write files to. :param file_prefix: The prefix to use for writing the files. :param output_key: The key to use for registering the distance output. :param use_mist_as_distance: If true, the distance is sampled over several iterations, useful for motion blur or soft edges, if this is turned off, only one sample is taken to determine the depth. Default: True. :param distance_start: Starting distance of the distance, measured from the camera. :param distance_range: Total distance in which the distance is measured. distance_end = distance_start + distance_range. :param distance_falloff: Type of transition used to fade distance. Available: [LINEAR, QUADRATIC, INVERSE_QUADRATIC] """ bpy.context.scene.render.use_compositing = True bpy.context.scene.use_nodes = True GlobalStorage.add("renderer_distance_end", distance_start + distance_range) 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') # use either mist rendering or the z-buffer # mists uses an interpolation during the sample per pixel # while the z buffer only returns the closest object per pixel if use_mist_as_distance: bpy.context.scene.world.mist_settings.start = distance_start bpy.context.scene.world.mist_settings.depth = distance_range bpy.context.scene.world.mist_settings.falloff = distance_falloff bpy.context.view_layer.use_pass_mist = True # Enable distance pass # Create a mapper node to map from 0-1 to SI units mapper_node = tree.nodes.new("CompositorNodeMapRange") links.new(render_layer_node.outputs["Mist"], mapper_node.inputs['Value']) # map the values 0-1 to range distance_start to distance_range mapper_node.inputs['To Min'].default_value = distance_start mapper_node.inputs[ 'To Max'].default_value = distance_start + distance_range final_output = mapper_node.outputs['Value'] else: bpy.context.view_layer.use_pass_z = True # add min and max nodes to perform the clipping to the desired range min_node = tree.nodes.new("CompositorNodeMath") min_node.operation = "MINIMUM" min_node.inputs[1].default_value = distance_start + distance_range links.new(render_layer_node.outputs["Depth"], min_node.inputs[0]) max_node = tree.nodes.new("CompositorNodeMath") max_node.operation = "MAXIMUM" max_node.inputs[1].default_value = distance_start links.new(min_node.outputs["Value"], max_node.inputs[0]) final_output = max_node.outputs["Value"] 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 or Mist output of the render layer to the input of the file IO layer links.new(final_output, 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" })