Blender  V3.3
usd_writer_camera.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2019 Blender Foundation. All rights reserved. */
3 #include "usd_writer_camera.h"
5 
6 #include <pxr/usd/usdGeom/camera.h>
7 #include <pxr/usd/usdGeom/tokens.h>
8 
9 #include "BKE_camera.h"
10 #include "BLI_assert.h"
11 
12 #include "DNA_camera_types.h"
13 #include "DNA_scene_types.h"
14 
15 namespace blender::io::usd {
16 
18 {
19 }
20 
22 {
23  Camera *camera = static_cast<Camera *>(context->object->data);
24  return camera->type == CAM_PERSP;
25 }
26 
28  const struct RenderData *rd,
29  float *r_sensor_x,
30  float *r_sensor_y)
31 {
32  /* Compute the final image size in pixels. */
33  float sizex = rd->xsch * rd->xasp;
34  float sizey = rd->ysch * rd->yasp;
35 
36  int sensor_fit = BKE_camera_sensor_fit(camera->sensor_fit, sizex, sizey);
37 
38  switch (sensor_fit) {
40  *r_sensor_x = camera->sensor_x;
41  *r_sensor_y = camera->sensor_x * sizey / sizex;
42  break;
44  *r_sensor_x = camera->sensor_y * sizex / sizey;
45  *r_sensor_y = camera->sensor_y;
46  break;
48  BLI_assert_msg(0, "Camera fit should be either horizontal or vertical");
49  break;
50  }
51 }
52 
54 {
55  pxr::UsdTimeCode timecode = get_export_time_code();
56  pxr::UsdGeomCamera usd_camera = pxr::UsdGeomCamera::Define(usd_export_context_.stage,
58 
59  Camera *camera = static_cast<Camera *>(context.object->data);
61 
62  usd_camera.CreateProjectionAttr().Set(pxr::UsdGeomTokens->perspective);
63 
64  /* USD stores the focal length in "millimeters or tenths of world units", because at some point
65  * they decided world units might be centimeters. Quite confusing, as the USD Viewer shows the
66  * correct FoV when we write millimeters and not "tenths of world units".
67  */
68  usd_camera.CreateFocalLengthAttr().Set(camera->lens, timecode);
69 
70  float aperture_x, aperture_y;
71  camera_sensor_size_for_render(camera, &scene->r, &aperture_x, &aperture_y);
72 
73  float film_aspect = aperture_x / aperture_y;
74  usd_camera.CreateHorizontalApertureAttr().Set(aperture_x, timecode);
75  usd_camera.CreateVerticalApertureAttr().Set(aperture_y, timecode);
76  usd_camera.CreateHorizontalApertureOffsetAttr().Set(aperture_x * camera->shiftx, timecode);
77  usd_camera.CreateVerticalApertureOffsetAttr().Set(aperture_y * camera->shifty * film_aspect,
78  timecode);
79 
80  usd_camera.CreateClippingRangeAttr().Set(
81  pxr::VtValue(pxr::GfVec2f(camera->clip_start, camera->clip_end)), timecode);
82 
83  /* Write DoF-related attributes. */
84  if (camera->dof.flag & CAM_DOF_ENABLED) {
85  usd_camera.CreateFStopAttr().Set(camera->dof.aperture_fstop, timecode);
86 
87  float focus_distance = scene->unit.scale_length *
89  usd_camera.CreateFocusDistanceAttr().Set(focus_distance, timecode);
90  }
91 }
92 
93 } // namespace blender::io::usd
Camera data-block and utility functions.
float BKE_camera_object_dof_distance(const struct Object *ob)
int BKE_camera_sensor_fit(int sensor_fit, float sizex, float sizey)
Definition: camera.c:246
#define BLI_assert_msg(a, msg)
Definition: BLI_assert.h:53
struct Scene * DEG_get_evaluated_scene(const struct Depsgraph *graph)
@ CAM_DOF_ENABLED
@ CAMERA_SENSOR_FIT_HOR
@ CAMERA_SENSOR_FIT_AUTO
@ CAMERA_SENSOR_FIT_VERT
@ CAM_PERSP
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Sky Generate a procedural sky texture Noise Generate fractal Perlin noise Wave Generate procedural bands or rings with noise Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a or normal between camera
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)
static const pxr::TfToken perspective("perspective", pxr::TfToken::Immortal)
struct RenderData r
struct UnitSettings unit