static std::vector<T> _Vector(const VtDictionary& userArgs, const TfToken& key) { // Check that vector exists. if (!VtDictionaryIsHolding<std::vector<VtValue>>(userArgs, key)) { TF_CODING_ERROR("Dictionary is missing required key '%s' or key is " "not vector type", key.GetText()); return std::vector<T>(); } // Check that vector is correctly-typed. std::vector<VtValue> vals = VtDictionaryGet<std::vector<VtValue>>(userArgs, key); if (!std::all_of(vals.begin(), vals.end(), [](const VtValue& v) { return v.IsHolding<T>(); })) { TF_CODING_ERROR("Vector at dictionary key '%s' contains elements of " "the wrong type", key.GetText()); return std::vector<T>(); } // Extract values. std::vector<T> result; for (const VtValue& v : vals) { result.push_back(v.UncheckedGet<T>()); } return result; }
void UsdImagingGprimAdapter::_DiscoverPrimvars(UsdGeomGprim const& gprim, SdfPath const& cachePath, UsdShadeShader const& shader, UsdTimeCode time, UsdImagingValueCache* valueCache) { // TODO: It might be convenient to implicitly wire up PtexFaceOffset and // PtexFaceIndex primvars. TF_DEBUG(USDIMAGING_SHADERS).Msg("\t Looking for <%s> primvars at <%s>\n", gprim.GetPrim().GetPath().GetText(), shader.GetPrim().GetPath().GetText()); for (UsdShadeParameter const& param : shader.GetParameters()) { UsdShadeShader source; TfToken outputName; if (param.GetConnectedSource(&source, &outputName)) { UsdAttribute attr = source.GetIdAttr(); TfToken id; if (not attr or not attr.Get(&id)) { continue; } TF_DEBUG(USDIMAGING_SHADERS).Msg("\t\t Param <%s> connected <%s>(%s)\n", param.GetAttr().GetName().GetText(), source.GetPath().GetText(), id.GetText()); if (id == UsdHydraTokens->HwPrimvar_1) { TfToken t; VtValue v; UsdGeomPrimvar primvarAttr; if (UsdHydraPrimvar(source).GetVarnameAttr().Get(&t, UsdTimeCode::Default())) { primvarAttr = gprim.GetPrimvar(t); if (primvarAttr.ComputeFlattened(&v, time)) { TF_DEBUG(USDIMAGING_SHADERS).Msg("Found primvar %s\n", t.GetText()); UsdImagingValueCache::PrimvarInfo primvar; primvar.name = t; primvar.interpolation = primvarAttr.GetInterpolation(); valueCache->GetPrimvar(cachePath, t) = v; _MergePrimvar(primvar, &valueCache->GetPrimvars(cachePath)); } else { TF_DEBUG(USDIMAGING_SHADERS).Msg( "\t\t No primvar on <%s> named %s\n", gprim.GetPath().GetText(), t.GetText()); } } } else { // Recursively look for more primvars _DiscoverPrimvars(gprim, cachePath, source, time, valueCache); } } } }
UsdGeomXformOp UsdGeomXformable::AddXformOp( UsdGeomXformOp::Type const opType, UsdGeomXformOp::Precision const precision, TfToken const &opSuffix, bool isInverseOp) const { VtTokenArray xformOpOrder; _GetXformOpOrderValue(&xformOpOrder); // Check if the xformOp we're about to add already exists in xformOpOrder TfToken opName = UsdGeomXformOp::GetOpName(opType, opSuffix, isInverseOp); VtTokenArray::iterator it = std::find(xformOpOrder.begin(), xformOpOrder.end(), opName); if (it != xformOpOrder.end()) { TF_CODING_ERROR("The xformOp '%s' already exists in xformOpOrder [%s].", opName.GetText(), TfStringify(xformOpOrder).c_str()); return UsdGeomXformOp(); } TfToken const &xformOpAttrName = UsdGeomXformOp::GetOpName(opType, opSuffix); UsdGeomXformOp result; if (UsdAttribute xformOpAttr = GetPrim().GetAttribute(xformOpAttrName)) { // Check if the attribute's typeName has the requested precision level. UsdGeomXformOp::Precision existingPrecision = UsdGeomXformOp::GetPrecisionFromValueTypeName( xformOpAttr.GetTypeName()); if (existingPrecision != precision) { TF_CODING_ERROR("XformOp <%s> has typeName '%s' which does not " "match the requested precision '%s'. Proceeding to " "use existing typeName / precision.", xformOpAttr.GetPath().GetText(), xformOpAttr.GetTypeName().GetAsToken().GetText(), TfEnum::GetName(precision).c_str()); } result = UsdGeomXformOp(xformOpAttr, isInverseOp); } else { result = UsdGeomXformOp(GetPrim(), opType, precision, opSuffix, isInverseOp); } if (result) { xformOpOrder.push_back(result.GetOpName()); CreateXformOpOrderAttr().Set(xformOpOrder); } else { TF_CODING_ERROR("Unable to add xform op of type %s and precision %s on " "prim at path <%s>. opSuffix=%s, isInverseOp=%d", TfEnum::GetName(opType).c_str(), TfEnum::GetName(precision).c_str(), GetPath().GetText(), opSuffix.GetText(), isInverseOp); return UsdGeomXformOp(); } return result; }
UsdMayaAdaptor::SchemaAdaptor UsdMayaAdaptor::ApplySchemaByName( const TfToken& schemaName, MDGModifier& modifier) { if (!*this) { TF_CODING_ERROR("Adaptor is not valid"); return SchemaAdaptor(); } // Get the schema's TfType; its name should be registered as an alias. const TfType schemaType = TfType::Find<UsdSchemaBase>().FindDerivedByName(schemaName); // Make sure that this is an API schema. Only API schemas can be applied. if (!schemaType.IsA<UsdAPISchemaBase>()) { TF_CODING_ERROR("'%s' is not a registered API schema", schemaName.GetText()); return SchemaAdaptor(); } // Make sure that this is an "apply" schema. if (!UsdSchemaRegistry::GetInstance().IsAppliedAPISchema(schemaType)) { TF_CODING_ERROR("'%s' is not an applied API schema", schemaName.GetText()); return SchemaAdaptor(); } // Get the schema definition. If it's registered, there should be a def. SdfPrimSpecHandle primDef = UsdSchemaRegistry::GetInstance().GetPrimDefinition(schemaName); if (!primDef) { TF_CODING_ERROR("Can't find schema definition for name '%s'", schemaName.GetText()); return SchemaAdaptor(); } // Add to schema list (if not yet present). TfTokenVector currentSchemas = GetAppliedSchemas(); if (std::find(currentSchemas.begin(), currentSchemas.end(), schemaName) == currentSchemas.end()) { currentSchemas.push_back(schemaName); SetMetadata( UsdTokens->apiSchemas, _GetListOpForTokenVector(currentSchemas), modifier); } return SchemaAdaptor(_handle.object(), primDef); }
void HdPerfLog::AddCacheMiss(TfToken const& name, SdfPath const& id, TfToken const& tag) { if (ARCH_LIKELY(not _enabled)) return; _Lock lock(_mutex); _cacheMap[name].AddMiss(); TF_DEBUG(HD_CACHE_MISSES).Msg("Cache miss: %s %s %s Total misses: %lu\n", name.GetText(), id.GetText(), tag.GetText(), _cacheMap[name].GetMisses()); }
void HdPerfLog::AddCacheHit(TfToken const& name, SdfPath const& id, TfToken const& tag) { if (ARCH_LIKELY(not _enabled)) return; _Lock lock(_mutex); _cacheMap[name].AddHit(); TF_DEBUG(HD_CACHE_HITS).Msg("Cache hit: %s %s %s hits: %lu\n", name.GetText(), id.GetText(), tag.GetText(), _cacheMap[name].GetHits()); }
static std::string _GetRelPrefix(const TfToken& renderTarget) { return TfStringPrintf("%s:%s", renderTarget.GetText(), _tokens->interfaceRecipientsOf.GetText()); }
/* static */ UsdGeomXformOp::Type UsdGeomXformOp::GetOpTypeEnum(TfToken const &opTypeToken) { if (opTypeToken == UsdGeomXformOpTypes->transform) return TypeTransform; else if (opTypeToken == UsdGeomXformOpTypes->translate) return TypeTranslate; // RotateXYZ is expected to be more common than the remaining ops. else if (opTypeToken == UsdGeomXformOpTypes->rotateXYZ) return TypeRotateXYZ; else if (opTypeToken == UsdGeomXformOpTypes->scale) return TypeScale; else if (opTypeToken == UsdGeomXformOpTypes->rotateX) return TypeRotateX; else if (opTypeToken == UsdGeomXformOpTypes->rotateY) return TypeRotateY; else if (opTypeToken == UsdGeomXformOpTypes->rotateZ) return TypeRotateZ; else if (opTypeToken == UsdGeomXformOpTypes->rotateXZY) return TypeRotateXZY; else if (opTypeToken == UsdGeomXformOpTypes->rotateYXZ) return TypeRotateYXZ; else if (opTypeToken == UsdGeomXformOpTypes->rotateYZX) return TypeRotateYZX; else if (opTypeToken == UsdGeomXformOpTypes->rotateZXY) return TypeRotateZXY; else if (opTypeToken == UsdGeomXformOpTypes->rotateZYX) return TypeRotateZYX; else if (opTypeToken == UsdGeomXformOpTypes->orient) return TypeOrient; TF_CODING_ERROR("Invalid xform opType token %s.", opTypeToken.GetText()); return TypeInvalid; }
void Hd_PrimTypeIndex<PrimType>::RemovePrim(const TfToken &typeId, const SdfPath &primId, HdChangeTracker &tracker, HdRenderDelegate *renderDelegate) { HD_TRACE_FUNCTION(); HF_MALLOC_TAG_FUNCTION(); typename _TypeIndex::iterator typeIt = _index.find(typeId); if (typeIt ==_index.end()) { TF_CODING_ERROR("Unsupported prim type: %s", typeId.GetText()); return; } _PrimTypeEntry &typeEntry = _entries[typeIt->second]; typename _PrimMap::iterator primIt = typeEntry.primMap.find(primId); if (primIt == typeEntry.primMap.end()) { return; } _TrackerRemovePrim(tracker, primId); _PrimInfo &primInfo = primIt->second; _RenderDelegateDestroyPrim(renderDelegate, primInfo.prim); primInfo.prim = nullptr; typeEntry.primMap.erase(primIt); typeEntry.primIds.Remove(primId); }
static bool _GetMayaDictValue(const UsdAttribute& attr, const TfToken& key, T* outVal) { VtValue data = attr.GetCustomDataByKey(_tokens->Maya); if (!data.IsEmpty()) { if (data.IsHolding<VtDictionary>()) { VtValue val; if (TfMapLookup(data.UncheckedGet<VtDictionary>(), key, &val)) { if (val.IsHolding<T>()) { *outVal = val.UncheckedGet<T>(); return true; } else { TF_WARN("Unexpected type for %s[%s] on <%s>.", _tokens->Maya.GetText(), key.GetText(), attr.GetPath().GetText()); } } } else { TF_WARN("Expected to get %s on <%s> to be a dictionary.", _tokens->Maya.GetText(), attr.GetPath().GetText()); } } return false; }
static TfToken _GetMaterialsScopeName(const std::string& materialsScopeName) { const TfToken defaultMaterialsScopeName = UsdUtilsGetMaterialsScopeName(); if (TfGetEnvSetting(USD_FORCE_DEFAULT_MATERIALS_SCOPE_NAME)) { // If the env setting is set, make sure we don't allow the materials // scope name to be overridden by a parameter value. return defaultMaterialsScopeName; } if (SdfPath::IsValidIdentifier(materialsScopeName)) { return TfToken(materialsScopeName); } TF_CODING_ERROR( "'%s' value '%s' is not a valid identifier. Using default " "value of '%s' instead.", UsdMayaJobExportArgsTokens->materialsScopeName.GetText(), materialsScopeName.c_str(), defaultMaterialsScopeName.GetText()); return defaultMaterialsScopeName; }
static bool _GetMetadataUnchecked( const MFnDependencyNode& node, const TfToken& key, VtValue* value) { VtValue fallback = SdfSchema::GetInstance().GetFallback(key); if (fallback.IsEmpty()) { return false; } std::string mayaAttrName = _GetMayaAttrNameForMetadataKey(key); MPlug plug = node.findPlug(mayaAttrName.c_str()); if (plug.isNull()) { return false; } TfType ty = fallback.GetType(); VtValue result = UsdMayaWriteUtil::GetVtValue(plug, ty, TfToken()); if (result.IsEmpty()) { TF_RUNTIME_ERROR( "Cannot convert plug '%s' into metadata '%s' (%s)", plug.name().asChar(), key.GetText(), ty.GetTypeName().c_str()); return false; } *value = result; return true; }
static bool _TranslateUsdAttributeToPlug( const UsdAttribute& usdAttr, const MFnCamera& cameraFn, TfToken plugName, const PxrUsdMayaPrimReaderArgs& args, PxrUsdMayaPrimReaderContext* context, bool millimetersToInches=false) { MStatus status; MPlug plug = cameraFn.findPlug(plugName.GetText(), true, &status); CHECK_MSTATUS_AND_RETURN(status, false); // First check for and translate animation if there is any. if (!_TranslateAnimatedUsdAttributeToPlug(usdAttr, plug, args, context, millimetersToInches)) { // If that fails, then try just setting a static value. UsdTimeCode timeCode = UsdTimeCode::EarliestTime(); float attrValue; usdAttr.Get(&attrValue, timeCode); if (millimetersToInches) { attrValue = PxrUsdMayaUtil::ConvertMMToInches(attrValue); } status = plug.setFloat(attrValue); CHECK_MSTATUS_AND_RETURN(status, false); } return true; }
Hd_BindlessSamplerBufferSource(TfToken const &name, GLenum type, size_t value) : _name(name), _type(type), _value(value) { if (_value == 0) { TF_CODING_ERROR("Invalid texture handle: %s: %ld\n", name.GetText(), value); } }
bool UsdMayaAdaptor::SetMetadata( const TfToken& key, const VtValue& value, MDGModifier& modifier) { if (!*this) { TF_CODING_ERROR("Adaptor is not valid"); return false; } VtValue fallback; if (!SdfSchema::GetInstance().IsRegistered(key, &fallback)) { TF_CODING_ERROR("Metadata key '%s' is not registered", key.GetText()); return false; } if (fallback.IsEmpty()) { return false; } VtValue castValue = VtValue::CastToTypeOf(value, fallback); if (castValue.IsEmpty()) { TF_CODING_ERROR("Can't cast value to type '%s'", fallback.GetTypeName().c_str()); return false; } std::string mayaAttrName = _GetMayaAttrNameForMetadataKey(key); std::string mayaNiceAttrName = key.GetText(); MFnDependencyNode node(_handle.object()); TfType ty = fallback.GetType(); MObject attrObj = UsdMayaReadUtil::FindOrCreateMayaAttr( ty, TfToken(), SdfVariabilityUniform, node, mayaAttrName, mayaNiceAttrName, modifier); if (attrObj.isNull()) { return false; } MPlug plug = node.findPlug(attrObj); if (!UsdMayaReadUtil::SetMayaAttr(plug, castValue, modifier)) { return false; } return true; }
SdfPath SdfPath::AppendChild(TfToken const &childName) const { if (!IsAbsoluteRootOrPrimPath() && !IsPrimVariantSelectionPath() && (*this != ReflexiveRelativePath())) { TF_WARN("Cannot append child '%s' to path '%s'.", childName.GetText(), GetText()); return EmptyPath(); } if (ARCH_UNLIKELY(childName == SdfPathTokens->parentPathElement)) { return GetParentPath(); } else { if (ARCH_UNLIKELY(!_IsValidIdentifier(childName))) { TF_WARN("Invalid prim name '%s'", childName.GetText()); return EmptyPath(); } return SdfPath(Sdf_PathNode::FindOrCreatePrim(_pathNode, childName)); } }
/// Extracts a bool at \p key from \p userArgs, or false if it can't extract. static bool _Boolean(const VtDictionary& userArgs, const TfToken& key) { if (!VtDictionaryIsHolding<bool>(userArgs, key)) { TF_CODING_ERROR("Dictionary is missing required key '%s' or key is " "not bool type", key.GetText()); return false; } return VtDictionaryGet<bool>(userArgs, key); }
/// Extracts a string at \p key from \p userArgs, or "" if it can't extract. static std::string _String(const VtDictionary& userArgs, const TfToken& key) { if (!VtDictionaryIsHolding<std::string>(userArgs, key)) { TF_CODING_ERROR("Dictionary is missing required key '%s' or key is " "not string type", key.GetText()); return std::string(); } return VtDictionaryGet<std::string>(userArgs, key); }
unsigned HdChangeTracker::GetCollectionVersion(TfToken const& collectionName) const { _CollectionStateMap::const_iterator it = _collectionState.find(collectionName); if (!(it != _collectionState.end())) { TF_CODING_ERROR("Change Tracker unable to find collection %s", collectionName.GetText()); return _indexVersion; } return it->second + _indexVersion; }
void HdChangeTracker::MarkStateDirty(TfToken const& name) { _GeneralStateMap::iterator it = _generalState.find(name); if (it != _generalState.end()) { ++it->second; } else { TF_CODING_ERROR("Change Tracker unable to find state %s", name.GetText()); } }
/* static */ TfToken UsdShadeInterfaceAttribute::_GetName( const TfToken& interfaceAttrName) { if (TfStringStartsWith(interfaceAttrName, _tokens->interface)) { return interfaceAttrName; } return TfToken(TfStringPrintf("%s%s", _tokens->interface.GetText(), interfaceAttrName.GetText())); }
bool UsdGeomPrimvar::SetInterpolation(const TfToken &interpolation) { if (!IsValidInterpolation(interpolation)){ TF_CODING_ERROR("Attempt to set invalid primvar interpolation " "\"%s\" for attribute %s", interpolation.GetText(), _attr.GetPath().GetString().c_str()); return false; } return _attr.SetMetadata(UsdGeomTokens->interpolation, interpolation); }
unsigned HdChangeTracker::GetStateVersion(TfToken const &name) const { _GeneralStateMap::const_iterator it = _generalState.find(name); if (it != _generalState.end()) { return it->second; } else { TF_CODING_ERROR("Change Tracker unable to find state %s", name.GetText()); return 0; } }
TfToken KindRegistry::_GetBaseKind(const TfToken &kind) const { _KindMap::const_iterator it = _kindMap.find(kind); if (it == _kindMap.end()) { TF_CODING_ERROR("Unknown kind: '%s'", kind.GetText()); return TfToken(); } return it->second.baseKind; }
void KindRegistry::_Register(const TfToken& kind, const TfToken& baseKind) { if (not TfIsValidIdentifier(kind.GetString())) { TF_CODING_ERROR("Invalid kind: '%s'", kind.GetText()); return; } _KindMap::const_iterator it = _kindMap.find(kind); if (it != _kindMap.end()) { TF_CODING_ERROR("Kind '%s' has already been registered", kind.GetText()); return; } _KindData data; data.baseKind = baseKind; _kindMap[kind] = data; }
UsdMayaAdaptor::SchemaAdaptor UsdMayaAdaptor::GetSchemaByName(const TfToken& schemaName) const { if (!*this) { return SchemaAdaptor(); } // Get the schema definition. If it's registered, there should be a def. SdfPrimSpecHandle primDef = UsdSchemaRegistry::GetInstance().GetPrimDefinition(schemaName); if (!primDef) { return SchemaAdaptor(); } // Get the schema's TfType; its name should be registered as an alias. const TfType schemaType = TfType::Find<UsdSchemaBase>().FindDerivedByName(schemaName); // Is this an API schema? if (schemaType.IsA<UsdAPISchemaBase>()) { return SchemaAdaptor(_handle.object(), primDef); } // Is this a typed schema? else if (schemaType.IsA<UsdSchemaBase>()) { // XXX // We currently require an exact type match instead of the polymorphic // behavior that actual USD schema classes implement. This is because // we can't currently get the prim definition from the schema registry // for non-concrete schemas like Imageable (see bug 160436). Ideally, // once that's resolved, we would cache a mapping of Maya types to all // compatible USD type names based on schema inheritance. // (In that future world, we'll also want to special case some schemas // like UsdGeomImageable to be "compatible" with all Maya nodes.) const TfToken objectTypeName = GetUsdTypeName(); if (schemaName == objectTypeName) { // There's an exact MFn::Type match? Easy-peasy. return SchemaAdaptor(_handle.object(), primDef); } else { // If no match, do not allow usage of the typed-schema adaptor // mechanism. The importer/exporter have not declared that they // will use the adaptor mechanism to handle this type. return SchemaAdaptor(); } } // We shouldn't be able to reach this (everything is either typed or API). TF_CODING_ERROR("'%s' isn't a known API or typed schema", schemaName.GetText()); return SchemaAdaptor(); }
/// Extracts a token at \p key from \p userArgs. /// If the token value is not either \p defaultToken or one of the /// \p otherTokens, then returns \p defaultToken instead. static TfToken _Token( const VtDictionary& userArgs, const TfToken& key, const TfToken& defaultToken, const std::vector<TfToken>& otherTokens) { const TfToken tok(_String(userArgs, key)); for (const TfToken& allowedTok : otherTokens) { if (tok == allowedTok) { return tok; } } // Empty token will silently be promoted to default value. // Warning for non-empty tokens that don't match. if (tok != defaultToken && !tok.IsEmpty()) { TF_WARN("Value '%s' is not allowed for flag '%s'; using fallback '%s' " "instead", tok.GetText(), key.GetText(), defaultToken.GetText()); } return defaultToken; }
/*virtual*/ VtValue UsdMayaGLBatchRenderer::TaskDelegate::Get( SdfPath const& id, TfToken const &key) { _ValueCache *vcache = TfMapLookupPtr(_valueCacheMap, id); VtValue ret; if( vcache && TfMapLookup(*vcache, key, &ret) ) return ret; TF_CODING_ERROR("%s:%s doesn't exist in the value cache\n", id.GetText(), key.GetText()); return VtValue(); }
bool UsdGeomPointBased::SetNormalsInterpolation(TfToken const &interpolation) { if (UsdGeomPrimvar::IsValidInterpolation(interpolation)){ return GetNormalsAttr().SetMetadata(UsdGeomTokens->interpolation, interpolation); } TF_CODING_ERROR("Attempt to set invalid interpolation " "\"%s\" for normals attr on prim %s", interpolation.GetText(), GetPrim().GetPath().GetString().c_str()); return false; }
bool UsdMayaAdaptor::GetMetadata(const TfToken& key, VtValue* value) const { if (!*this) { return false; } if (!SdfSchema::GetInstance().IsRegistered(key)) { TF_CODING_ERROR("Metadata key '%s' is not registered", key.GetText()); return false; } MFnDependencyNode node(_handle.object()); return _GetMetadataUnchecked(node, key, value); }