Blender  V2.93
usd_writer_camera.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2019 Blender Foundation.
17  * All rights reserved.
18  */
19 #include "usd_writer_camera.h"
20 #include "usd_hierarchy_iterator.h"
21 
22 #include <pxr/usd/usdGeom/camera.h>
23 #include <pxr/usd/usdGeom/tokens.h>
24 
25 #include "BKE_camera.h"
26 #include "BLI_assert.h"
27 
28 #include "DNA_camera_types.h"
29 #include "DNA_scene_types.h"
30 
31 namespace blender::io::usd {
32 
34 {
35 }
36 
38 {
39  Camera *camera = static_cast<Camera *>(context->object->data);
40  return camera->type == CAM_PERSP;
41 }
42 
43 static void camera_sensor_size_for_render(const Camera *camera,
44  const struct RenderData *rd,
45  float *r_sensor_x,
46  float *r_sensor_y)
47 {
48  /* Compute the final image size in pixels. */
49  float sizex = rd->xsch * rd->xasp;
50  float sizey = rd->ysch * rd->yasp;
51 
52  int sensor_fit = BKE_camera_sensor_fit(camera->sensor_fit, sizex, sizey);
53 
54  switch (sensor_fit) {
56  *r_sensor_x = camera->sensor_x;
57  *r_sensor_y = camera->sensor_x * sizey / sizex;
58  break;
60  *r_sensor_x = camera->sensor_y * sizex / sizey;
61  *r_sensor_y = camera->sensor_y;
62  break;
64  BLI_assert(!"Camera fit should be either horizontal or vertical");
65  break;
66  }
67 }
68 
70 {
71  pxr::UsdTimeCode timecode = get_export_time_code();
72  pxr::UsdGeomCamera usd_camera = pxr::UsdGeomCamera::Define(usd_export_context_.stage,
74 
75  Camera *camera = static_cast<Camera *>(context.object->data);
77 
78  usd_camera.CreateProjectionAttr().Set(pxr::UsdGeomTokens->perspective);
79 
80  /* USD stores the focal length in "millimeters or tenths of world units", because at some point
81  * they decided world units might be centimeters. Quite confusing, as the USD Viewer shows the
82  * correct FoV when we write millimeters and not "tenths of world units".
83  */
84  usd_camera.CreateFocalLengthAttr().Set(camera->lens, timecode);
85 
86  float aperture_x, aperture_y;
87  camera_sensor_size_for_render(camera, &scene->r, &aperture_x, &aperture_y);
88 
89  float film_aspect = aperture_x / aperture_y;
90  usd_camera.CreateHorizontalApertureAttr().Set(aperture_x, timecode);
91  usd_camera.CreateVerticalApertureAttr().Set(aperture_y, timecode);
92  usd_camera.CreateHorizontalApertureOffsetAttr().Set(aperture_x * camera->shiftx, timecode);
93  usd_camera.CreateVerticalApertureOffsetAttr().Set(aperture_y * camera->shifty * film_aspect,
94  timecode);
95 
96  usd_camera.CreateClippingRangeAttr().Set(
97  pxr::VtValue(pxr::GfVec2f(camera->clip_start, camera->clip_end)), timecode);
98 
99  /* Write DoF-related attributes. */
100  if (camera->dof.flag & CAM_DOF_ENABLED) {
101  usd_camera.CreateFStopAttr().Set(camera->dof.aperture_fstop, timecode);
102 
103  float focus_distance = scene->unit.scale_length *
105  usd_camera.CreateFocusDistanceAttr().Set(focus_distance, timecode);
106  }
107 }
108 
109 } // namespace blender::io::usd
Camera data-block and utility functions.
float BKE_camera_object_dof_distance(struct Object *ob)
Definition: camera.c:227
int BKE_camera_sensor_fit(int sensor_fit, float sizex, float sizey)
Definition: camera.c:252
#define BLI_assert(a)
Definition: BLI_assert.h:58
struct Scene * DEG_get_evaluated_scene(const struct Depsgraph *graph)
@ CAM_DOF_ENABLED
@ CAM_PERSP
@ CAMERA_SENSOR_FIT_HOR
@ CAMERA_SENSOR_FIT_AUTO
@ CAMERA_SENSOR_FIT_VERT
pxr::UsdTimeCode get_export_time_code() const
const USDExporterContext usd_export_context_
USDCameraWriter(const USDExporterContext &ctx)
virtual bool is_supported(const HierarchyContext *context) const override
virtual void do_write(HierarchyContext &context) override
Scene scene
static void camera_sensor_size_for_render(const Camera *camera, const struct RenderData *rd, float *r_sensor_x, float *r_sensor_y)
struct SELECTID_Context context
Definition: select_engine.c:47
float clip_end
char sensor_fit
float sensor_y
float shiftx
float sensor_x
float clip_start
float shifty
struct CameraDOFSettings dof
struct RenderData r
struct UnitSettings unit