bool UsdPrim::_HasAPI(const TfType& schemaType, bool validateSchemaType) const { if (validateSchemaType) { if (schemaType.IsUnknown()) { TF_CODING_ERROR("Unknown schema type (%s) is invalid for HasAPI query", schemaType.GetTypeName().c_str()); return false; } // Note that this is only hit in python code paths, // C++ clients would hit the static_asserts defined in prim.h auto schemaRegistry = UsdSchemaRegistry::GetInstance(); if (schemaRegistry.IsConcrete(schemaType)) { TF_CODING_ERROR("Provided schema type must be non-concrete"); return false; } if (schemaRegistry.IsTyped(schemaType)) { TF_CODING_ERROR("Provided schema type must be untyped"); return false; } } // Get our composed set of applied schemas static const auto usdSchemaBase = TfType::FindByName("UsdSchemaBase"); auto appliedSchemas = GetAppliedSchemas(); if (appliedSchemas.empty()) { return false; } auto foundMatch = [&appliedSchemas](const std::string& alias) { return std::find(appliedSchemas.begin(), appliedSchemas.end(), alias) != appliedSchemas.end(); }; // See if our schema is directly authored for (const auto& alias : usdSchemaBase.GetAliases(schemaType)) { if (foundMatch(alias)) { return true; } } // If we couldn't find it directly authored in apiSchemas, // consider derived types. For example, if a user queries // prim.HasAPI<UsdModelAPI>() on a prim with // apiSchemas = ["UsdGeomModelAPI"], we should return true std::set<TfType> derivedTypes; schemaType.GetAllDerivedTypes(&derivedTypes); for (const auto& derived : derivedTypes) { for (const auto& alias : usdSchemaBase.GetAliases(derived)) { if (foundMatch(alias)) { return true; } } } return false; }
bool UsdPrim::_IsA(const TfType& schemaType) const { // Check Schema TfType if (schemaType.IsUnknown()) { TF_CODING_ERROR("Unknown schema type (%s) is invalid for IsA query", schemaType.GetTypeName().c_str()); return false; } // Get Prim TfType const std::string &typeName = GetTypeName().GetString(); return !typeName.empty() && PlugRegistry::FindDerivedTypeByName<UsdSchemaBase>(typeName). IsA(schemaType); }
bool SdfPropertySpec::SetDefaultValue(const VtValue &defaultValue) { if (defaultValue.IsEmpty()) { ClearDefaultValue(); return true; } if (defaultValue.IsHolding<SdfValueBlock>()) { return SetField(SdfFieldKeys->Default, defaultValue); } TfType valueType = GetValueType(); if (valueType.IsUnknown()) { TF_CODING_ERROR("Can't set value on attribute <%s> with " "unknown type \"%s\"", GetPath().GetText(), GetTypeName().GetAsToken().GetText()); return false; } if (ARCH_UNLIKELY(valueType.GetTypeid() == typeid(void))) { // valueType may be provided by a plugin that has not been loaded. // In that case, we cannot get the type info, which is required to cast. // So we load the plugin in that case. if (PlugPluginPtr p = PlugRegistry::GetInstance().GetPluginForType(valueType)) { p->Load(); } } VtValue value = VtValue::CastToTypeid(defaultValue, valueType.GetTypeid()); if (value.IsEmpty()) { TF_CODING_ERROR("Can't set value on <%s> to %s: " "expected a value of type \"%s\"", GetPath().GetText(), TfStringify(defaultValue).c_str(), valueType.GetTypeName().c_str()); return false; } return SetField(SdfFieldKeys->Default, value); }