Blender  V3.3
usd_reader_camera.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Adapted from the Blender Alembic importer implementation.
3  * Modifications Copyright 2021 Tangent Animation. All rights reserved. */
4 
5 #include "usd_reader_camera.h"
6 
7 #include "DNA_camera_types.h"
8 #include "DNA_object_types.h"
9 
10 #include "BKE_camera.h"
11 #include "BKE_object.h"
12 
13 #include "BLI_math.h"
14 
15 #include <pxr/pxr.h>
16 #include <pxr/usd/usdGeom/camera.h>
17 
18 namespace blender::io::usd {
19 
20 void USDCameraReader::create_object(Main *bmain, const double /* motionSampleTime */)
21 {
22  Camera *bcam = static_cast<Camera *>(BKE_camera_add(bmain, name_.c_str()));
23 
25  object_->data = bcam;
26 }
27 
28 void USDCameraReader::read_object_data(Main *bmain, const double motionSampleTime)
29 {
30  Camera *bcam = (Camera *)object_->data;
31 
32  pxr::UsdGeomCamera cam_prim(prim_);
33 
34  if (!cam_prim) {
35  return;
36  }
37 
38  pxr::VtValue val;
39  cam_prim.GetFocalLengthAttr().Get(&val, motionSampleTime);
40  pxr::VtValue verApOffset;
41  cam_prim.GetVerticalApertureOffsetAttr().Get(&verApOffset, motionSampleTime);
42  pxr::VtValue horApOffset;
43  cam_prim.GetHorizontalApertureOffsetAttr().Get(&horApOffset, motionSampleTime);
44  pxr::VtValue clippingRangeVal;
45  cam_prim.GetClippingRangeAttr().Get(&clippingRangeVal, motionSampleTime);
46  pxr::VtValue focalDistanceVal;
47  cam_prim.GetFocusDistanceAttr().Get(&focalDistanceVal, motionSampleTime);
48  pxr::VtValue fstopVal;
49  cam_prim.GetFStopAttr().Get(&fstopVal, motionSampleTime);
50  pxr::VtValue projectionVal;
51  cam_prim.GetProjectionAttr().Get(&projectionVal, motionSampleTime);
52  pxr::VtValue verAp;
53  cam_prim.GetVerticalApertureAttr().Get(&verAp, motionSampleTime);
54  pxr::VtValue horAp;
55  cam_prim.GetHorizontalApertureAttr().Get(&horAp, motionSampleTime);
56 
57  bcam->lens = val.Get<float>();
58  /* TODO(@makowalski): support sensor size. */
59 #if 0
60  bcam->sensor_x = 0.0f;
61  bcam->sensor_y = 0.0f;
62 #endif
63  bcam->shiftx = verApOffset.Get<float>();
64  bcam->shifty = horApOffset.Get<float>();
65 
66  bcam->type = (projectionVal.Get<pxr::TfToken>().GetString() == "perspective") ? CAM_PERSP :
67  CAM_ORTHO;
68 
69  /* Calling UncheckedGet() to silence compiler warnings. */
70  bcam->clip_start = max_ff(0.1f, clippingRangeVal.UncheckedGet<pxr::GfVec2f>()[0]);
71  bcam->clip_end = clippingRangeVal.UncheckedGet<pxr::GfVec2f>()[1];
72 
73  bcam->dof.focus_distance = focalDistanceVal.Get<float>();
74  bcam->dof.aperture_fstop = static_cast<float>(fstopVal.Get<float>());
75 
76  if (bcam->type == CAM_ORTHO) {
77  bcam->ortho_scale = max_ff(verAp.Get<float>(), horAp.Get<float>());
78  }
79 
80  USDXformReader::read_object_data(bmain, motionSampleTime);
81 }
82 
83 } // namespace blender::io::usd
Camera data-block and utility functions.
void * BKE_camera_add(struct Main *bmain, const char *name)
Definition: camera.c:203
General operations, lookup, etc. for blender objects.
struct Object * BKE_object_add_only_object(struct Main *bmain, int type, const char *name) ATTR_RETURNS_NONNULL
Definition: object.cc:2241
MINLINE float max_ff(float a, float b)
@ CAM_PERSP
@ CAM_ORTHO
Object is a sort of wrapper for general info.
@ OB_CAMERA
void create_object(Main *bmain, double motionSampleTime) override
void read_object_data(Main *bmain, double motionSampleTime) override
void read_object_data(Main *bmain, double motionSampleTime) override
float clip_end
float sensor_y
float lens
float shiftx
float sensor_x
float clip_start
float shifty
struct CameraDOFSettings dof
float ortho_scale
Definition: BKE_main.h:121
void * data