#include "OCIOConfigManager.h" #include #include OCIOConfigManager::OCIOConfigManager(): _ocioConfigPathMap() { } OCIO::ConstConfigRcPtr OCIOConfigManager::loadConfig(const std::string& configPath) { mFnAssert(!configPath.empty()); const bool addNewConfig = !hasConfig(configPath); if (addNewConfig) { addConfig(configPath); } return getConfig(configPath); } OCIO::ConstConfigRcPtr OCIOConfigManager::reloadConfig(const std::string& configPath) { addConfig(configPath); return getConfig(configPath); } bool OCIOConfigManager::hasConfig(const std::string& configPath) const { mFnAssert(!configPath.empty()); return (_ocioConfigPathMap.count(configPath) == 1); } void OCIOConfigManager::addConfig(const std::string& configPath) { mFnAssert(!configPath.empty()); try { OCIO::ConstConfigRcPtr config = OCIO::Config::CreateFromFile(configPath.c_str()); _ocioConfigPathMap[configPath] = { config, config->getCacheID() }; } catch (OCIO::Exception& exception) { _ocioConfigPathMap[configPath] = { OCIO::ConstConfigRcPtr(), std::string() }; } } OCIO::ConstConfigRcPtr OCIOConfigManager::getConfig(const std::string& configPath) const { mFnAssert(!configPath.empty()); mFnAssert(hasConfig(configPath)); const auto& configCachePair = _ocioConfigPathMap.at(configPath); return configCachePair.first; } /*static*/ OCIO::ConstConfigRcPtr NukeOCIOConfigManager::LoadConfig(const DD::Image::Op* op) { static NukeOCIOConfigManager* ocioConfigManager = new NukeOCIOConfigManager; return ocioConfigManager->loadConfig(op); } NukeOCIOConfigManager::NukeOCIOConfigManager(): _ocioConfigManager() { } OCIO::ConstConfigRcPtr NukeOCIOConfigManager::loadConfig(const DD::Image::Op* op) { // We have to register for changes here because its the only place we have access to the op registerForChangesOnRootNode(op); _configPath = getConfigPath(op); return _ocioConfigManager.loadConfig(_configPath); } std::string NukeOCIOConfigManager::getConfigPath(const DD::Image::Op* op) const { const DD::Image::Op* rootOp = op->rootOp(); mFnAssert(rootOp); const DD::Image::Knob* ocioConfigKnob = rootOp->knob("OCIOConfigPath"); mFnAssert(ocioConfigKnob); // Pass in a dummy OutputContext to get_text(), so that the File_Knob remaps the path it returns if needed. const DD::Image::OutputContext outputContext; const char* configPath = ocioConfigKnob->get_text(&outputContext); mFnAssert(configPath); return std::string(configPath); } void NukeOCIOConfigManager::registerForChangesOnRootNode(const DD::Image::Op* op) { const auto root = op->rootOp()->getNode(); root->registerKnobChangedObserver(this); } void NukeOCIOConfigManager::observedKnobChanged(DD::Image::Knob* knob) { if (knob->is("reloadConfig")) { _ocioConfigManager.reloadConfig(_configPath); } }