[Bf-blender-cvs] [957e292c586] master: Fix T64953: Add cryptomatte meta data to file output node.

Jeroen Bakker noreply at git.blender.org
Tue Jan 12 16:25:34 CET 2021


Commit: 957e292c5864cba3cf62c7c59dc644422660034f
Author: Jeroen Bakker
Date:   Tue Jan 12 16:19:54 2021 +0100
Branches: master
https://developer.blender.org/rB957e292c5864cba3cf62c7c59dc644422660034f

Fix T64953: Add cryptomatte meta data to file output node.

This change will try to add meta data when using a multilayered open
exr file output node in the compositor. It adds the current scene meta
data and converts existing cryptomatte keys so it follows the
naming that is configured in the file output node.

This change supports the basic use-case where the compositor is
used to output cryptomatte layers with a different naming scheme to
support external compositors. In this case the Multilayered OpenEXR
files are used and the meta data is read from the render result.

Meta data is found when render layer node is connected with the
file output node without any other nodes in between. Redirects and empty
node groups are allowed.

The patch has been verified to work with external compositors.
See https://devtalk.blender.org/t/making-sense-of-cryptomatte-usage-in-third-party-programs/16576/17

See patch for example files.

Reviewed By: Sergey Sharybin

Differential Revision: https://developer.blender.org/D10016

===================================================================

A	source/blender/blenkernel/BKE_cryptomatte.hh
M	source/blender/blenkernel/CMakeLists.txt
M	source/blender/blenkernel/intern/cryptomatte.cc
A	source/blender/blenkernel/intern/cryptomatte_test.cc
M	source/blender/compositor/CMakeLists.txt
A	source/blender/compositor/intern/COM_MetaData.cpp
A	source/blender/compositor/intern/COM_MetaData.h
M	source/blender/compositor/intern/COM_SocketReader.h
M	source/blender/compositor/nodes/COM_OutputFileNode.cpp
M	source/blender/compositor/operations/COM_OutputFileMultiViewOperation.cpp
M	source/blender/compositor/operations/COM_OutputFileMultiViewOperation.h
M	source/blender/compositor/operations/COM_OutputFileOperation.cpp
M	source/blender/compositor/operations/COM_OutputFileOperation.h
M	source/blender/compositor/operations/COM_RenderLayersProg.cpp
M	source/blender/compositor/operations/COM_RenderLayersProg.h
M	source/blender/compositor/operations/COM_SocketProxyOperation.cpp
M	source/blender/compositor/operations/COM_SocketProxyOperation.h

===================================================================

diff --git a/source/blender/blenkernel/BKE_cryptomatte.hh b/source/blender/blenkernel/BKE_cryptomatte.hh
new file mode 100644
index 00000000000..892f228187e
--- /dev/null
+++ b/source/blender/blenkernel/BKE_cryptomatte.hh
@@ -0,0 +1,59 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2020 Blender Foundation.
+ * All rights reserved.
+ */
+
+/** \file
+ * \ingroup bke
+ */
+
+#pragma once
+
+#include <string>
+
+#include "BLI_string_ref.hh"
+
+namespace blender {
+
+/* Format to a cryptomatte meta data key.
+ *
+ * Cryptomatte stores meta data. The keys are formatted containing a hash that
+ * is generated from its layer name.
+ *
+ * The output of this function is:
+ * 'cryptomatte/{hash of layer_name}/{key_name}'.
+ */
+std::string BKE_cryptomatte_meta_data_key(const StringRef layer_name,
+                                          const StringRefNull key_name);
+
+/* Extract the cryptomatte layer name from the given `render_pass_name`.
+ *
+ * Cryptomatte passes are formatted with a trailing number for storing multiple samples that belong
+ * to the same cryptomatte layer. This function would remove the trailing numbers to determine the
+ * cryptomatte layer name.
+ *
+ * # Example
+ *
+ * A render_pass_name could be 'View Layer.CryptoMaterial02'. The cryptomatte layer would be 'View
+ * Layer.CryptoMaterial'.
+ *
+ * NOTE: The return type is a substring of `render_pass_name` and therefore cannot outlive the
+ * `render_pass_name` internal data.
+ */
+StringRef BKE_cryptomatte_extract_layer_name(const StringRef render_pass_name);
+
+}  // namespace blender
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index 574d70bf456..5021736dbfe 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -731,6 +731,7 @@ add_dependencies(bf_blenkernel bf_dna)
 if(WITH_GTESTS)
   set(TEST_SRC
     intern/armature_test.cc
+    intern/cryptomatte_test.cc
     intern/fcurve_test.cc
     intern/lattice_deform_test.cc
     intern/layer_test.cc
diff --git a/source/blender/blenkernel/intern/cryptomatte.cc b/source/blender/blenkernel/intern/cryptomatte.cc
index 1dd71dacc46..7ddc1474e09 100644
--- a/source/blender/blenkernel/intern/cryptomatte.cc
+++ b/source/blender/blenkernel/intern/cryptomatte.cc
@@ -22,6 +22,7 @@
  */
 
 #include "BKE_cryptomatte.h"
+#include "BKE_cryptomatte.hh"
 #include "BKE_image.h"
 #include "BKE_main.h"
 
@@ -43,6 +44,7 @@
 #include <iomanip>
 #include <sstream>
 #include <string>
+#include <string_view>
 
 enum CryptomatteLayerState {
   EMPTY,
@@ -299,15 +301,15 @@ static uint32_t cryptomatte_determine_identifier(const std::string name)
   return BLI_hash_mm3(reinterpret_cast<const unsigned char *>(name.c_str()), name.length(), 0);
 }
 
-static std::string cryptomatte_determine_prefix(const std::string name)
+static void add_render_result_meta_data(RenderResult *render_result,
+                                        const blender::StringRef layer_name,
+                                        const blender::StringRefNull key_name,
+                                        const blender::StringRefNull value)
 {
-  std::stringstream stream;
-  const uint32_t render_pass_identifier = cryptomatte_determine_identifier(name);
-  stream << "cryptomatte/";
-  stream << std::setfill('0') << std::setw(sizeof(uint32_t) * 2) << std::hex
-         << render_pass_identifier;
-  stream << "/";
-  return stream.str();
+  BKE_render_result_stamp_data(
+      render_result,
+      blender::BKE_cryptomatte_meta_data_key(layer_name, key_name).c_str(),
+      value.data());
 }
 
 void BKE_cryptomatte_store_metadata(struct CryptomatteSession *session,
@@ -335,12 +337,47 @@ void BKE_cryptomatte_store_metadata(struct CryptomatteSession *session,
 
   const std::string manifest = layer->manifest_get_string();
   const std::string name = cryptomatte_determine_name(view_layer, cryptomatte_layer_name);
-  const std::string prefix = cryptomatte_determine_prefix(name);
 
   /* Store the meta data into the render result. */
-  BKE_render_result_stamp_data(render_result, (prefix + "name").c_str(), name.c_str());
-  BKE_render_result_stamp_data(render_result, (prefix + "hash").c_str(), "MurmurHash3_32");
-  BKE_render_result_stamp_data(
-      render_result, (prefix + "conversion").c_str(), "uint32_to_float32");
-  BKE_render_result_stamp_data(render_result, (prefix + "manifest").c_str(), manifest.c_str());
+  add_render_result_meta_data(render_result, name, "name", name);
+  add_render_result_meta_data(render_result, name, "hash", "MurmurHash3_32");
+  add_render_result_meta_data(render_result, name, "conversion", "uint32_to_float32");
+  add_render_result_meta_data(render_result, name, "manifest", manifest);
+}
+
+namespace blender {
+
+/* Return the hash of the given cryptomatte layer name.
+ *
+ * The cryptomatte specification limits the hash to 7 characters.
+ * The 7 position limitation solves issues when using cryptomatte together with OpenEXR.
+ * The specification suggests to use the first 7 chars of the hashed layer_name.
+ */
+static std::string cryptomatte_layer_name_hash(const StringRef layer_name)
+{
+  std::stringstream stream;
+  const uint32_t render_pass_identifier = cryptomatte_determine_identifier(layer_name);
+  stream << std::setfill('0') << std::setw(sizeof(uint32_t) * 2) << std::hex
+         << render_pass_identifier;
+  return stream.str().substr(0, 7);
+}
+
+std::string BKE_cryptomatte_meta_data_key(const StringRef layer_name, const StringRefNull key_name)
+{
+  return "cryptomatte/" + cryptomatte_layer_name_hash(layer_name) + "/" + key_name;
 }
+
+/* Extracts the cryptomatte name from a render pass name.
+ *
+ * Example: A render pass could be named `CryptoObject00`. This
+ *   function would remove the trailing digits and return `CryptoObject`. */
+StringRef BKE_cryptomatte_extract_layer_name(const StringRef render_pass_name)
+{
+  int64_t last_token = render_pass_name.size();
+  while (last_token > 0 && std::isdigit(render_pass_name[last_token - 1])) {
+    last_token -= 1;
+  }
+  return render_pass_name.substr(0, last_token);
+}
+
+}  // namespace blender
diff --git a/source/blender/blenkernel/intern/cryptomatte_test.cc b/source/blender/blenkernel/intern/cryptomatte_test.cc
new file mode 100644
index 00000000000..3773b2b3bf9
--- /dev/null
+++ b/source/blender/blenkernel/intern/cryptomatte_test.cc
@@ -0,0 +1,44 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2021 by Blender Foundation.
+ */
+#include "testing/testing.h"
+
+#include "BKE_cryptomatte.hh"
+
+namespace blender::bke::tests {
+
+TEST(cryptomatte, meta_data_key)
+{
+  ASSERT_EQ("cryptomatte/c7dbf5e/key",
+            BKE_cryptomatte_meta_data_key("ViewLayer.CryptoMaterial", "key"));
+  ASSERT_EQ("cryptomatte/b990b65/𝓴𝓮𝔂",
+            BKE_cryptomatte_meta_data_key("𝖚𝖓𝖎𝖈𝖔𝖉𝖊.CryptoMaterial", "𝓴𝓮𝔂"));
+}
+
+TEST(cryptomatte, extract_layer_name)
+{
+  ASSERT_EQ("ViewLayer.CryptoMaterial",
+            BKE_cryptomatte_extract_layer_name("ViewLayer.CryptoMaterial00"));
+  ASSERT_EQ("𝖚𝖓𝖎𝖈𝖔𝖉𝖊", BKE_cryptomatte_extract_layer_name("𝖚𝖓𝖎𝖈𝖔𝖉𝖊13"));
+  ASSERT_EQ("NoTrailingSampleNumber",
+            BKE_cryptomatte_extract_layer_name("NoTrailingSampleNumber"));
+  ASSERT_EQ("W1thM1dd13Numb3rs", BKE_cryptomatte_extract_layer_name("W1thM1dd13Numb3rs09"));
+  ASSERT_EQ("", BKE_cryptomatte_extract_layer_name("0123"));
+  ASSERT_EQ("", BKE_cryptomatte_extract_layer_name(""));
+}
+
+}  // namespace blender::bke::tests
diff --git a/source/blender/compositor/CMakeLists.txt b/source/blender/compositor/CMakeLists.txt
index 19eeb90c822..a226b009ec9 100644
--- a/source/blender/compositor/CMakeLists.txt
+++ b/source/blender/compositor/CMakeLists.txt
@@ -71,6 +71,8 @@ set(SRC
   intern/COM_MemoryBuffer.h
   intern/COM_MemoryProxy.cpp
   intern/COM_MemoryProxy.h
+  intern/COM_MetaData.cpp
+  intern/COM_MetaData.h
   intern/COM_Node.cpp
   intern/COM_Node.h
   intern/COM_NodeConverter.cpp
diff --git a/source/blender/compositor/intern/COM_MetaData.cpp b/source/blender/compositor/intern/COM_MetaData.cpp
new file mode 100644
index 00000000000..2b75947ff89
--- /dev/null
+++ b/source/blender/compositor/intern/COM_MetaData.cpp
@@ -0,0 +1,71 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GN

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list