1 //
2 // Copyright 2019 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 //    names, trademarks, service marks, or product names of the Licensor
11 //    and its affiliates, except as required to comply with Section 4(c) of
12 //    the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 //     http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #include "pxr/pxr.h"
25 #include "pxr/usdImaging/usdAppUtils/camera.h"
26 
27 #include "pxr/base/tf/diagnostic.h"
28 #include "pxr/usd/sdf/path.h"
29 #include "pxr/usd/usd/prim.h"
30 #include "pxr/usd/usd/primFlags.h"
31 #include "pxr/usd/usd/primRange.h"
32 #include "pxr/usd/usd/stage.h"
33 #include "pxr/usd/usdGeom/camera.h"
34 
35 
36 PXR_NAMESPACE_OPEN_SCOPE
37 
38 
39 UsdGeomCamera
UsdAppUtilsGetCameraAtPath(const UsdStagePtr & stage,const SdfPath & cameraPath)40 UsdAppUtilsGetCameraAtPath(
41         const UsdStagePtr& stage,
42         const SdfPath& cameraPath)
43 {
44     if (!stage) {
45         TF_CODING_ERROR("Invalid stage");
46         return UsdGeomCamera();
47     }
48 
49     if (!cameraPath.IsPrimPath()) {
50         // A non-prim path cannot be a camera.
51         return UsdGeomCamera();
52     }
53 
54     SdfPath usdCameraPath = cameraPath;
55 
56     if (!cameraPath.IsAbsolutePath()) {
57         if (cameraPath.GetPathElementCount() > 1u) {
58             // XXX: Perhaps we should error here? For now we coerce the camera
59             // path to be absolute using the absolute root path and print a
60             // warning.
61             usdCameraPath =
62                 cameraPath.MakeAbsolutePath(SdfPath::AbsoluteRootPath());
63             TF_WARN(
64                 "Camera path \"%s\" is not absolute. Using absolute path "
65                 "instead: \"%s\"",
66                 cameraPath.GetText(),
67                 usdCameraPath.GetText());
68         } else {
69             // Search for the camera by name.
70             UsdPrimRange primRange =
71                 UsdPrimRange::Stage(stage, UsdTraverseInstanceProxies());
72 
73             for (const UsdPrim& usdPrim : primRange) {
74                 if (usdPrim.GetName() == cameraPath.GetNameToken()) {
75                     const UsdGeomCamera usdCamera(usdPrim);
76                     if (usdCamera) {
77                         return usdCamera;
78                     }
79                 }
80             }
81         }
82     }
83 
84     return UsdGeomCamera::Get(stage, usdCameraPath);
85 }
86 
87 
88 PXR_NAMESPACE_CLOSE_SCOPE
89