Blender V5.3
abc_reader_camera.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "abc_reader_camera.h"
10#include "abc_keyframing.h"
11#include "abc_util.h"
12
13/* Silence warnings from copying deprecated fields. */
14#define DNA_DEPRECATED_ALLOW
15
16#include "DNA_camera_types.h"
17#include "DNA_object_types.h"
18
19#include "ANIM_action.hh"
20#include "ANIM_fcurve.hh"
21
22#include "BLI_math_base_c.hh"
23
24#include "BKE_camera.h"
25#include "BKE_object.hh"
26
27#include "BLT_translation.hh"
28
29namespace blender {
30
31using Alembic::AbcGeom::CameraSample;
32using Alembic::AbcGeom::ICamera;
33using Alembic::AbcGeom::ICompoundProperty;
34using Alembic::AbcGeom::IFloatProperty;
35using Alembic::AbcGeom::ISampleSelector;
36using Alembic::AbcGeom::kWrapExisting;
37
38namespace io::alembic {
39
41{
42 ICamera abc_cam(m_iobject, kWrapExisting);
43 m_schema = abc_cam.getSchema();
44}
45
47{
48 return m_schema.valid();
49}
50
52 const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
53 const Object *const ob,
54 const char **r_err_str) const
55{
56 if (!Alembic::AbcGeom::ICamera::matches(alembic_header)) {
57 *r_err_str = RPT_(
58 "Object type mismatch, Alembic object path pointed to Camera when importing, but not any "
59 "more");
60 return false;
61 }
62
63 if (ob->type != OB_CAMERA) {
64 *r_err_str = RPT_("Object type mismatch, Alembic object path points to Camera");
65 return false;
66 }
67
68 return true;
69}
70
71static void read_camera_sample(Camera *bcam,
72 const ICamera::schema_type &schema,
73 const ISampleSelector &sample_sel)
74{
75 CameraSample cam_sample;
76 schema.get(cam_sample, sample_sel);
77
78 ICompoundProperty customDataContainer = schema.getUserProperties();
79
80 if (customDataContainer.valid() && customDataContainer.getPropertyHeader("stereoDistance") &&
81 customDataContainer.getPropertyHeader("eyeSeparation"))
82 {
83 IFloatProperty convergence_plane(customDataContainer, "stereoDistance");
84 IFloatProperty eye_separation(customDataContainer, "eyeSeparation");
85
86 bcam->stereo.interocular_distance = eye_separation.getValue(sample_sel);
87 bcam->stereo.convergence_distance = convergence_plane.getValue(sample_sel);
88 }
89
90 const float lens = float(cam_sample.getFocalLength());
91 const float apperture_x = float(cam_sample.getHorizontalAperture());
92 const float apperture_y = float(cam_sample.getVerticalAperture());
93 const float h_film_offset = float(cam_sample.getHorizontalFilmOffset());
94 const float v_film_offset = float(cam_sample.getVerticalFilmOffset());
95 const float film_aspect = apperture_x / apperture_y;
96
97 bcam->lens = lens;
98 bcam->sensor_x = apperture_x * 10;
99 bcam->sensor_y = apperture_y * 10;
100 bcam->shiftx = h_film_offset / apperture_x;
101 bcam->shifty = v_film_offset / apperture_y / film_aspect;
102 bcam->clip_start = max_ff(0.1f, float(cam_sample.getNearClippingPlane()));
103 bcam->clip_end = float(cam_sample.getFarClippingPlane());
104 bcam->dof.focus_distance = float(cam_sample.getFocusDistance());
105 bcam->dof.aperture_fstop = float(cam_sample.getFStop());
106}
107
108void AbcCameraReader::readObjectData(Main *bmain, const ISampleSelector &sample_sel)
109{
110 Camera *bcam = BKE_camera_add(bmain, m_data_name.c_str());
111 read_camera_sample(bcam, m_schema, sample_sel);
113 m_object->data = id_cast<ID *>(bcam);
114}
115
116/* The macro that needs to be passed should have arguments :
117 * (short_name, rna_path, member_accessor) */
118#define ENUMERATE_CAMERA_PROPERTIES(X) \
119 X(lens, lens, lens) \
120 X(sensor_width, sensor_width, sensor_x) \
121 X(sensor_height, sensor_height, sensor_y) \
122 X(clip_start, clip_start, clip_start) \
123 X(clip_end, clip_end, clip_end) \
124 X(shift_x, shift_x, shiftx) \
125 X(shift_y, shift_y, shifty) \
126 X(focus_distance, dof.focus_distance, dof.focus_distance) \
127 X(aperture_fstop, dof.aperture_fstop, dof.aperture_fstop) \
128 X(interocular_distance, stereo.interocular_distance, stereo.interocular_distance) \
129 X(convergence_distance, stereo.convergence_distance, stereo.convergence_distance)
130
132 Camera *camera_ = nullptr;
133 const Alembic::AbcGeom::ICameraSchema &schema_{};
134
135 /* Keep track of what has been modified to remove unnecessary fcurves at the end as Alembic
136 * seemingly does not have per property information. */
137 struct MemberModified {
138#define DECLARE_MEMBER(short_name, rna_path, member_accessor) bool short_name = false;
140#undef DECLARE_MEMBER
141 };
142
143 MemberModified member_modified_{};
144
145#define DECLARE_FCURVES(short_name, rna_path, member_accessor) \
146 FCurve *short_name##_fcurve = nullptr;
148#undef DECLARE_FCURVES
149
150 public:
151 CameraFCurveCreationHelper(Camera *camera, const Alembic::AbcGeom::ICameraSchema &schema)
152 : FCurveCreationHelper(&camera->id), camera_(camera), schema_(schema)
153 {
154 }
155
156 void create_fcurves(const int sample_count) override
157 {
158#define CREATE_FCURVE(short_name, rna_path, member_accessor) \
159 short_name##_fcurve = create_fcurve({#rna_path, 0}, sample_count);
161#undef CREATE_FCURVE
162 }
163
164 void set_fcurves_sample(const FrameSampleInfo &sample_info) override
165 {
166 /* To detect what has been modified. */
167 Camera last_camera = *camera_;
168 read_camera_sample(camera_, schema_, sample_info.selector);
169
170#define SET_FCURVE_SAMPLE(short_name, rna_path, member_accessor) \
171 set_fcurve_sample(short_name##_fcurve, \
172 sample_info.sample_index, \
173 sample_info.frame, \
174 camera_->member_accessor); \
175 member_modified_.short_name |= last_camera.member_accessor != camera_->member_accessor;
177#undef SET_FCURVE_SAMPLE
178 }
179
181 {
182#define REMOVE_UNNECESSARY_FCURVE(short_name, rna_path, member_accessor) \
183 if (member_modified_.short_name == false) { \
184 channelbag->fcurve_remove(*short_name##_fcurve); \
185 }
187#undef REMOVE_UNNECESSARY_FCURVE
188 }
189};
190
191std::unique_ptr<FCurveCreationHelper> AbcCameraReader::getKeyFramingHelper()
192{
193 if (m_schema.isConstant()) {
194 return nullptr;
195 }
196
197 Camera *camera = id_cast<Camera *>(m_object->data);
198 return std::make_unique<CameraFCurveCreationHelper>(camera, m_schema);
199}
200
201} // namespace io::alembic
202} // namespace blender
Functions and classes to work with Actions.
Functions to modify FCurves.
Camera data-block and utility functions.
General operations, lookup, etc. for blender objects.
#define RPT_(msgid)
Object is a sort of wrapper for general info.
#define REMOVE_UNNECESSARY_FCURVE(short_name, rna_path, member_accessor)
#define DECLARE_MEMBER(short_name, rna_path, member_accessor)
#define SET_FCURVE_SAMPLE(short_name, rna_path, member_accessor)
#define DECLARE_FCURVES(short_name, rna_path, member_accessor)
#define CREATE_FCURVE(short_name, rna_path, member_accessor)
#define ENUMERATE_CAMERA_PROPERTIES(X)
bool accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header, const Object *const ob, const char **r_err_str) const override
AbcCameraReader(const AbcReaderConstructorArgs &args)
std::unique_ptr< FCurveCreationHelper > getKeyFramingHelper() override
void readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel) override
AbcObjectReader(const AbcReaderConstructorArgs &args)
void set_fcurves_sample(const FrameSampleInfo &sample_info) override
void create_fcurves(const int sample_count) override
CameraFCurveCreationHelper(Camera *camera, const Alembic::AbcGeom::ICameraSchema &schema)
void camera(KernelGlobals &kg, const ShadingData &sd, float3 &outview, float &outdepth, float &outdist)
static void read_camera_sample(Camera *bcam, const ICamera::schema_type &schema, const ISampleSelector &sample_sel)
BMesh Iterators.
struct Camera * BKE_camera_add(struct Main *bmain, const char *name)
MINLINE float max_ff(float a, float b)
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
Alembic::Abc::ISampleSelector selector