Blender V5.3
camera_delegate.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "camera_delegate.hh"
6
7#include "DNA_ID.h"
8#include "DNA_camera_types.h"
9#include "DNA_object_types.h"
10
11#include "BKE_idprop.hh"
12
14
15#include <pxr/imaging/hd/dataSource.h>
16#include <pxr/imaging/hd/dataSourceLocator.h>
17#include <pxr/imaging/hd/overlayContainerDataSource.h>
18#include <pxr/imaging/hd/retainedDataSource.h>
19#include <pxr/imaging/hd/tokens.h>
20
22
23class BlenderCameraIDPropertiesDataSource : public pxr::HdContainerDataSource {
24 public:
26
28 {
29 camera_ = camera;
30 }
31
32 pxr::TfTokenVector GetNames() override
33 {
34 pxr::TfTokenVector result;
35 if (camera_ && camera_->id.properties) {
36 for (const IDProperty &prop : camera_->id.properties->data.group) {
37 result.emplace_back(prop.name);
38 }
39 }
40 return result;
41 }
42
43 pxr::HdDataSourceBaseHandle Get(const pxr::TfToken &name) override
44 {
45 if (!camera_ || !camera_->id.properties) {
46 return nullptr;
47 }
48 const IDProperty *prop = IDP_GetPropertyFromGroup(camera_->id.properties, name.GetText());
49 if (!prop) {
50 return nullptr;
51 }
52 switch (prop->type) {
53 case IDP_INT:
54 return pxr::HdRetainedTypedSampledDataSource<int>::New(IDP_int_get(prop));
55 case IDP_FLOAT:
56 return pxr::HdRetainedTypedSampledDataSource<float>::New(IDP_float_get(prop));
57 case IDP_DOUBLE:
58 return pxr::HdRetainedTypedSampledDataSource<double>::New(IDP_double_get(prop));
59 case IDP_BOOLEAN:
60 return pxr::HdRetainedTypedSampledDataSource<bool>::New(bool(IDP_bool_get(prop)));
61 default:
62 return nullptr;
63 }
64 }
65
66 private:
67 const Camera *camera_ = nullptr;
68};
69
70CameraDelegate::CameraDelegate(pxr::HdRenderIndex *render_index, pxr::SdfPath const &camera_id)
71 : camera_scene_index_(pxr::HdRetainedSceneIndex::New()),
72 camera_id_(camera_id),
73 free_camera_ds_(pxr::HdxFreeCameraPrimDataSource::New()),
74 id_properties_ds_(BlenderCameraIDPropertiesDataSource::New())
75{
76 pxr::HdContainerDataSourceHandle camera_ds = pxr::HdOverlayContainerDataSource::New(
77 id_properties_ds_, free_camera_ds_);
78
79 camera_scene_index_->AddPrims({{camera_id_, pxr::HdPrimTypeTokens->camera, camera_ds}});
80
81 render_index->InsertSceneIndex(
82 camera_scene_index_, pxr::SdfPath::AbsoluteRootPath(), /*needsPrefixing=*/false);
83}
84
85void CameraDelegate::sync(const Object *camera_object)
86{
87 const Camera *camera = (camera_object && camera_object->type == OB_CAMERA) ?
88 id_cast<const Camera *>(camera_object->data) :
89 nullptr;
90 id_properties_ds_->set_camera(camera);
91 camera_scene_index_->DirtyPrims({{camera_id_, pxr::HdDataSourceLocator::EmptyLocator()}});
92}
93
94void CameraDelegate::SetCamera(pxr::GfCamera const &camera)
95{
96 pxr::HdDataSourceLocatorSet dirty_locators;
97 free_camera_ds_->SetCamera(camera, &dirty_locators);
98 if (!dirty_locators.IsEmpty()) {
99 camera_scene_index_->DirtyPrims({{camera_id_, dirty_locators}});
100 }
101}
102
103} // namespace blender::io::hydra
#define IDP_float_get(prop)
#define IDP_int_get(prop)
#define IDP_double_get(prop)
#define IDP_bool_get(prop)
ID and Library types, which are fundamental for SDNA.
Object is a sort of wrapper for general info.
pxr::HdDataSourceBaseHandle Get(const pxr::TfToken &name) override
HD_DECLARE_DATASOURCE(BlenderCameraIDPropertiesDataSource)
void SetCamera(pxr::GfCamera const &camera)
CameraDelegate(pxr::HdRenderIndex *render_index, pxr::SdfPath const &camera_id)
void sync(const Object *camera_object)
void camera(KernelGlobals &kg, const ShadingData &sd, float3 &outview, float &outdepth, float &outdist)
IDProperty * IDP_GetPropertyFromGroup(const IDProperty *prop, StringRef name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:818
ListBaseT< struct IDProperty > group
Definition DNA_ID.h:133
eIDPropertyType type
Definition DNA_ID.h:145
IDPropertyData data
Definition DNA_ID.h:157