diff options
author | Jeroen Bakker <jeroen@blender.org> | 2021-03-16 09:37:30 +0300 |
---|---|---|
committer | Jeroen Bakker <jeroen@blender.org> | 2021-03-16 09:43:17 +0300 |
commit | d49e7b82da67885aac5933e094ee217ff777ac03 (patch) | |
tree | 6ef994837bc165e76bd17aacb1f5296033f31c09 /source/blender/blenkernel/intern | |
parent | 269536d47ec5684a1c6018353120559be0ba2961 (diff) |
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
Diffstat (limited to 'source/blender/blenkernel/intern')
-rw-r--r-- | source/blender/blenkernel/intern/cryptomatte.cc | 58 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/node.cc | 9 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/scene.c | 5 |
3 files changed, 67 insertions, 5 deletions
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) && (node->type == CMP_NODE_CRYPTOMATTE)) { + else if ((ntree->type == NTREE_COMPOSIT) && + (ELEM(node->type, CMP_NODE_CRYPTOMATTE, CMP_NODE_CRYPTOMATTE_LEGACY))) { NodeCryptomatte *nc = (NodeCryptomatte *)node->storage; BLO_write_string(writer, nc->matte_id); LISTBASE_FOREACH (CryptomatteEntry *, entry, &nc->entries) { @@ -703,10 +704,12 @@ void ntreeBlendReadData(BlendDataReader *reader, bNodeTree *ntree) iuser->scene = nullptr; break; } + case CMP_NODE_CRYPTOMATTE_LEGACY: case CMP_NODE_CRYPTOMATTE: { NodeCryptomatte *nc = (NodeCryptomatte *)node->storage; BLO_read_data_address(reader, &nc->matte_id); BLO_read_list(reader, &nc->entries); + BLI_listbase_clear(&nc->runtime.layers); break; } case TEX_NODE_IMAGE: { @@ -903,7 +906,8 @@ void ntreeBlendReadExpand(BlendExpander *expander, bNodeTree *ntree) } LISTBASE_FOREACH (bNode *, node, &ntree->nodes) { - if (node->id && node->type != CMP_NODE_R_LAYERS) { + if (node->id && !(node->type == CMP_NODE_R_LAYERS) && + !(node->type == CMP_NODE_CRYPTOMATTE && node->custom1 == CMP_CRYPTOMATTE_SRC_RENDER)) { BLO_expand(expander, node->id); } @@ -4607,6 +4611,7 @@ static void registerCompositNodes() register_node_type_cmp_keyingscreen(); register_node_type_cmp_keying(); register_node_type_cmp_cryptomatte(); + register_node_type_cmp_cryptomatte_legacy(); register_node_type_cmp_translate(); register_node_type_cmp_rotate(); diff --git a/source/blender/blenkernel/intern/scene.c b/source/blender/blenkernel/intern/scene.c index 3633dff8690..de3e1023b08 100644 --- a/source/blender/blenkernel/intern/scene.c +++ b/source/blender/blenkernel/intern/scene.c @@ -1377,9 +1377,10 @@ static void scene_blend_read_data(BlendDataReader *reader, ID *id) /* patch for missing scene IDs, can't be in do-versions */ static void composite_patch(bNodeTree *ntree, Scene *scene) { - LISTBASE_FOREACH (bNode *, node, &ntree->nodes) { - if (node->id == NULL && node->type == CMP_NODE_R_LAYERS) { + if (node->id == NULL && + ((node->type == CMP_NODE_R_LAYERS) || + (node->type == CMP_NODE_CRYPTOMATTE && node->custom1 == CMP_CRYPTOMATTE_SRC_RENDER))) { node->id = &scene->id; } } |