StageAttributes::ptr StageAttributesManager::initNewObjectInternal()

in src/esp/metadata/managers/StageAttributesManager.cpp [162:323]


StageAttributes::ptr StageAttributesManager::initNewObjectInternal(
    const std::string& attributesHandle,
    bool builtFromConfig) {
  // If default template exists from some source, create this template as a
  // copy
  StageAttributes::ptr newAttributes =
      this->constructFromDefault(attributesHandle);
  bool createNewAttributes = (nullptr == newAttributes);
  if (createNewAttributes) {
    newAttributes = StageAttributes::create(attributesHandle);
  }
  // set the attributes source filedirectory, from the attributes name
  this->setFileDirectoryFromHandle(newAttributes);

  if (!createNewAttributes) {
    // default exists and was used to create this attributes - investigate any
    // filename fields that may have %%USE_FILENAME%% directive specified in the
    // default attributes.
    // Render asset handle
    setHandleFromDefaultTag(newAttributes,
                            newAttributes->getRenderAssetHandle(),
                            [newAttributes](const std::string& newHandle) {
                              newAttributes->setRenderAssetHandle(newHandle);
                            });
    // Collision asset handle
    setHandleFromDefaultTag(newAttributes,
                            newAttributes->getCollisionAssetHandle(),
                            [newAttributes](const std::string& newHandle) {
                              newAttributes->setCollisionAssetHandle(newHandle);
                            });
    // navmesh asset handle
    setHandleFromDefaultTag(newAttributes,
                            newAttributes->getNavmeshAssetHandle(),
                            [newAttributes](const std::string& newHandle) {
                              newAttributes->setNavmeshAssetHandle(newHandle);
                            });
    // Semantic Scene Descriptor text filehandle
    setHandleFromDefaultTag(
        newAttributes, newAttributes->getSemanticDescriptorFilename(),
        [newAttributes](const std::string& newHandle) {
          newAttributes->setSemanticDescriptorFilename(newHandle);
        });
    // Semantic Scene asset handle
    setHandleFromDefaultTag(newAttributes,
                            newAttributes->getSemanticAssetHandle(),
                            [newAttributes](const std::string& newHandle) {
                              newAttributes->setSemanticAssetHandle(newHandle);
                            });
  }

  // set defaults that config files or other constructive processes might
  // override

  // set defaults from SimulatorConfig values;
  newAttributes->setLightSetupKey(cfgLightSetup_);
  newAttributes->setForceFlatShading(cfgLightSetup_ == NO_LIGHT_KEY);
  // set value from config so not necessary to be passed as argument
  newAttributes->setFrustumCulling(cfgFrustumCulling_);

  // only set handle defaults if attributesHandle is not a config file (which
  // would never be a valid render or collision asset name).  Otherise, expect
  // handles to be set when config is read.
  if (!builtFromConfig) {
    if (newAttributes->getRenderAssetHandle().empty()) {
      newAttributes->setRenderAssetHandle(attributesHandle);
    }
    if (newAttributes->getCollisionAssetHandle().empty()) {
      newAttributes->setCollisionAssetHandle(attributesHandle);
    }

    if (attributesHandle != "NONE") {
      // TODO when all datasets have configuration support, get rid of all these
      // default settings

      // set defaults for navmesh, semantic mesh and lexicon handles
      // start with root stage name, including path but without final extension
      // default navmesh should have .navmesh ext
      if (newAttributes->getNavmeshAssetHandle().empty()) {
        std::string navmeshFilename =
            this->findFilenameUsingCriteria(attributesHandle, {".navmesh"});
        // if present, file was found, so set value (overriding attributes
        // default)
        if (!navmeshFilename.empty()) {
          newAttributes->setNavmeshAssetHandle(navmeshFilename);
        }
      }

      if (newAttributes->getSemanticDescriptorFilename().empty()) {
        // Build default semantic descriptor file name, using extensions of
        std::string ssdFileName = this->findFilenameUsingCriteria(
            attributesHandle, {".house", ".scn", "_semantic.txt"});

        // if not present, set hacky defaults for back compat expectations.
        if (ssdFileName.empty()) {
          if (attributesHandle.find("/replica_dataset") != std::string::npos) {
            // replica hack until dataset gets appropriate configuration support
            ssdFileName = Cr::Utility::Directory::join(
                newAttributes->getFileDirectory(), "info_semantic.json");
          } else {
            // hack to support back-compat until configs are implemented for all
            // datasets
            ssdFileName =
                Cr::Utility::Directory::splitExtension(attributesHandle).first +
                ".scn";
          }
        }
        newAttributes->setSemanticDescriptorFilename(ssdFileName);
      }

      if (newAttributes->getSemanticAssetHandle().empty()) {
        // Build default semantic mesh filename as root stage name ending with
        // "_semantic.ply", for back-compat with Mp3d
        std::string semanticMeshFilename = this->findFilenameUsingCriteria(
            attributesHandle, {"_semantic.ply"});
        // if present, file was found, so set value (overriding attributes
        // default)
        if (!semanticMeshFilename.empty()) {
          newAttributes->setSemanticAssetHandle(semanticMeshFilename);
        }
      }
    }  // do not populate defaults for NONE scene

    // set default origin and orientation values based on file name
    // from AssetInfo::fromPath
    // set defaults for passed render asset handles
    StageAttributesManager::setDefaultAssetNameBasedAttributes(
        newAttributes, createNewAttributes,
        newAttributes->getRenderAssetHandle(), [newAttributes](auto&& PH1) {
          newAttributes->setRenderAssetType(std::forward<decltype(PH1)>(PH1));
        });
    // set defaults for passed collision asset handles
    StageAttributesManager::setDefaultAssetNameBasedAttributes(
        newAttributes, false, newAttributes->getCollisionAssetHandle(),
        [newAttributes](auto&& PH1) {
          newAttributes->setCollisionAssetType(
              std::forward<decltype(PH1)>(PH1));
        });

    // set defaults for passed semantic asset handles
    StageAttributesManager::setDefaultAssetNameBasedAttributes(
        newAttributes, false, newAttributes->getSemanticAssetHandle(),
        [newAttributes](auto&& PH1) {
          newAttributes->setSemanticAssetType(std::forward<decltype(PH1)>(PH1));
        });
    // TODO : get rid of this once the hardcoded mesh-type handling is removed,
    // but for now force all semantic assets to be instance_mesh
    newAttributes->setSemanticAssetType(
        static_cast<int>(AssetType::INSTANCE_MESH));
  }
  // set default physical quantities specified in physics manager attributes
  if (physicsAttributesManager_->getObjectLibHasHandle(
          physicsManagerAttributesHandle_)) {
    auto physMgrAttributes = physicsAttributesManager_->getObjectByHandle(
        physicsManagerAttributesHandle_);
    newAttributes->setGravity(physMgrAttributes->getGravity());
    newAttributes->setFrictionCoefficient(
        physMgrAttributes->getFrictionCoefficient());
    newAttributes->setRestitutionCoefficient(
        physMgrAttributes->getRestitutionCoefficient());
  }
  return newAttributes;
}  // StageAttributesManager::initNewObjectInternal