Blender V4.3
usd_reader_camera.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Tangent Animation. All rights reserved.
2 * SPDX-FileCopyrightText: 2023 Blender Authors
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 *
6 * Adapted from the Blender Alembic importer implementation. */
7
9
10#include "DNA_camera_types.h"
11#include "DNA_object_types.h"
12
13#include "BLI_math_base.h"
14
15#include "BKE_camera.h"
16#include "BKE_object.hh"
17
18#include <pxr/usd/usdGeom/camera.h>
19
20namespace blender::io::usd {
21
22void USDCameraReader::create_object(Main *bmain, const double /*motionSampleTime*/)
23{
24 Camera *bcam = static_cast<Camera *>(BKE_camera_add(bmain, name_.c_str()));
25
27 object_->data = bcam;
28}
29
30void USDCameraReader::read_object_data(Main *bmain, const double motionSampleTime)
31{
32 Camera *bcam = (Camera *)object_->data;
33
34 pxr::UsdGeomCamera cam_prim(prim_);
35
36 if (!cam_prim) {
37 return;
38 }
39
40 pxr::VtValue val;
41 cam_prim.GetFocalLengthAttr().Get(&val, motionSampleTime);
42 pxr::VtValue verApOffset;
43 cam_prim.GetVerticalApertureOffsetAttr().Get(&verApOffset, motionSampleTime);
44 pxr::VtValue horApOffset;
45 cam_prim.GetHorizontalApertureOffsetAttr().Get(&horApOffset, motionSampleTime);
46 pxr::VtValue clippingRangeVal;
47 cam_prim.GetClippingRangeAttr().Get(&clippingRangeVal, motionSampleTime);
48 pxr::VtValue focalDistanceVal;
49 cam_prim.GetFocusDistanceAttr().Get(&focalDistanceVal, motionSampleTime);
50 pxr::VtValue fstopVal;
51 cam_prim.GetFStopAttr().Get(&fstopVal, motionSampleTime);
52 pxr::VtValue projectionVal;
53 cam_prim.GetProjectionAttr().Get(&projectionVal, motionSampleTime);
54 pxr::VtValue verAp;
55 cam_prim.GetVerticalApertureAttr().Get(&verAp, motionSampleTime);
56 pxr::VtValue horAp;
57 cam_prim.GetHorizontalApertureAttr().Get(&horAp, motionSampleTime);
58
59 /*
60 * For USD, these camera properties are in tenths of a world unit.
61 * https://graphics.pixar.com/usd/release/api/class_usd_geom_camera.html#UsdGeom_CameraUnits
62 *
63 * tenth_unit_to_meters = stage_meters_per_unit / 10
64 * tenth_unit_to_millimeters = 1000 * unit_to_tenth_unit
65 * = 100 * stage_meters_per_unit
66 */
67 const double tenth_unit_to_millimeters = 100.0 * settings_->stage_meters_per_unit;
68 bcam->lens = val.Get<float>() * tenth_unit_to_millimeters;
69 bcam->sensor_x = horAp.Get<float>() * tenth_unit_to_millimeters;
70 bcam->sensor_y = verAp.Get<float>() * tenth_unit_to_millimeters;
71
72 bcam->sensor_fit = bcam->sensor_x >= bcam->sensor_y ? CAMERA_SENSOR_FIT_HOR :
74
75 float sensor_size = bcam->sensor_x >= bcam->sensor_y ? bcam->sensor_x : bcam->sensor_y;
76 bcam->shiftx = (horApOffset.Get<float>() * tenth_unit_to_millimeters) / sensor_size;
77 bcam->shifty = (verApOffset.Get<float>() * tenth_unit_to_millimeters) / sensor_size;
78
79 bcam->type = (projectionVal.Get<pxr::TfToken>().GetString() == "perspective") ? CAM_PERSP :
81
82 /* Call UncheckedGet() to silence compiler warnings.
83 * Clamp to 1e-6 matching range defined in RNA. */
84 bcam->clip_start = max_ff(1e-6f, clippingRangeVal.UncheckedGet<pxr::GfVec2f>()[0]);
85 bcam->clip_end = clippingRangeVal.UncheckedGet<pxr::GfVec2f>()[1];
86
87 bcam->dof.focus_distance = focalDistanceVal.Get<float>();
88 bcam->dof.aperture_fstop = float(fstopVal.Get<float>());
89
90 if (bcam->type == CAM_ORTHO) {
91 bcam->ortho_scale = max_ff(verAp.Get<float>(), horAp.Get<float>());
92 }
93
94 USDXformReader::read_object_data(bmain, motionSampleTime);
95}
96
97} // namespace blender::io::usd
Camera data-block and utility functions.
void * BKE_camera_add(struct Main *bmain, const char *name)
General operations, lookup, etc. for blender objects.
Object * BKE_object_add_only_object(Main *bmain, int type, const char *name) ATTR_RETURNS_NONNULL
MINLINE float max_ff(float a, float b)
@ CAMERA_SENSOR_FIT_HOR
@ CAMERA_SENSOR_FIT_VERT
@ 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
const ImportSettings * settings_
void read_object_data(Main *bmain, double motionSampleTime) override
draw_view in_light_buf[] float
float clip_end
char sensor_fit
float sensor_y
float sensor_x
float clip_start
struct CameraDOFSettings dof
float ortho_scale