[Bf-blender-cvs] [d49e7b82da6] master: Compositor: Redesign Cryptomatte node for better usability

Jeroen Bakker noreply at git.blender.org
Tue Mar 16 07:44:25 CET 2021


Commit: d49e7b82da67885aac5933e094ee217ff777ac03
Author: Jeroen Bakker
Date:   Tue Mar 16 07:37:30 2021 +0100
Branches: master
https://developer.blender.org/rBd49e7b82da67885aac5933e094ee217ff777ac03

Compositor: Redesign Cryptomatte node for better usability

In the current implementation, cryptomatte passes are connected to the node
and elements are picked by using the eyedropper tool on a special pick channel.

This design has two disadvantages - both connecting all passes individually
and always having to switch to the picker channel are tedious.

With the new design, the user selects the RenderLayer or Image from which the
Cryptomatte layers are directly loaded (the type of pass is determined by an
enum). This allows the node to automatically detect all relevant passes.

Then, when using the eyedropper tool, the operator looks up the selected
coordinates from the picked Image, Node backdrop or Clip and reads the picked
object directly from the Renderlayer/Image, therefore allowing to pick in any
context (e.g. by clicking on the Combined pass in the Image Viewer). The
sampled color is looked up in the metadata and the actual name is stored
in the cryptomatte node. This also allows to remove a hash by just removing
the name from the matte id.

Technically there is some loss of flexibility because the Cryptomatte pass
inputs can no longer be connected to other nodes, but since any compositing
done on them is likely to break the Cryptomatte system anyways, this isn't
really a concern in practise.

In the future, this would also allow to automatically translate values to names
by looking up the value in the associated metadata of the input, or to get a
better visualization of overlapping areas in the Pick output since we could
blend colors now that the output doesn't have to contain the exact value.

Idea + Original patch: Lucas Stockner
Reviewed By: Brecht van Lommel

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

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

M	release/scripts/startup/nodeitems_builtins.py
M	source/blender/blenkernel/BKE_cryptomatte.h
M	source/blender/blenkernel/BKE_cryptomatte.hh
M	source/blender/blenkernel/BKE_node.h
M	source/blender/blenkernel/intern/cryptomatte.cc
M	source/blender/blenkernel/intern/node.cc
M	source/blender/blenkernel/intern/scene.c
M	source/blender/blenloader/intern/versioning_290.c
M	source/blender/compositor/intern/COM_Converter.cc
M	source/blender/compositor/nodes/COM_CryptomatteNode.cc
M	source/blender/compositor/nodes/COM_CryptomatteNode.h
M	source/blender/editors/include/ED_clip.h
M	source/blender/editors/include/ED_image.h
M	source/blender/editors/include/ED_node.h
M	source/blender/editors/include/UI_interface.h
M	source/blender/editors/interface/CMakeLists.txt
M	source/blender/editors/interface/interface_eyedropper_color.c
M	source/blender/editors/interface/interface_templates.c
M	source/blender/editors/space_clip/clip_editor.c
M	source/blender/editors/space_image/image_ops.c
M	source/blender/editors/space_node/drawnode.c
M	source/blender/editors/space_node/node_edit.c
M	source/blender/editors/space_node/node_view.c
M	source/blender/makesdna/DNA_node_types.h
M	source/blender/makesrna/RNA_access.h
M	source/blender/makesrna/intern/rna_nodetree.c
M	source/blender/nodes/NOD_composite.h
M	source/blender/nodes/NOD_static_types.h
M	source/blender/nodes/composite/nodes/node_composite_cryptomatte.cc

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

diff --git a/release/scripts/startup/nodeitems_builtins.py b/release/scripts/startup/nodeitems_builtins.py
index e6e2c25e383..fefb266e591 100644
--- a/release/scripts/startup/nodeitems_builtins.py
+++ b/release/scripts/startup/nodeitems_builtins.py
@@ -390,6 +390,7 @@ compositor_node_categories = [
         NodeItem("CompositorNodeColorMatte"),
         NodeItem("CompositorNodeDoubleEdgeMask"),
         NodeItem("CompositorNodeCryptomatte"),
+        NodeItem("CompositorNodeCryptomatteV2"),
     ]),
     CompositorNodeCategory("CMP_DISTORT", "Distort", items=[
         NodeItem("CompositorNodeScale"),
diff --git a/source/blender/blenkernel/BKE_cryptomatte.h b/source/blender/blenkernel/BKE_cryptomatte.h
index 7d295929e77..576a2c1effd 100644
--- a/source/blender/blenkernel/BKE_cryptomatte.h
+++ b/source/blender/blenkernel/BKE_cryptomatte.h
@@ -30,14 +30,17 @@
 extern "C" {
 #endif
 
+/* Forward declarations. */
 struct CryptomatteSession;
 struct Material;
 struct Object;
 struct RenderResult;
+struct Scene;
 
 struct CryptomatteSession *BKE_cryptomatte_init(void);
 struct CryptomatteSession *BKE_cryptomatte_init_from_render_result(
     const struct RenderResult *render_result);
+struct CryptomatteSession *BKE_cryptomatte_init_from_scene(const struct Scene *scene);
 void BKE_cryptomatte_free(struct CryptomatteSession *session);
 void BKE_cryptomatte_add_layer(struct CryptomatteSession *session, const char *layer_name);
 
@@ -52,6 +55,10 @@ uint32_t BKE_cryptomatte_asset_hash(struct CryptomatteSession *session,
                                     const char *layer_name,
                                     const struct Object *object);
 float BKE_cryptomatte_hash_to_float(uint32_t cryptomatte_hash);
+bool BKE_cryptomatte_find_name(const struct CryptomatteSession *session,
+                               const float encoded_hash,
+                               char *r_name,
+                               int name_len);
 
 char *BKE_cryptomatte_entries_to_matte_id(struct NodeCryptomatte *node_storage);
 void BKE_cryptomatte_matte_id_to_entries(struct NodeCryptomatte *node_storage,
@@ -63,4 +70,4 @@ void BKE_cryptomatte_store_metadata(const struct CryptomatteSession *session,
 
 #ifdef __cplusplus
 }
-#endif
+#endif
\ No newline at end of file
diff --git a/source/blender/blenkernel/BKE_cryptomatte.hh b/source/blender/blenkernel/BKE_cryptomatte.hh
index 98fdfc965bc..9e205d01765 100644
--- a/source/blender/blenkernel/BKE_cryptomatte.hh
+++ b/source/blender/blenkernel/BKE_cryptomatte.hh
@@ -26,6 +26,8 @@
 #include <optional>
 #include <string>
 
+#include "BKE_cryptomatte.h"
+
 #include "BLI_map.hh"
 #include "BLI_string_ref.hh"
 
@@ -105,6 +107,9 @@ struct CryptomatteStampDataCallbackData {
   static void extract_layer_manifest(void *_data, const char *propname, char *propvalue, int len);
 };
 
+const blender::Vector<std::string> &BKE_cryptomatte_layer_names_get(
+    const CryptomatteSession &session);
+
 struct CryptomatteSessionDeleter {
   void operator()(CryptomatteSession *session)
   {
diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h
index 0379dea9e8c..42022cce5a5 100644
--- a/source/blender/blenkernel/BKE_node.h
+++ b/source/blender/blenkernel/BKE_node.h
@@ -47,6 +47,7 @@ struct BlendLibReader;
 struct BlendWriter;
 struct ColorManagedDisplaySettings;
 struct ColorManagedViewSettings;
+struct CryptomatteSession;
 struct FreestyleLineStyle;
 struct GPUMaterial;
 struct GPUNodeStack;
@@ -1204,9 +1205,10 @@ void ntreeGPUMaterialNodes(struct bNodeTree *localtree,
 #define CMP_NODE_PLANETRACKDEFORM 320
 #define CMP_NODE_CORNERPIN 321
 #define CMP_NODE_SWITCH_VIEW 322
-#define CMP_NODE_CRYPTOMATTE 323
+#define CMP_NODE_CRYPTOMATTE_LEGACY 323
 #define CMP_NODE_DENOISE 324
 #define CMP_NODE_EXPOSURE 325
+#define CMP_NODE_CRYPTOMATTE 326
 
 /* channel toggles */
 #define CMP_CHAN_RGB 1
@@ -1236,6 +1238,10 @@ void ntreeGPUMaterialNodes(struct bNodeTree *localtree,
 #define CMP_TRACKPOS_RELATIVE_FRAME 2
 #define CMP_TRACKPOS_ABSOLUTE_FRAME 3
 
+/* Cryptomatte source. */
+#define CMP_CRYPTOMATTE_SRC_RENDER 0
+#define CMP_CRYPTOMATTE_SRC_IMAGE 1
+
 /* API */
 void ntreeCompositExecTree(struct Scene *scene,
                            struct bNodeTree *ntree,
@@ -1278,11 +1284,15 @@ void ntreeCompositOutputFileUniqueLayer(struct ListBase *list,
 void ntreeCompositColorBalanceSyncFromLGG(bNodeTree *ntree, bNode *node);
 void ntreeCompositColorBalanceSyncFromCDL(bNodeTree *ntree, bNode *node);
 
-void ntreeCompositCryptomatteSyncFromAdd(bNodeTree *ntree, bNode *node);
-void ntreeCompositCryptomatteSyncFromRemove(bNodeTree *ntree, bNode *node);
-struct bNodeSocket *ntreeCompositCryptomatteAddSocket(struct bNodeTree *ntree, struct bNode *node);
-int ntreeCompositCryptomatteRemoveSocket(struct bNodeTree *ntree, struct bNode *node);
+void ntreeCompositCryptomatteSyncFromAdd(bNode *node);
+void ntreeCompositCryptomatteSyncFromRemove(bNode *node);
+bNodeSocket *ntreeCompositCryptomatteAddSocket(bNodeTree *ntree, bNode *node);
+int ntreeCompositCryptomatteRemoveSocket(bNodeTree *ntree, bNode *node);
+void ntreeCompositCryptomatteLayerPrefix(const bNode *node, char *r_prefix, size_t prefix_len);
 
+/* Update the runtime layer names with the cryptomatte layer names of the references
+ * render layer or image. */
+void ntreeCompositCryptomatteUpdateLayerNames(bNode *node);
 /** \} */
 
 /* -------------------------------------------------------------------- */
diff --git a/source/blender/blenkernel/intern/cryptomatte.cc b/source/blender/blenkernel/intern/cryptomatte.cc
index 55426f738ff..74576b8bbbb 100644
--- a/source/blender/blenkernel/intern/cryptomatte.cc
+++ b/source/blender/blenkernel/intern/cryptomatte.cc
@@ -30,6 +30,7 @@
 #include "DNA_material_types.h"
 #include "DNA_node_types.h"
 #include "DNA_object_types.h"
+#include "DNA_scene_types.h"
 
 #include "BLI_compiler_attrs.h"
 #include "BLI_dynstr.h"
@@ -50,10 +51,13 @@
 
 struct CryptomatteSession {
   blender::Map<std::string, blender::bke::cryptomatte::CryptomatteLayer> layers;
+  /* Layer names in order of creation. */
+  blender::Vector<std::string> layer_names;
 
   CryptomatteSession();
   CryptomatteSession(const Main *bmain);
   CryptomatteSession(StampData *stamp_data);
+  CryptomatteSession(const Scene *scene);
 
   blender::bke::cryptomatte::CryptomatteLayer &add_layer(std::string layer_name);
   std::optional<std::string> operator[](float encoded_hash) const;
@@ -99,8 +103,32 @@ CryptomatteSession::CryptomatteSession(StampData *stamp_data)
       false);
 }
 
+CryptomatteSession::CryptomatteSession(const Scene *scene)
+{
+  LISTBASE_FOREACH (ViewLayer *, view_layer, &scene->view_layers) {
+    eViewLayerCryptomatteFlags cryptoflags = static_cast<eViewLayerCryptomatteFlags>(
+        view_layer->cryptomatte_flag & VIEW_LAYER_CRYPTOMATTE_ALL);
+    if (cryptoflags == 0) {
+      cryptoflags = static_cast<eViewLayerCryptomatteFlags>(VIEW_LAYER_CRYPTOMATTE_ALL);
+    }
+
+    if (cryptoflags & VIEW_LAYER_CRYPTOMATTE_OBJECT) {
+      add_layer(blender::StringRefNull(view_layer->name) + ".CryptoObject");
+    }
+    if (cryptoflags & VIEW_LAYER_CRYPTOMATTE_ASSET) {
+      add_layer(blender::StringRefNull(view_layer->name) + ".CryptoAsset");
+    }
+    if (cryptoflags & VIEW_LAYER_CRYPTOMATTE_MATERIAL) {
+      add_layer(blender::StringRefNull(view_layer->name) + ".CryptoMaterial");
+    }
+  }
+}
+
 blender::bke::cryptomatte::CryptomatteLayer &CryptomatteSession::add_layer(std::string layer_name)
 {
+  if (!layer_names.contains(layer_name)) {
+    layer_names.append(layer_name);
+  }
   return layers.lookup_or_add_default(layer_name);
 }
 
@@ -128,6 +156,12 @@ struct CryptomatteSession *BKE_cryptomatte_init_from_render_result(
   return session;
 }
 
+struct CryptomatteSession *BKE_cryptomatte_init_from_scene(const struct Scene *scene)
+{
+  CryptomatteSession *session = new CryptomatteSession(scene);
+  return session;
+}
+
 void BKE_cryptomatte_add_layer(struct CryptomatteSession *session, const char *layer_name)
 {
   session->add_layer(layer_name);
@@ -182,6 +216,21 @@ float BKE_cryptomatte_hash_to_float(uint32_t cryptomatte_hash)
   return blender::bke::cryptomatte::CryptomatteHash(cryptomatte_hash).float_encoded();
 }
 
+/* Find an ID in the given main that matches the given encoded float. */
+bool BKE_cryptomatte_find_name(const CryptomatteSession *session,
+                               const float encoded_hash,
+                               char *r_name,
+                               int name_len)
+{
+  std::optional<std::string> name = (*session)[encoded_hash];
+  if (!name) {
+    return false;
+  }
+
+  BLI_strncpy(r_name, name->c_str(), name_len);
+  return true;
+}
+
 char *BKE_cryptomatte_entries_to_matte_id(NodeCryptomatte *node_storage)
 {
   DynStr *matte_id = BLI_dynstr_new();
@@ -213,7 +262,8 @@ void BKE_cryptomatte_matte_id_to_entries(NodeCryptomatte *node_storage, const ch
   }
   /* Update the matte_id so the files can be opened in versions that don't
    * use `CryptomatteEntry`. */
-  if (matte_id != node_storage->matte_id && STREQ(node_storage->matte_id, matte_id)) {
+  if (matte_id != node_storage->matte_id && node_storage->matte_id &&
+      STREQ(node_storage->matte_id, matte_id)) {
     MEM_SAFE_FREE(node_storage->matte_id);
     node_storage->matte_id = static_cast<char *>(MEM_dupallocN(matte_id));
   }
@@ -599,4 +649,10 @@ void CryptomatteStampDataCallbackData::extract_layer_manifest(void *_data,
   blender::bke::cryptomatte::manifest::from_manifest(layer, propvalue);
 }
 
+const blender::Vector<std::string> &BKE_cryptomatte_layer_names_get(
+    const CryptomatteSession &session)
+{
+  return session.layer_names;
+}
+
 }  // namespace blender::bke::cryptomatte
diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc
index b6f02128353..66c0e724fdf 100644
--- a/source/blender/blenkernel/intern/node.cc
+++ b/source/blender/blenkernel/intern/node.cc
@@ -538,7 +538,8 @@ void ntreeBlendWrite(BlendWriter *writer, bNodeTree *ntree)
         }
         BLO_write_struct_by_name(writer, node->typeinfo->storagename, node->storage);
       }
-      else if ((ntree->type == NTREE_COMPOSIT) && (nod

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list