Blender V4.3
file_reader.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "hydra/file_reader.h"
6#include "hydra/camera.h"
8
9#include "util/path.h"
10#include "util/unique_ptr.h"
11
12#include "scene/scene.h"
13
14#include <pxr/base/plug/registry.h>
15#include <pxr/imaging/hd/dirtyList.h>
16#include <pxr/imaging/hd/renderDelegate.h>
17#include <pxr/imaging/hd/renderIndex.h>
18#include <pxr/imaging/hd/rprimCollection.h>
19#include <pxr/imaging/hd/task.h>
20#include <pxr/usd/usd/stage.h>
21#include <pxr/usd/usdGeom/camera.h>
22#include <pxr/usd/usdGeom/metrics.h>
23#include <pxr/usdImaging/usdImaging/delegate.h>
24
26
27/* Dummy task whose only purpose is to provide render tag tokens to the render index. */
28class DummyHdTask : public HdTask {
29 public:
30 DummyHdTask(HdSceneDelegate *delegate, SdfPath const &id)
31 : HdTask(id), tags({HdRenderTagTokens->geometry, HdRenderTagTokens->render})
32 {
33 }
34
35 protected:
36 void Sync(HdSceneDelegate *delegate, HdTaskContext *ctx, HdDirtyBits *dirtyBits) override {}
37
38 void Prepare(HdTaskContext *ctx, HdRenderIndex *render_index) override {}
39
40 void Execute(HdTaskContext *ctx) override {}
41
42 const TfTokenVector &GetRenderTags() const override
43 {
44 return tags;
45 }
46
47 TfTokenVector tags;
48};
49
50void HdCyclesFileReader::read(Session *session, const char *filepath, const bool use_camera)
51{
52 /* Initialize USD. */
53 PlugRegistry::GetInstance().RegisterPlugins(path_get("usd"));
54
55 /* Open Stage. */
56 UsdStageRefPtr stage = UsdStage::Open(filepath);
57 if (!stage) {
58 fprintf(stderr, "%s read error\n", filepath);
59 return;
60 }
61
62 /* Init paths. */
63 SdfPath root_path = SdfPath::AbsoluteRootPath();
64 SdfPath task_path("/_hdCycles/DummyHdTask");
65
66 /* Create render delegate. */
67 HdRenderSettingsMap settings_map;
68 settings_map.insert(std::make_pair(HdCyclesRenderSettingsTokens->stageMetersPerUnit,
69 VtValue(UsdGeomGetStageMetersPerUnit(stage))));
70
71 HdCyclesDelegate render_delegate(settings_map, session, true);
72
73 /* Create render index and scene delegate. */
74 unique_ptr<HdRenderIndex> render_index(HdRenderIndex::New(&render_delegate, {}));
75 unique_ptr<UsdImagingDelegate> scene_delegate = make_unique<UsdImagingDelegate>(
76 render_index.get(), root_path);
77
78 /* Add render tags and collection to render index. */
79 HdRprimCollection collection(HdTokens->geometry, HdReprSelector(HdReprTokens->smoothHull));
80 collection.SetRootPath(root_path);
81
82 render_index->InsertTask<DummyHdTask>(scene_delegate.get(), task_path);
83
84#if PXR_VERSION < 2111
85 HdDirtyListSharedPtr dirty_list = std::make_shared<HdDirtyList>(collection,
86 *(render_index.get()));
87 render_index->EnqueuePrimsToSync(dirty_list, collection);
88#else
89 render_index->EnqueueCollectionToSync(collection);
90#endif
91
92 /* Create prims. */
93 const UsdPrim &stage_root = stage->GetPseudoRoot();
94 scene_delegate->Populate(stage_root.GetStage()->GetPrimAtPath(root_path), {});
95
96 /* Sync prims. */
97 HdTaskContext task_context;
98 HdTaskSharedPtrVector tasks;
99 tasks.push_back(render_index->GetTask(task_path));
100
101 render_index->SyncAll(&tasks, &task_context);
102 render_delegate.CommitResources(&render_index->GetChangeTracker());
103
104 /* Use first camera in stage.
105 * TODO: get camera from UsdRender if available. */
106 if (use_camera) {
107 for (UsdPrim const &prim : stage->Traverse()) {
108 if (prim.IsA<UsdGeomCamera>()) {
109 HdSprim *sprim = render_index->GetSprim(HdPrimTypeTokens->camera, prim.GetPath());
110 if (sprim) {
111 HdCyclesCamera *camera = dynamic_cast<HdCyclesCamera *>(sprim);
112 camera->ApplyCameraSettings(render_delegate.GetRenderParam(), session->scene->camera);
113 break;
114 }
115 }
116 }
117 }
118}
119
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 Gabor Generate Gabor noise Gradient Generate interpolated color and intensity values based on the input vector Magic Generate a psychedelic color texture 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
const TfTokenVector & GetRenderTags() const override
void Sync(HdSceneDelegate *delegate, HdTaskContext *ctx, HdDirtyBits *dirtyBits) override
void Execute(HdTaskContext *ctx) override
void Prepare(HdTaskContext *ctx, HdRenderIndex *render_index) override
TfTokenVector tags
DummyHdTask(HdSceneDelegate *delegate, SdfPath const &id)
PXR_NS::HdRenderParam * GetRenderParam() const override
void CommitResources(PXR_NS::HdChangeTracker *tracker) override
static void read(Session *session, const char *filepath, const bool use_camera=true)
Scene * scene
#define HDCYCLES_NAMESPACE_CLOSE_SCOPE
string path_get(const string &sub)
Definition path.cpp:339
struct Object * camera