diff options
Diffstat (limited to 'source/blender/blenkernel/intern/cryptomatte.cc')
-rw-r--r-- | source/blender/blenkernel/intern/cryptomatte.cc | 58 |
1 files changed, 57 insertions, 1 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 |