HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
USD/USD_TmpArResolver.C
/*
* Copyright 2019 Side Effects Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Produced by:
* Side Effects Software Inc
* 123 Front Street West, Suite 1401
* Toronto, Ontario
* Canada M5J 2M2
* 416-504-9876
*
* NAME: USD_TmpArResolver.C (USD Plugin, C++)
*
*/
#include "USD_TmpArResolver.h"
#include <UT/UT_DirUtil.h>
#include <string>
AR_DEFINE_RESOLVER(USD_TmpArResolver, ArResolver);
#define TMP_PREFIX "tmp:"
#define TMP_PREFIX_LEN 4
// NOTE: this doesn't quite work... This initialization code is only run when
// the plugin is loaded by USD, which only happens when USD is asked to read
// or write a "tmp:" URI. But by that point, Houdini may have already tried to
// process a "tmp:" prefixed path without knowing that it represents a full
// path. So this call to UTaddAbsolutePathPrefix should actually be added to
// a separate Houdini plugin that gets loaded as soon as Houdini starts, rather
// than waiting until USD decides to load this plugin.
static bool theUtInit = []{UTaddAbsolutePathPrefix(TMP_PREFIX); return true;}();
// ============================================================================
USD_TmpArResolver::USD_TmpArResolver()
{
}
USD_TmpArResolver::~USD_TmpArResolver()
{
}
USD_TmpArResolver::_CreateIdentifier(
const std::string& assetPath,
const ArResolvedPath& anchorAssetPath) const
{
// Ar will call this function if either assetPath or anchorAssetPath
// have a URI scheme that is associated with this resolver.
if (assetPath.empty()) {
return assetPath;
}
if (!anchorAssetPath) {
return TfNormPath(assetPath);
}
// If assetPath has a URI scheme it must be an absolute URI so we
// just return the normalized URI as the asset's identifier.
if (assetPath.length() > TMP_PREFIX_LEN &&
assetPath.substr(0, TMP_PREFIX_LEN).compare(TMP_PREFIX) == 0) {
return assetPath;
}
// Otherwise anchor assetPath to anchorAssetPath and return the
// normalized URI.
return TfStringCatPaths(anchorAssetPath, assetPath);
}
USD_TmpArResolver::_CreateIdentifierForNewAsset(
const std::string& assetPath,
const ArResolvedPath& anchorAssetPath) const
{
// Resolving is the same for reading and writing assets.
return _CreateIdentifier(assetPath, anchorAssetPath);
}
USD_TmpArResolver::_Resolve(
const std::string& assetPath) const
{
ArResolvedPath newpath = _ResolveForNewAsset(assetPath);
return TfPathExists(newpath) ? newpath : ArResolvedPath();
}
USD_TmpArResolver::_ResolveForNewAsset(
const std::string& assetPath) const
{
if (assetPath.empty()) {
return ArResolvedPath();
}
static const std::string theTempDir(TfGetenv("HOUDINI_TEMP_DIR"));
std::string newpath =
TfStringCatPaths(theTempDir, assetPath.substr(TMP_PREFIX_LEN));
return ArResolvedPath(newpath);
}
std::shared_ptr<ArAsset>
USD_TmpArResolver::_OpenAsset(
const ArResolvedPath& resolvedPath) const
{
return ArFilesystemAsset::Open(resolvedPath);
}
std::shared_ptr<ArWritableAsset>
USD_TmpArResolver::_OpenAssetForWrite(
const ArResolvedPath& resolvedPath,
WriteMode writeMode) const
{
_ResolveForNewAsset(resolvedPath), writeMode);
}