[Bf-blender-cvs] [412295cc89b] compositor-cryptomatte-workflow: Cleanup: Split converToOperations in multiple methods.

Jeroen Bakker noreply at git.blender.org
Tue Feb 16 09:47:21 CET 2021


Commit: 412295cc89be96e5559f0b34860c9af850e11cc2
Author: Jeroen Bakker
Date:   Mon Feb 15 12:09:48 2021 +0100
Branches: compositor-cryptomatte-workflow
https://developer.blender.org/rB412295cc89be96e5559f0b34860c9af850e11cc2

Cleanup: Split converToOperations in multiple methods.

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

M	source/blender/compositor/nodes/COM_CryptomatteNode.cpp
M	source/blender/compositor/nodes/COM_CryptomatteNode.h

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

diff --git a/source/blender/compositor/nodes/COM_CryptomatteNode.cpp b/source/blender/compositor/nodes/COM_CryptomatteNode.cpp
index 3352bde22bc..4b6c6b2c266 100644
--- a/source/blender/compositor/nodes/COM_CryptomatteNode.cpp
+++ b/source/blender/compositor/nodes/COM_CryptomatteNode.cpp
@@ -29,128 +29,162 @@
 #include "COM_SetAlphaMultiplyOperation.h"
 #include "COM_SetColorOperation.h"
 #include <iterator>
+#include <string>
+
+constexpr blender::StringRef CRYPTOMATTE_LAYER_PREFIX_OBJECT("CryptoObject");
+constexpr blender::StringRef CRYPTOMATTE_LAYER_PREFIX_MATERIAL("CryptoMaterial");
+constexpr blender::StringRef CRYPTOMATTE_LAYER_PREFIX_ASSET("CryptoAsset");
 
 CryptomatteNode::CryptomatteNode(bNode *editorNode) : Node(editorNode)
 {
   /* pass */
 }
 
-void CryptomatteNode::convertToOperations(NodeConverter &converter,
-                                          const CompositorContext &context) const
+blender::StringRef CryptomatteNode::getCryptomatteLayerPrefix(const bNode &node) const
 {
-  NodeInput *inputSocketImage = this->getInputSocket(0);
-  NodeOutput *outputSocketImage = this->getOutputSocket(0);
-  NodeOutput *outputSocketMatte = this->getOutputSocket(1);
-  NodeOutput *outputSocketPick = this->getOutputSocket(2);
-
-  bNode *node = this->getbNode();
-  NodeCryptomatte *cryptoMatteSettings = (NodeCryptomatte *)node->storage;
+  NodeCryptomatte *cryptoMatteSettings = (NodeCryptomatte *)node.storage;
 
-  const char *prefix = "";
   switch (cryptoMatteSettings->type) {
     case CMP_CRYPTOMATTE_TYPE_OBJECT:
-      prefix = "CryptoObject";
-      break;
+      return CRYPTOMATTE_LAYER_PREFIX_OBJECT;
+
     case CMP_CRYPTOMATTE_TYPE_MATERIAL:
-      prefix = "CryptoMaterial";
-      break;
+      return CRYPTOMATTE_LAYER_PREFIX_MATERIAL;
+
     case CMP_CRYPTOMATTE_TYPE_ASSET:
-      prefix = "CryptoAsset";
-      break;
+      return CRYPTOMATTE_LAYER_PREFIX_ASSET;
   }
-  BLI_assert(prefix[0] != '\0');
-
-  vector<NodeOperation *> input_operations;
-  if (node->custom1 == CMP_CRYPTOMATTE_SRC_RENDER) {
-    Scene *scene = (Scene *)node->id;
-    BLI_assert(GS(scene->id.name) == ID_SCE);
-    Render *re = (scene) ? RE_GetSceneRender(scene) : NULL;
-
-    if (re) {
-      const short layerId = node->custom2;
-      RenderResult *rr = RE_AcquireResultRead(re);
-      if (rr) {
-        ViewLayer *view_layer = (ViewLayer *)BLI_findlink(&scene->view_layers, layerId);
-        if (view_layer) {
-          RenderLayer *rl = RE_GetRenderLayer(rr, view_layer->name);
-          if (rl) {
-            LISTBASE_FOREACH (RenderPass *, rpass, &rl->passes) {
-              if (STRPREFIX(rpass->name, prefix)) {
-                RenderLayersProg *op = new RenderLayersProg(
-                    rpass->name, COM_DT_COLOR, rpass->channels);
-                op->setScene(scene);
-                op->setLayerId(layerId);
-                op->setRenderData(context.getRenderData());
-                op->setViewName(context.getViewName());
-                input_operations.push_back(op);
-              }
+  BLI_assert(false && "Invalid Cryptomatte layer.");
+  return "";
+}
+
+void CryptomatteNode::buildInputOperationsFromRenderSource(
+    const CompositorContext &context,
+    const bNode &node,
+    blender::Vector<NodeOperation *> &r_input_operations) const
+{
+  Scene *scene = (Scene *)node.id;
+  BLI_assert(GS(scene->id.name) == ID_SCE);
+  Render *render = (scene) ? RE_GetSceneRender(scene) : NULL;
+
+  if (render) {
+    const short cryptomatte_layer_id = node.custom2;
+    RenderResult *rr = RE_AcquireResultRead(render);
+    if (rr) {
+      ViewLayer *view_layer = (ViewLayer *)BLI_findlink(&scene->view_layers, cryptomatte_layer_id);
+      if (view_layer) {
+        RenderLayer *render_layer = RE_GetRenderLayer(rr, view_layer->name);
+        if (render_layer) {
+          std::string prefix = getCryptomatteLayerPrefix(node);
+          LISTBASE_FOREACH (RenderPass *, rpass, &render_layer->passes) {
+            if (blender::StringRef(rpass->name, sizeof(rpass->name)).startswith(prefix)) {
+              RenderLayersProg *op = new RenderLayersProg(
+                  rpass->name, COM_DT_COLOR, rpass->channels);
+              op->setScene(scene);
+              op->setLayerId(cryptomatte_layer_id);
+              op->setRenderData(context.getRenderData());
+              op->setViewName(context.getViewName());
+              r_input_operations.append(op);
             }
           }
         }
       }
-      RE_ReleaseResult(re);
     }
+    RE_ReleaseResult(render);
   }
-  else if (node->custom1 == CMP_CRYPTOMATTE_SRC_IMAGE) {
-    Image *image = (Image *)node->id;
-    BLI_assert(!image || GS(image->id.name) == ID_IM);
-    ImageUser *iuser = &cryptoMatteSettings->iuser;
-    BKE_image_user_frame_calc(image, iuser, context.getFramenumber());
-
-    if (image && image->type == IMA_TYPE_MULTILAYER) {
-      ImBuf *ibuf = BKE_image_acquire_ibuf(image, iuser, NULL);
-      if (image->rr) {
-        int view = 0;
-        if (BLI_listbase_count_at_most(&image->rr->views, 2) > 1) {
-          if (iuser->view == 0) {
-            /* heuristic to match image name with scene names, check if the view name exists in the
-             * image */
-            view = BLI_findstringindex(
-                &image->rr->views, context.getViewName(), offsetof(RenderView, name));
-            if (view == -1)
-              view = 0;
-          }
-          else {
-            view = iuser->view - 1;
-          }
+}
+
+void CryptomatteNode::buildInputOperationsFromImageSource(
+    const CompositorContext &context,
+    const bNode &node,
+    blender::Vector<NodeOperation *> &r_input_operations) const
+{
+  NodeCryptomatte *cryptoMatteSettings = (NodeCryptomatte *)node.storage;
+  Image *image = (Image *)node.id;
+  BLI_assert(!image || GS(image->id.name) == ID_IM);
+  ImageUser *iuser = &cryptoMatteSettings->iuser;
+  BKE_image_user_frame_calc(image, iuser, context.getFramenumber());
+
+  if (image && image->type == IMA_TYPE_MULTILAYER) {
+    ImBuf *ibuf = BKE_image_acquire_ibuf(image, iuser, NULL);
+    if (image->rr) {
+      int view = 0;
+      if (BLI_listbase_count_at_most(&image->rr->views, 2) > 1) {
+        if (iuser->view == 0) {
+          /* heuristic to match image name with scene names, check if the view name exists in the
+           * image */
+          view = BLI_findstringindex(
+              &image->rr->views, context.getViewName(), offsetof(RenderView, name));
+          if (view == -1)
+            view = 0;
         }
+        else {
+          view = iuser->view - 1;
+        }
+      }
 
-        RenderLayer *rl = (RenderLayer *)BLI_findlink(&image->rr->layers, iuser->layer);
-        if (rl) {
-          int passindex = 0;
-          for (RenderPass *rpass = (RenderPass *)rl->passes.first; rpass;
-               rpass = rpass->next, passindex++) {
-            if (STRPREFIX(rpass->name, prefix)) {
-              MultilayerColorOperation *op = new MultilayerColorOperation(passindex, view);
-              op->setImage(image);
-              op->setRenderLayer(rl);
-              op->setImageUser(iuser);
-              op->setFramenumber(context.getFramenumber());
-              input_operations.push_back(op);
-            }
+      RenderLayer *render_layer = (RenderLayer *)BLI_findlink(&image->rr->layers, iuser->layer);
+      if (render_layer) {
+        int render_pass_index = 0;
+        std::string prefix = getCryptomatteLayerPrefix(node);
+        for (RenderPass *rpass = (RenderPass *)render_layer->passes.first; rpass;
+             rpass = rpass->next, render_pass_index++) {
+          if (blender::StringRef(rpass->name, sizeof(rpass->name)).startswith(prefix)) {
+            MultilayerColorOperation *op = new MultilayerColorOperation(render_pass_index, view);
+            op->setImage(image);
+            op->setRenderLayer(render_layer);
+            op->setImageUser(iuser);
+            op->setFramenumber(context.getFramenumber());
+            r_input_operations.append(op);
           }
         }
       }
-      BKE_image_release_ibuf(image, ibuf, NULL);
     }
+    BKE_image_release_ibuf(image, ibuf, NULL);
+  }
+}
+
+blender::Vector<NodeOperation *> CryptomatteNode::createInputOperations(
+    const CompositorContext &context, const bNode &node) const
+{
+  blender::Vector<NodeOperation *> input_operations;
+  switch (node.custom1) {
+    case CMP_CRYPTOMATTE_SRC_RENDER:
+      buildInputOperationsFromRenderSource(context, node, input_operations);
+      break;
+    case CMP_CRYPTOMATTE_SRC_IMAGE:
+      buildInputOperationsFromImageSource(context, node, input_operations);
+      break;
   }
 
-  if (input_operations.empty()) {
+  if (input_operations.is_empty()) {
     SetColorOperation *op = new SetColorOperation();
     op->setChannel1(0.0f);
     op->setChannel2(1.0f);
     op->setChannel3(0.0f);
     op->setChannel4(0.0f);
-    input_operations.push_back(op);
+    input_operations.append(op);
   }
+  return input_operations;
+}
 
+void CryptomatteNode::convertToOperations(NodeConverter &converter,
+                                          const CompositorContext &context) const
+{
+  NodeInput *inputSocketImage = this->getInputSocket(0);
+  NodeOutput *outputSocketImage = this->getOutputSocket(0);
+  NodeOutput *outputSocketMatte = this->getOutputSocket(1);
+  NodeOutput *outputSocketPick = this->getOutputSocket(2);
+
+  bNode *node = this->getbNode();
+  NodeCryptomatte *cryptoMatteSettings = (NodeCryptomatte *)node->storage;
+
+  blender::Vector<NodeOperation *> input_operations = createInputOperations(context, *node);
   CryptomatteOperation *operation = new CryptomatteOperation(input_operations.size());
   LISTBASE_FOREACH (CryptomatteEntry *, cryptomatte_entry, &cryptoMatteSettings->entries) {
     operation->addObjectIndex(cryptomatte_entry->encoded_hash);
   }
-
   converter.addOperation(operation);
-
   for (int i = 0; i < input_operations.size(); ++i) {
     converter.addOperation(input_operations[i]);
     converter.addLink(input_operations[i]->getOutputSocket(), operation->getInputSocket(i));
diff --git a/source/blender/compositor/nodes/COM_CryptomatteNode.h b/source/blender/compositor/nodes/COM_Cryptom

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list