Blender V5.3
python.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "final_engine.hh"
6#include "preview_engine.hh"
7#include "viewport_engine.hh"
8
9#include <Python.h>
10
11#include "RE_engine.h"
12
13#include "../generic/py_capi_utils.hh"
14#include "bpy_rna.hh"
15
16#include "BKE_context.hh"
17
18#include "RNA_prototypes.hh"
19
20#include "hydra/image.hh"
21
22namespace blender {
23
24namespace render::hydra {
25
26template<typename T> T *pyrna_to_pointer(PyObject *pyobject, const StructRNA *rnatype)
27{
28 const PointerRNA *ptr = pyrna_struct_as_ptr_or_null(pyobject, rnatype);
29 return (ptr) ? static_cast<T *>(ptr->data) : nullptr;
30}
31
32static PyObject *engine_create_func(PyObject * /*self*/, PyObject *args)
33{
34 PyObject *pyengine;
35 char *engine_type, *render_delegate_id;
36 if (!PyArg_ParseTuple(args,
37 "O" /* `engine` */
38 "s" /* `engine_type` */
39 "s" /* `render_delegate_id` */
40 ":engine_create",
41 &pyengine,
42 &engine_type,
43 &render_delegate_id))
44 {
45 return nullptr;
46 }
47
48 RenderEngine *bl_engine = pyrna_to_pointer<RenderEngine>(pyengine, RNA_RenderEngine);
49
50 CLOG_DEBUG(LOG_HYDRA_RENDER, "Engine %s", engine_type);
51 Engine *engine = nullptr;
52 try {
53 if (STREQ(engine_type, "VIEWPORT")) {
54 engine = new ViewportEngine(bl_engine, render_delegate_id);
55 }
56 else if (STREQ(engine_type, "PREVIEW")) {
57 engine = new PreviewEngine(bl_engine, render_delegate_id);
58 }
59 else {
60 engine = new FinalEngine(bl_engine, render_delegate_id);
61 }
62 }
63 catch (std::runtime_error &e) {
64 CLOG_ERROR(LOG_HYDRA_RENDER, "%s", e.what());
65 }
66
67 CLOG_DEBUG(LOG_HYDRA_RENDER, "Engine %p", engine);
68 return PyLong_FromVoidPtr(engine);
69}
70
71static PyObject *engine_free_func(PyObject * /*self*/, PyObject *args)
72{
73 PyObject *pyengine;
74 if (!PyArg_ParseTuple(args,
75 "O" /* `engine` */
76 ":engine_free",
77 &pyengine))
78 {
79 return nullptr;
80 }
81
82 Engine *engine = static_cast<Engine *>(PyLong_AsVoidPtr(pyengine));
83 CLOG_DEBUG(LOG_HYDRA_RENDER, "Engine %p", engine);
84 delete engine;
85
86 Py_RETURN_NONE;
87}
88
89static PyObject *engine_update_func(PyObject * /*self*/, PyObject *args)
90{
91 PyObject *pyengine, *pydepsgraph, *pycontext;
92 if (!PyArg_ParseTuple(args,
93 "O" /* `engine` */
94 "O" /* `depsgraph` */
95 "O" /* `context` */
96 ":engine_update",
97 &pyengine,
98 &pydepsgraph,
99 &pycontext))
100 {
101 return nullptr;
102 }
103
104 Engine *engine = static_cast<Engine *>(PyLong_AsVoidPtr(pyengine));
105 Depsgraph *depsgraph = pyrna_to_pointer<Depsgraph>(pydepsgraph, RNA_Depsgraph);
106 bContext *context = pyrna_to_pointer<bContext>(pycontext, RNA_Context);
107
108 CLOG_DEBUG(LOG_HYDRA_RENDER, "Engine %p", engine);
109 engine->sync(depsgraph, context);
110
111 Py_RETURN_NONE;
112}
113
114static PyObject *engine_render_func(PyObject * /*self*/, PyObject *args)
115{
116 PyObject *pyengine;
117 if (!PyArg_ParseTuple(args,
118 "O" /* `engine` */
119 ":engine_render",
120 &pyengine))
121 {
122 return nullptr;
123 }
124
125 Engine *engine = static_cast<Engine *>(PyLong_AsVoidPtr(pyengine));
126
127 CLOG_DEBUG(LOG_HYDRA_RENDER, "Engine %p", engine);
128
129 /* Allow Blender to execute other Python scripts. */
130 Py_BEGIN_ALLOW_THREADS;
131 engine->render();
132 Py_END_ALLOW_THREADS;
133
134 Py_RETURN_NONE;
135}
136
137static PyObject *engine_view_draw_func(PyObject * /*self*/, PyObject *args)
138{
139 PyObject *pyengine, *pycontext;
140 if (!PyArg_ParseTuple(args,
141 "O" /* `engine` */
142 "O" /* `context` */
143 ":engine_view_draw",
144 &pyengine,
145 &pycontext))
146 {
147 return nullptr;
148 }
149
150 ViewportEngine *engine = static_cast<ViewportEngine *>(PyLong_AsVoidPtr(pyengine));
151 bContext *context = pyrna_to_pointer<bContext>(pycontext, RNA_Context);
152
153 CLOG_DEBUG(LOG_HYDRA_RENDER, "Engine %p", engine);
154
155 /* Allow Blender to execute other Python scripts. */
156 Py_BEGIN_ALLOW_THREADS;
157 engine->render(context);
158 Py_END_ALLOW_THREADS;
159
160 Py_RETURN_NONE;
161}
162
163static pxr::VtValue get_setting_val(PyObject *pyval)
164{
165 pxr::VtValue val;
166 if (PyBool_Check(pyval)) {
167 val = Py_IsTrue(pyval);
168 }
169 else if (PyLong_Check(pyval)) {
170 val = PyLong_AsLong(pyval);
171 }
172 else if (PyFloat_Check(pyval)) {
173 val = PyFloat_AsDouble(pyval);
174 }
175 else if (PyUnicode_Check(pyval)) {
176 val = std::string(PyUnicode_AsUTF8(pyval));
177 }
178 return val;
179}
180
181static PyObject *engine_set_render_setting_func(PyObject * /*self*/, PyObject *args)
182{
183 PyObject *pyengine, *pyval;
184 char *key;
185 if (!PyArg_ParseTuple(args,
186 "O" /* `engine` */
187 "s" /* `key` */
188 "O" /* `value` */
189 ":engine_set_render_setting",
190 &pyengine,
191 &key,
192 &pyval))
193 {
194 return nullptr;
195 }
196
197 Engine *engine = static_cast<Engine *>(PyLong_AsVoidPtr(pyengine));
198
199 CLOG_DEBUG(LOG_HYDRA_RENDER, "Engine %p: %s", engine, key);
200 engine->set_render_setting(key, get_setting_val(pyval));
201
202 Py_RETURN_NONE;
203}
204
205static PyObject *cache_or_get_image_file_func(PyObject * /*self*/, PyObject *args)
206{
207 PyObject *pycontext, *pyimage;
208 if (!PyArg_ParseTuple(args,
209 "O" /* `context` */
210 "O" /* `image` */
211 ":cache_or_get_image_file",
212 &pycontext,
213 &pyimage))
214 {
215 return nullptr;
216 }
217
218 bContext *context = static_cast<bContext *>(PyLong_AsVoidPtr(pycontext));
219 Image *image = static_cast<Image *>(PyLong_AsVoidPtr(pyimage));
220
221 std::string image_path = io::hydra::cache_or_get_image_file(
222 CTX_data_main(context), CTX_data_scene(context), image, nullptr);
223 return PyC_UnicodeFromStdStr(image_path);
224}
225
226static PyMethodDef methods[] = {
227 {"engine_create", engine_create_func, METH_VARARGS, ""},
228 {"engine_free", engine_free_func, METH_VARARGS, ""},
229 {"engine_update", engine_update_func, METH_VARARGS, ""},
230 {"engine_render", engine_render_func, METH_VARARGS, ""},
231 {"engine_view_draw", engine_view_draw_func, METH_VARARGS, ""},
232 {"engine_set_render_setting", engine_set_render_setting_func, METH_VARARGS, ""},
233
234 {"cache_or_get_image_file", cache_or_get_image_file_func, METH_VARARGS, ""},
235
236 {nullptr, nullptr, 0, nullptr},
237};
238
239static PyModuleDef module = {
241 "_bpy_hydra",
242 "Hydra render API",
243 -1,
244 methods,
245 nullptr,
246 nullptr,
247 nullptr,
248 nullptr,
249};
250
251} // namespace render::hydra
252
253PyObject *BPyInit_hydra();
254
255PyObject *BPyInit_hydra()
256{
257 PyObject *mod = PyModule_Create(&render::hydra::module);
258 return mod;
259}
260
261} // namespace blender
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define STREQ(a, b)
void sync(Depsgraph *depsgraph, bContext *context)
virtual void set_render_setting(const std::string &key, const pxr::VtValue &val)
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
#define T
std::string cache_or_get_image_file(Main *bmain, Scene *scene, Image *image, ImageUser *iuser)
static pxr::VtValue get_setting_val(PyObject *pyval)
Definition python.cc:163
static PyObject * engine_view_draw_func(PyObject *, PyObject *args)
Definition python.cc:137
static PyObject * engine_set_render_setting_func(PyObject *, PyObject *args)
Definition python.cc:181
static PyObject * engine_free_func(PyObject *, PyObject *args)
Definition python.cc:71
static PyObject * engine_render_func(PyObject *, PyObject *args)
Definition python.cc:114
CLG_LogRef * LOG_HYDRA_RENDER
static PyObject * engine_update_func(PyObject *, PyObject *args)
Definition python.cc:89
static PyObject * cache_or_get_image_file_func(PyObject *, PyObject *args)
Definition python.cc:205
static PyObject * engine_create_func(PyObject *, PyObject *args)
Definition python.cc:32
T * pyrna_to_pointer(PyObject *pyobject, const StructRNA *rnatype)
Definition python.cc:26
static PyMethodDef methods[]
Definition python.cc:226
BMesh Iterators.
Main * CTX_data_main(const bContext *C)
PyObject * BPyInit_hydra()
Definition python.cc:255
const PointerRNA * pyrna_struct_as_ptr_or_null(PyObject *py_obj, const StructRNA *srna)
Definition bpy_rna.cc:9405
PyObject * PyC_UnicodeFromStdStr(const std::string &str)
Scene * CTX_data_scene(const bContext *C)
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e