UsdGeomPrimvar 
UsdGeomPrimvarsAPI::FindPrimvarWithInheritance(const TfToken &name) const
{
    TRACE_FUNCTION();

    const TfToken attrName = UsdGeomPrimvar::_MakeNamespaced(name);
    UsdPrim prim = GetPrim();
    if (!prim) {
        TF_CODING_ERROR("FindPrimvarWithInheritance called on invalid prim: %s", 
                        UsdDescribe(prim).c_str());
        return UsdGeomPrimvar();
    }
    UsdGeomPrimvar  localPv = GetPrimvar(name);
    if (localPv.HasAuthoredValue()){
        return localPv;
    }
    
    for (prim = prim.GetParent(); prim && !prim.IsPseudoRoot();
         prim = prim.GetParent()) {
        UsdAttribute attr = prim.GetAttribute(attrName);
        if (attr.HasAuthoredValue()) {
            if (UsdGeomPrimvar pv = UsdGeomPrimvar(attr)) {
                // Only constant primvars can be inherited.
                if (pv.GetInterpolation() == UsdGeomTokens->constant) {
                    return pv;
                } else {
                    // Non-constant interpolation blocks inheritance.
                    return UsdGeomPrimvar();
                }
            }
        }
    }
    return localPv;
}
bool
UsdGeomPrimvarsAPI::HasPossiblyInheritedPrimvar(const TfToken &name) const
{
    TRACE_FUNCTION();

    UsdPrim prim = GetPrim();
    if (!prim) {
        TF_CODING_ERROR("HasPossiblyInheritedPrimvar called on invalid prim: %s", 
                        UsdDescribe(prim).c_str());
        return false;
    }
    UsdGeomPrimvar  pv = GetPrimvar(name);
    if (pv.HasAuthoredValue()){
        return true;
    }

    const TfToken attrName = UsdGeomPrimvar::_MakeNamespaced(name);
    if (attrName.IsEmpty()) {
        return false;
    }
    for (prim = prim.GetParent(); prim && !prim.IsPseudoRoot();
         prim = prim.GetParent()) {
        UsdAttribute attr = prim.GetAttribute(attrName);
        if (attr.HasAuthoredValue() && UsdGeomPrimvar::IsPrimvar(attr)) {
            // Only constant primvars can be inherited.
            // Non-constant interpolation blocks inheritance.
            return UsdGeomPrimvar(attr).GetInterpolation()
                == UsdGeomTokens->constant;
        }
    }
    return false;
}
Exemple #3
0
UsdAttribute
PxrUsdMayaWriteUtil::GetOrCreateUsdAttr(
    const MPlug& plg,
    const UsdPrim& usdPrim,
    const std::string &attrName,
    bool custom)
{
    MObject attrObj(plg.attribute());

    TfToken usdAttrName(attrName);
    if (usdAttrName.IsEmpty()) {
        printf("Invalid attrName '%s' for %s\n",
               attrName.c_str(),
               plg.name().asChar());
        return UsdAttribute();
    }

    // See if usdAttr already exists.  If so, return.
    UsdAttribute usdAttr = usdPrim.GetAttribute(usdAttrName);
    if (usdAttr) {
        return usdAttr;
    }

    SdfValueTypeName attrType = PxrUsdMayaWriteUtil::GetUsdTypeName(plg);

    // ---------------------
    // CreateAttribute on USD Prim if specified above
    if (attrType) {
        usdAttr = usdPrim.CreateAttribute(usdAttrName, attrType, custom);
    }
    else {
        // Skipping.  Unsupported type.
    }
    return usdAttr;
}
Exemple #4
0
static bool
_GatherRibAttributes(
        const UsdPrim &prim, 
        double currentTime,
        FnKat::GroupBuilder& attrsBuilder)
{
    bool hasAttrs = false;

    // USD SHADING STYLE ATTRIBUTES
    UsdRiStatements riStatements(prim);
    if (riStatements) {
        const std::vector<UsdProperty> props = 
            riStatements.GetRiAttributes();
        std::string attrName;
        TF_FOR_ALL(propItr, props) {
            UsdProperty prop = *propItr;
            if (!prop) continue;

            std::string nameSpace = 
                riStatements.GetRiAttributeNameSpace(prop).GetString();
            nameSpace = TfStringReplace(nameSpace, ":", ".") + ".";

            attrName = nameSpace +
                riStatements.GetRiAttributeName(prop).GetString();

            VtValue vtValue;
            UsdAttribute usdAttr = prim.GetAttribute(prop.GetName());
            if (usdAttr) {
                if (not usdAttr.Get(&vtValue, currentTime)) 
                    continue;

                // XXX asShaderParam really means:
                // "For arrays, as a single attr vs a type/value pair group"
                // The type/value pair group is meaningful for attrs who don't
                // have a formal type definition -- like a "user" RiAttribute.
                // 
                // However, other array values (such as two-element shadingrate)
                // are not expecting the type/value pair form and will not
                // generate rib correctly. As such, we'll handle the "user"
                // attribute as a special case.
                bool asShaderParam = true;
                
                if (nameSpace == "user.")
                {
                    asShaderParam = false;
                }

                attrsBuilder.set(attrName, PxrUsdKatanaUtils::ConvertVtValueToKatAttr(vtValue,
                    asShaderParam) );
            }
            else {
                UsdRelationship usdRel = prim.GetRelationship(prop.GetName());
                attrsBuilder.set(attrName, PxrUsdKatanaUtils::ConvertRelTargetsToKatAttr(usdRel,
                    /* asShaderParam */ false) );
            }
            hasAttrs = true;
        }
    }
Exemple #5
0
void
UsdImagingPointsAdapter::_GetPoints(UsdPrim const& prim, 
                                   VtValue* value, 
                                   UsdTimeCode time)
{
    HD_TRACE_FUNCTION();
    if (!prim.GetAttribute(UsdGeomTokens->points).Get(value, time)) {
        *value = VtVec3fArray();
    }
}
Exemple #6
0
UsdShadeParameter::UsdShadeParameter(
        UsdPrim prim,
        TfToken const &name,
        SdfValueTypeName const &typeName)
{
    // XXX what do we do if the type name doesn't match and it exists already?

    _attr = prim.GetAttribute(name);
    if (!_attr) {
        _attr = prim.CreateAttribute(name, typeName, /* custom = */ false);
    }
}
Exemple #7
0
UsdShadeInterfaceAttribute::UsdShadeInterfaceAttribute(
        const UsdPrim& prim,
        TfToken const& interfaceAttrName,
        SdfValueTypeName const& typeName)
    : _name(interfaceAttrName)
{
    TfToken attrName = _GetName(interfaceAttrName);
    _attr = prim.GetAttribute(attrName);
    if (not _attr) {
        _attr = prim.CreateAttribute(attrName, typeName, /* custom = */ false);
    }
}
Exemple #8
0
UsdShadeOutput::UsdShadeOutput(
    UsdPrim prim,
    TfToken const &name,
    SdfValueTypeName const &typeName)
{
    // XXX what do we do if the type name doesn't match and it exists already?
    TfToken attrName = _GetOutputAttrName(name);
    _prop = prim.GetAttribute(attrName);
    if (!_prop) {
        _prop = prim.CreateAttribute(attrName, typeName, /* custom = */ false);
    }
}
Exemple #9
0
/* static */
UsdAttribute PxrUsdMayaWriteUtil::GetOrCreateUsdRiAttribute(
        const MPlug& attrPlug,
        const UsdPrim& usdPrim,
        const std::string& attrName,
        const std::string& nameSpace,
        const bool translateMayaDoubleToUsdSinglePrecision)
{
    UsdAttribute usdAttr;

    if (!usdPrim) {
        return usdAttr;
    }

    MObject attrObj(attrPlug.attribute());

    TfToken riAttrNameToken(attrName);
    if (riAttrNameToken.IsEmpty()) {
        MGlobal::displayError(
            TfStringPrintf("Invalid UsdRi attribute name '%s' for Maya plug '%s'",
                           attrName.c_str(),
                           attrPlug.name().asChar()).c_str());
        return usdAttr;
    }

    UsdRiStatements riStatements(usdPrim);
    if (!riStatements) {
        return usdAttr;
    }

    // See if a UsdRi attribute with this name already exists. If so, return it.
    // XXX: There isn't currently API for looking for a specific UsdRi attribute
    // by name, so we have to get them all and then see if one matches.
    const std::vector<UsdProperty>& riAttrs = riStatements.GetRiAttributes(nameSpace);
    TF_FOR_ALL(iter, riAttrs) {
        if (iter->GetBaseName() == riAttrNameToken) {
            // Re-get the attribute from the prim so we can return it as a
            // UsdAttribute rather than a UsdProperty.
            return usdPrim.GetAttribute(iter->GetName());
        }
    }

    const SdfValueTypeName& typeName =
        PxrUsdMayaWriteUtil::GetUsdTypeName(attrPlug,
                                            translateMayaDoubleToUsdSinglePrecision);
    if (typeName) {
        usdAttr = riStatements.CreateRiAttribute(riAttrNameToken,
                                                 typeName.GetType(),
                                                 nameSpace);
    }

    return usdAttr;
}
Exemple #10
0
/* static */
UsdAttribute
UsdGeomXformOp::_GetXformOpAttr(UsdPrim const& prim, const TfToken &opName,
                                bool *isInverseOp)
{
    *isInverseOp = _IsInverseOp(opName);

    // Is it is an inverse operation, strip off the "invert:" at the beginning 
    // of opName to get the associated attribute's name.
    const TfToken &xformOpAttrName = *isInverseOp 
        ? TfToken(opName.GetString().substr(
            _tokens->invertPrefix.GetString().size())) 
        : opName;
  
    return prim.GetAttribute(xformOpAttrName);
}
Exemple #11
0
/* static */
UsdAttribute
PxrUsdMayaWriteUtil::GetOrCreateUsdAttr(
        const MPlug& attrPlug,
        const UsdPrim& usdPrim,
        const std::string& attrName,
        const bool custom,
        const bool translateMayaDoubleToUsdSinglePrecision)
{
    UsdAttribute usdAttr;

    if (!usdPrim) {
        return usdAttr;
    }

    MObject attrObj(attrPlug.attribute());

    TfToken usdAttrNameToken(attrName);
    if (usdAttrNameToken.IsEmpty()) {
        MGlobal::displayError(
            TfStringPrintf("Invalid USD attribute name '%s' for Maya plug '%s'",
                           attrName.c_str(),
                           attrPlug.name().asChar()).c_str());
        return usdAttr;
    }

    // See if the USD attribute already exists. If so, return it.
    usdAttr = usdPrim.GetAttribute(usdAttrNameToken);
    if (usdAttr) {
        return usdAttr;
    }

    const SdfValueTypeName& typeName =
        PxrUsdMayaWriteUtil::GetUsdTypeName(attrPlug,
                                            translateMayaDoubleToUsdSinglePrecision);
    if (typeName) {
        usdAttr = usdPrim.CreateAttribute(usdAttrNameToken, typeName, custom);
    }

    return usdAttr;
}
Exemple #12
0
void
UsdImagingGprimAdapter::_DiscoverPrimvarsDeprecated(UsdGeomGprim const& gprim,
                                          SdfPath const& cachePath, 
                                          UsdPrim const& shaderPrim,
                                          UsdTimeCode time,
                                          UsdImagingValueCache* valueCache)
{
    UsdImagingValueCache::PrimvarInfo primvar;
    std::vector<UsdProperty> const& props 
                                    = shaderPrim.GetProperties();
    TF_FOR_ALL(propIt, props) {
        UsdAttribute attr = propIt->As<UsdAttribute>();
        if (not attr) {
            continue;
        }
        if (attr.GetPath().IsNamespacedPropertyPath()) {
            continue;
        }
        // Ok this is a parameter, check source input.
        if (UsdAttribute texAttr = shaderPrim.GetAttribute(
                                TfToken(attr.GetPath().GetName() 
                                + ":texture"))) {
            TfToken t;
            SdfAssetPath ap;
            VtValue v;
            UsdGeomPrimvar primvarAttr;
            texAttr.Get(&ap, UsdTimeCode::Default());
            bool isPtex = GlfPtexTexture::IsPtexTexture(TfToken(ap.GetAssetPath()));
            if (isPtex) {
                t = UsdImagingTokens->ptexFaceIndex;
                // Allow the client to override this name
                texAttr.GetMetadata(UsdImagingTokens->faceIndexPrimvar, &t);
                primvarAttr = gprim.GetPrimvar(t);
                if (primvarAttr) {
                    if (primvarAttr.ComputeFlattened(&v, time)) {
                        primvar.name = t;
                        primvar.interpolation = primvarAttr.GetInterpolation();
                        valueCache->GetPrimvar(cachePath, t) = v;
                        _MergePrimvar(primvar, &valueCache->GetPrimvars(cachePath));
                    }
                }
                t = UsdImagingTokens->ptexFaceOffset;
                // Allow the client to override this name
                texAttr.GetMetadata(UsdImagingTokens->faceOffsetPrimvar, &t);
                primvarAttr = gprim.GetPrimvar(t);
                if (primvarAttr) {
                    primvar.name = t;
                    primvar.interpolation = primvarAttr.GetInterpolation();
                    if (primvarAttr.ComputeFlattened(&v, time)) {
                        valueCache->GetPrimvar(cachePath, t) = v;
                        _MergePrimvar(primvar, &valueCache->GetPrimvars(cachePath));
                    }
                }
            } else {
                texAttr.GetMetadata(UsdImagingTokens->uvPrimvar, &t);
                primvarAttr = gprim.GetPrimvar(t);
                if (TF_VERIFY(primvarAttr, "%s\n", t.GetText())) {
                    if (TF_VERIFY(primvarAttr.ComputeFlattened(&v, time))) {
                        primvar.name = t; // does not include primvars:
                        primvar.interpolation = primvarAttr.GetInterpolation();
                        // Convert double to float, we don't need double precision.
                        if (v.IsHolding<VtVec2dArray>()) {
                            v = VtValue::Cast<VtVec2fArray>(v);
                        }
                        valueCache->GetPrimvar(cachePath, t) = v;
                        _MergePrimvar(primvar, &valueCache->GetPrimvars(cachePath));
                    }
                }
            }
        } else if (UsdAttribute pvAttr = shaderPrim.GetAttribute(
                                        TfToken(attr.GetPath().GetName() 
                                                + ":primvar"))) {
            TfToken t;
            VtValue v;
            UsdGeomPrimvar primvarAttr;
            if (TF_VERIFY(pvAttr.Get(&t, UsdTimeCode::Default()))) {
                primvarAttr = gprim.GetPrimvar(t);
                if (TF_VERIFY(primvarAttr.ComputeFlattened(&v, time))) {
                    primvar.name = t; // does not include primvars:
                    primvar.interpolation = primvarAttr.GetInterpolation();
                    valueCache->GetPrimvar(cachePath, t) = v;
                   _MergePrimvar(primvar, &valueCache->GetPrimvars(cachePath));
                }
            }
        }
    }