Skip to content

Commit

Permalink
Remove deprecated API calls relying on the default universe Psyop#3
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienblor committed Jun 23, 2021
1 parent 87c2311 commit 2eecd5d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 51 deletions.
105 changes: 56 additions & 49 deletions cryptomatte/cryptomatte.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ getting global
// AOVs are active (connected to EXR drivers), this does all the
// complicated setup work of creating multiple AOVs if necessary,
// writing metadata, etc.
data->setup_all(AiNodeGetStr(node, "aov_crypto_asset"),
// An arnold AtUniverse must be provided, so that we can find nodes
// in the proper universe
data->setup_all(universe,
AiNodeGetStr(node, "aov_crypto_asset"),
AiNodeGetStr(node, "aov_crypto_object"),
AiNodeGetStr(node, "aov_crypto_material"), uc_aov_array,
uc_src_array);
Expand Down Expand Up @@ -807,7 +810,8 @@ struct CryptomatteData {
AiMsgError("[Cryptomatte] Critical section was not initialized. ");
}

void setup_all(const AtString aov_cryptoasset_, const AtString aov_cryptoobject_,
void setup_all(AtUniverse *universe,
const AtString aov_cryptoasset_, const AtString aov_cryptoobject_,
const AtString aov_cryptomaterial_, AtArray* uc_aov_array,
AtArray* uc_src_array) {
aov_cryptoasset = aov_cryptoasset_;
Expand All @@ -819,7 +823,7 @@ struct CryptomatteData {
user_cryptomattes = UserCryptomattes(uc_aov_array, uc_src_array);

crypto_crit_sec_enter();
setup_outputs();
setup_outputs(universe);
crypto_crit_sec_leave();
}

Expand Down Expand Up @@ -853,9 +857,9 @@ struct CryptomatteData {
}
}

void write_sidecar_manifests() {
write_standard_sidecar_manifests();
write_user_sidecar_manifests();
void write_sidecar_manifests(AtUniverse *universe) {
write_standard_sidecar_manifests(universe);
write_user_sidecar_manifests(universe);
}

~CryptomatteData() { destroy_arrays(); }
Expand Down Expand Up @@ -966,16 +970,18 @@ struct CryptomatteData {
String driver_tok = "";
bool half_flag = false;
AtNode* raw_driver = nullptr;
AtUniverse *universe = nullptr;

private:
AtNode* driver = nullptr;

public:
TokenizedOutput() {}

TokenizedOutput(AtNode* raw_driver_in) { raw_driver = raw_driver_in; }
TokenizedOutput(AtUniverse *universe_in, AtNode* raw_driver_in) { universe = universe_in; raw_driver = raw_driver_in; }

TokenizedOutput(AtString output_string) {
TokenizedOutput(AtUniverse *universe_in, AtString output_string) {
universe = universe_in;
char* temp_string = strdup(output_string.c_str());
const String c0 = to_string_safe(strtok(temp_string, " "));
const String c1 = to_string_safe(strtok(nullptr, " "));
Expand All @@ -995,7 +1001,7 @@ struct CryptomatteData {
filter_tok = no_camera ? c2 : c3;
driver_tok = no_camera ? c3 : c4;

driver = AiNodeLookUpByName(driver_tok.c_str());
driver = AiNodeLookUpByName(universe, driver_tok.c_str());
}

String rebuild_output() const {
Expand Down Expand Up @@ -1023,7 +1029,7 @@ struct CryptomatteData {
if (driver && driver_tok == String(AiNodeGetName(driver)))
return driver;
else if (!driver_tok.empty())
return AiNodeLookUpByName(driver_tok.c_str());
return AiNodeLookUpByName(universe, driver_tok.c_str());
else
return nullptr;
}
Expand All @@ -1033,10 +1039,10 @@ struct CryptomatteData {
String to_string_safe(const char* c_str) const { return c_str ? c_str : ""; }
};

void setup_outputs() {
const AtArray* outputs = AiNodeGetArray(AiUniverseGetOptions(), "outputs");
void setup_outputs(AtUniverse *universe) {
const AtArray* outputs = AiNodeGetArray(AiUniverseGetOptions(universe), "outputs");
const uint32_t prev_output_num = AiArrayGetNumElements(outputs);
AtNode* noop_filter = option_exr_preview_channels ? nullptr : get_or_create_noop_filter();
AtNode* noop_filter = option_exr_preview_channels ? nullptr : get_or_create_noop_filter(universe);

// if a driver is set to half, it needs to be set to full,
// and its non-cryptomatte outputs need to be set to half.
Expand All @@ -1047,7 +1053,7 @@ struct CryptomatteData {
std::vector<TokenizedOutput> outputs_orig(prev_output_num), outputs_new;

for (uint32_t i = 0; i < prev_output_num; i++) {
TokenizedOutput t_output(AiArrayGetStr(outputs, i));
TokenizedOutput t_output(universe, AiArrayGetStr(outputs, i));
AtNode* driver = t_output.get_driver();

AtArray* crypto_aovs = nullptr;
Expand All @@ -1071,7 +1077,7 @@ struct CryptomatteData {
}

if (crypto_aovs && check_driver(driver)) {
setup_new_outputs(t_output, crypto_aovs, outputs_new);
setup_new_outputs(universe, t_output, crypto_aovs, outputs_new);

if (AiNodeGetBool(driver, "half_precision")) {
AiNodeSetBool(driver, "half_precision", false);
Expand All @@ -1086,8 +1092,8 @@ struct CryptomatteData {

if (outputs_new.size()) {
if (option_sidecar_manifests) {
AtNode* manifest_driver = setup_manifest_driver();
outputs_new.push_back(TokenizedOutput(manifest_driver));
AtNode* manifest_driver = setup_manifest_driver(universe);
outputs_new.push_back(TokenizedOutput(universe, manifest_driver));
}

for (auto& t_output : outputs_orig) {
Expand All @@ -1106,17 +1112,17 @@ struct CryptomatteData {
for (auto& t_output : outputs_new)
AiArraySetStr(final_outputs, i++, t_output.rebuild_output().c_str());

AiNodeSetArray(AiUniverseGetOptions(), "outputs", final_outputs);
AiNodeSetArray(AiUniverseGetOptions(universe), "outputs", final_outputs);
}

build_standard_metadata(driver_asset, driver_object, driver_material);
build_user_metadata(tmp_uc_drivers);
build_standard_metadata(universe, driver_asset, driver_object, driver_material);
build_user_metadata(universe, tmp_uc_drivers);
}

void setup_new_outputs(TokenizedOutput& t_output, AtArray* crypto_aovs,
std::vector<TokenizedOutput>& new_outputs) const {
void setup_new_outputs(AtUniverse *universe, TokenizedOutput& t_output,
AtArray* crypto_aovs, std::vector<TokenizedOutput>& new_outputs) const {
// Populates crypto_aovs and new_outputs
AtNode* orig_filter = AiNodeLookUpByName(t_output.filter_tok.c_str());
AtNode* orig_filter = AiNodeLookUpByName(universe, t_output.filter_tok.c_str());

// Outlaw RLE, dwaa, dwab
AtNode* driver = t_output.get_driver();
Expand All @@ -1136,7 +1142,7 @@ struct CryptomatteData {
AiNodeSetStr(driver, "compression", "zip");
}

AtArray* outputs = AiNodeGetArray(AiUniverseGetOptions(), "outputs");
AtArray* outputs = AiNodeGetArray(AiUniverseGetOptions(universe), "outputs");

std::unordered_set<String> output_set;
for (uint32_t i = 0; i < AiArrayGetNumElements(outputs); i++)
Expand All @@ -1150,8 +1156,8 @@ struct CryptomatteData {
const String filter_rank_name = t_output.aov_name_tok + "_filter" + rank_num;
const String aov_rank_name = t_output.aov_name_tok + rank_num;

if (AiNodeLookUpByName(filter_rank_name.c_str()) == nullptr)
AtNode* filter = create_filter(orig_filter, filter_rank_name, i);
if (AiNodeLookUpByName(universe, filter_rank_name.c_str()) == nullptr)
AtNode* filter = create_filter(universe, orig_filter, filter_rank_name, i);

TokenizedOutput new_t_output = t_output;
new_t_output.aov_name_tok = aov_rank_name;
Expand All @@ -1170,26 +1176,26 @@ struct CryptomatteData {
}
}

AtNode* create_filter(const AtNode* orig_filter, const String filter_name, int aovindex) const {
AtNode* create_filter(AtUniverse *universe, const AtNode* orig_filter, const String filter_name, int aovindex) const {
const AtNodeEntry* filter_nentry = AiNodeGetNodeEntry(orig_filter);
const auto width = AiNodeEntryLookUpParameter(filter_nentry, "width")
? AiNodeGetFlt(orig_filter, "width")
: 2.0f;
const String filter_type = AiNodeEntryGetName(filter_nentry);
const String filter_param = filter_type.substr(0, filter_type.find("_filter"));

AtNode* filter = AiNode("cryptomatte_filter", filter_name.c_str(), nullptr);
AtNode* filter = AiNode(universe, "cryptomatte_filter", filter_name.c_str(), nullptr);
AiNodeSetStr(filter, "filter", filter_param.c_str());
AiNodeSetInt(filter, "rank", aovindex * 2);
AiNodeSetFlt(filter, "width", width);
return filter;
}

AtNode* get_or_create_noop_filter() const {
AtNode* get_or_create_noop_filter(AtUniverse *universe) const {
const static AtString noop_filter_name("cryptomatte_noop_filter");
AtNode* filter = AiNodeLookUpByName(noop_filter_name);
AtNode* filter = AiNodeLookUpByName(universe, noop_filter_name);
if (!filter) {
filter = AiNode("cryptomatte_filter", noop_filter_name, nullptr);
filter = AiNode(universe, "cryptomatte_filter", noop_filter_name, nullptr);
AiNodeSetBool(filter, "noop", true);
}
return filter;
Expand All @@ -1203,11 +1209,11 @@ struct CryptomatteData {
return aovs;
}

AtNode* setup_manifest_driver() {
AtNode* setup_manifest_driver(AtUniverse *universe) {
AtString manifest_driver_name("cryptomatte_manifest_driver");
AtNode* manifest_driver = AiNodeLookUpByName(manifest_driver_name);
AtNode* manifest_driver = AiNodeLookUpByName(universe, manifest_driver_name);
if (!manifest_driver)
manifest_driver = AiNode("cryptomatte_manifest_driver", manifest_driver_name, nullptr);
manifest_driver = AiNode(universe, "cryptomatte_manifest_driver", manifest_driver_name, nullptr);
AiNodeSetLocalData(manifest_driver, this);
return manifest_driver;
}
Expand Down Expand Up @@ -1235,7 +1241,7 @@ struct CryptomatteData {
}
}

void write_standard_sidecar_manifests() {
void write_standard_sidecar_manifests(AtUniverse *universe) {
const bool do_md_asset = manif_asset_paths.size() > 0;
const bool do_md_object = manif_object_paths.size() > 0;
const bool do_md_material = manif_material_paths.size() > 0;
Expand All @@ -1244,7 +1250,7 @@ struct CryptomatteData {
return;

ManifestMap map_md_asset, map_md_object, map_md_material;
compile_standard_manifests(do_md_asset, do_md_object, do_md_material, map_md_asset,
compile_standard_manifests(universe, do_md_asset, do_md_object, do_md_material, map_md_asset,
map_md_object, map_md_material);

if (do_md_asset)
Expand All @@ -1260,10 +1266,10 @@ struct CryptomatteData {
manif_material_paths = StringVector();
}

void compile_standard_manifests(bool do_md_asset, bool do_md_object, bool do_md_material,
ManifestMap& map_md_asset, ManifestMap& map_md_object,
ManifestMap& map_md_material) {
AtNodeIterator* shape_iterator = AiUniverseGetNodeIterator(AI_NODE_SHAPE);
void compile_standard_manifests(AtUniverse *universe, bool do_md_asset, bool do_md_object,
bool do_md_material, ManifestMap& map_md_asset,
ManifestMap& map_md_object, ManifestMap& map_md_material) {
AtNodeIterator* shape_iterator = AiUniverseGetNodeIterator(universe, AI_NODE_SHAPE);
while (!AiNodeIteratorFinished(shape_iterator)) {
AtNode* node = AiNodeIteratorGetNext(shape_iterator);
if (!node || AiNodeIsDisabled(node))
Expand Down Expand Up @@ -1304,7 +1310,7 @@ struct CryptomatteData {
AiNodeIteratorDestroy(shape_iterator);
}

void write_user_sidecar_manifests() {
void write_user_sidecar_manifests(AtUniverse *universe) {
std::vector<bool> do_metadata;
do_metadata.resize(user_cryptomattes.count);
std::vector<ManifestMap> manf_maps;
Expand All @@ -1315,7 +1321,7 @@ struct CryptomatteData {
for (size_t j = 0; j < manifs_user_paths[i].size(); j++)
do_metadata[i] = do_metadata[i] && manifs_user_paths[i][j].length() > 0;
}
compile_user_manifests(do_metadata, manf_maps);
compile_user_manifests(universe, do_metadata, manf_maps);

for (size_t i = 0; i < manifs_user_paths.size(); i++)
if (do_metadata[i])
Expand All @@ -1324,11 +1330,11 @@ struct CryptomatteData {
manifs_user_paths = std::vector<StringVector>();
}

void compile_user_manifests(std::vector<bool>& do_metadata,
void compile_user_manifests(AtUniverse *universe, std::vector<bool>& do_metadata,
std::vector<ManifestMap>& manf_maps) {
if (user_cryptomattes.count == 0)
return;
AtNodeIterator* shape_iterator = AiUniverseGetNodeIterator(AI_NODE_SHAPE);
AtNodeIterator* shape_iterator = AiUniverseGetNodeIterator(universe, AI_NODE_SHAPE);
while (!AiNodeIteratorFinished(shape_iterator)) {
AtNode* node = AiNodeIteratorGetNext(shape_iterator);
if (!node || AiNodeIsDisabled(node))
Expand All @@ -1342,7 +1348,8 @@ struct CryptomatteData {
AiNodeIteratorDestroy(shape_iterator);
}

void build_standard_metadata(const std::vector<AtNode*>& driver_asset,
void build_standard_metadata(AtUniverse *universe,
const std::vector<AtNode*>& driver_asset,
const std::vector<AtNode*>& driver_object,
const std::vector<AtNode*>& driver_material) {
clock_t metadata_start_time = clock();
Expand All @@ -1357,8 +1364,8 @@ struct CryptomatteData {
ManifestMap map_md_asset, map_md_object, map_md_material;

if (!option_sidecar_manifests)
compile_standard_manifests(do_md_asset, do_md_object, do_md_material, map_md_asset,
map_md_object, map_md_material);
compile_standard_manifests(universe, do_md_asset, do_md_object, do_md_material,
map_md_asset, map_md_object, map_md_material);

String manif_asset_m, manif_object_m, manif_material_m;
manif_asset_paths.resize(driver_asset.size());
Expand Down Expand Up @@ -1390,7 +1397,7 @@ struct CryptomatteData {
"written at end of render.");
}

void build_user_metadata(const std::vector<std::vector<AtNode*>>& drivers_vv) {
void build_user_metadata(AtUniverse *universe, const std::vector<std::vector<AtNode*>>& drivers_vv) {
std::vector<StringVector> manifs_user_m;
manifs_user_paths = std::vector<StringVector>();
manifs_user_m.resize(drivers_vv.size());
Expand Down Expand Up @@ -1429,7 +1436,7 @@ struct CryptomatteData {
return;

if (!sidecar)
compile_user_manifests(do_metadata, manf_maps);
compile_user_manifests(universe, do_metadata, manf_maps);

for (uint32_t i = 0; i < drivers_vv.size(); i++) {
if (!do_metadata[i])
Expand Down
2 changes: 1 addition & 1 deletion cryptomatte/cryptomatte_manifest_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ driver_write_bucket {}
driver_close {
CryptomatteData* data = (CryptomatteData*)AiNodeGetLocalData(node);
if (data)
data->write_sidecar_manifests();
data->write_sidecar_manifests(AiNodeGetUniverse(node));
}

node_finish {}
Expand Down
3 changes: 2 additions & 1 deletion cryptomatte/cryptomatte_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ node_finish {

node_update {
CryptomatteData* data = reinterpret_cast<CryptomatteData*>(AiNodeGetLocalData(node));
AtUniverse *universe = AiNodeGetUniverse(node);

data->set_option_sidecar_manifests(AiNodeGetBool(node, "sidecar_manifests"));
data->set_option_channels(AiNodeGetInt(node, "cryptomatte_depth"),
Expand Down Expand Up @@ -107,7 +108,7 @@ node_update {
AiNodeGetStr(node, "user_crypto_src_2").c_str(),
AiNodeGetStr(node, "user_crypto_src_3").c_str());

data->setup_all(AiNodeGetStr(node, "aov_crypto_asset"), AiNodeGetStr(node, "aov_crypto_object"),
data->setup_all(universe, AiNodeGetStr(node, "aov_crypto_asset"), AiNodeGetStr(node, "aov_crypto_object"),
AiNodeGetStr(node, "aov_crypto_material"), uc_aov_array, uc_src_array);
}

Expand Down

0 comments on commit 2eecd5d

Please sign in to comment.