Blender V5.3
bpy_inline_shader_nodes.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
8
9#include "BKE_idtype.hh"
10#include "BKE_lib_id.hh"
11#include "BKE_node.hh"
12
13#include "DNA_light_types.h"
14#include "DNA_material_types.h"
15#include "DNA_node_types.h"
16
17#include "DNA_world_types.h"
19
21#include "bpy_rna.hh"
22
24
25namespace blender {
26
27extern PyTypeObject bpy_inline_shader_nodes_Type;
28
30 PyObject_HEAD
32};
33
35{
36 BPy_InlineShaderNodes *self = reinterpret_cast<BPy_InlineShaderNodes *>(
38 if (!self) {
39 return nullptr;
40 }
42 nullptr, (StringRef(tree.id.name) + " Inlined").c_str(), tree.idname);
45 return self;
46}
47
49 /* Wrap. */
50 bpy_inline_shader_nodes_from_material_doc,
51 ".. staticmethod:: from_material(material)\n"
52 "\n"
53 " Create an inlined shader node tree from a material.\n"
54 "\n"
55 " :param material: The material to inline the node tree of.\n"
56 " :type material: bpy.types.Material\n");
58 PyObject *args,
59 PyObject *kwds)
60{
61 static const char *kwlist[] = {"material", nullptr};
62 PyObject *py_material;
63 if (!PyArg_ParseTupleAndKeywords(args,
64 kwds,
65 "O" /* `material` */
66 ":from_material",
67 const_cast<char **>(kwlist),
68 &py_material))
69 {
70 return nullptr;
71 }
72 ID *material_id = nullptr;
73 if (!pyrna_id_FromPyObject(py_material, &material_id)) {
74 PyErr_Format(
75 PyExc_TypeError, "Expected a Material, not %.200s", Py_TYPE(py_material)->tp_name);
76 return nullptr;
77 }
78 if (GS(material_id->name) != ID_MA) {
79 PyErr_Format(PyExc_TypeError,
80 "Expected a Material, not %.200s",
81 BKE_idtype_idcode_to_name(GS(material_id->name)));
82 return nullptr;
83 }
84 Material *material = id_cast<Material *>(material_id);
85 if (!material->nodetree) {
86 PyErr_Format(PyExc_TypeError, "Material '%s' has no node tree", BKE_id_name(*material_id));
87 return nullptr;
88 }
89 return create_from_shader_node_tree(*material->nodetree);
90}
91
93 /* Wrap. */
94 bpy_inline_shader_nodes_from_light_doc,
95 ".. staticmethod:: from_light(light)\n"
96 "\n"
97 " Create an inlined shader node tree from a light.\n"
98 "\n"
99 " :param light: The light to online the node tree of.\n"
100 " :type light: bpy.types.Light\n");
102 PyObject *args,
103 PyObject *kwds)
104{
105 static const char *kwlist[] = {"light", nullptr};
106 PyObject *py_light;
107 if (!PyArg_ParseTupleAndKeywords(args,
108 kwds,
109 "O" /* `light` */
110 ":from_light",
111 const_cast<char **>(kwlist),
112 &py_light))
113 {
114 return nullptr;
115 }
116 ID *light_id = nullptr;
117 if (!pyrna_id_FromPyObject(py_light, &light_id)) {
118 PyErr_Format(PyExc_TypeError, "Expected a Light, not %.200s", Py_TYPE(py_light)->tp_name);
119 return nullptr;
120 }
121 if (GS(light_id->name) != ID_LA) {
122 PyErr_Format(PyExc_TypeError,
123 "Expected a Light, not %.200s",
124 BKE_idtype_idcode_to_name(GS(light_id->name)));
125 return nullptr;
126 }
127 Light *light = id_cast<Light *>(light_id);
128 if (!light->nodetree) {
129 PyErr_Format(PyExc_TypeError, "Light '%s' has no node tree", BKE_id_name(*light_id));
130 return nullptr;
131 }
132 return create_from_shader_node_tree(*light->nodetree);
133}
134
136 /* Wrap. */
137 bpy_inline_shader_nodes_from_world_doc,
138 ".. staticmethod:: from_world(world)\n"
139 "\n"
140 " Create an inlined shader node tree from a world.\n"
141 "\n"
142 " :param world: The world to inline the node tree of.\n"
143 " :type world: bpy.types.World\n");
145 PyObject *args,
146 PyObject *kwds)
147{
148 static const char *kwlist[] = {"world", nullptr};
149 PyObject *py_world;
150 if (!PyArg_ParseTupleAndKeywords(args,
151 kwds,
152 "O" /* `world` */
153 ":from_world",
154 const_cast<char **>(kwlist),
155 &py_world))
156 {
157 return nullptr;
158 }
159 ID *world_id = nullptr;
160 if (!pyrna_id_FromPyObject(py_world, &world_id)) {
161 PyErr_Format(PyExc_TypeError, "Expected a World, not %.200s", Py_TYPE(py_world)->tp_name);
162 return nullptr;
163 }
164 if (GS(world_id->name) != ID_WO) {
165 PyErr_Format(PyExc_TypeError,
166 "Expected a World, not %.200s",
167 BKE_idtype_idcode_to_name(GS(world_id->name)));
168 return nullptr;
169 }
170 World *world = id_cast<World *>(world_id);
171 if (!world->nodetree) {
172 PyErr_Format(PyExc_TypeError, "World '%s' has no node tree", BKE_id_name(*world_id));
173 return nullptr;
174 }
175 return create_from_shader_node_tree(*world->nodetree);
176}
177
179{
180 BKE_id_free(nullptr, self->inline_node_tree);
181 Py_TYPE(self)->tp_free(reinterpret_cast<PyObject *>(self));
182}
183
185 /* Wrap. */
186 bpy_inline_shader_nodes_node_tree_doc,
187 "The inlined node tree.\n"
188 "\n"
189 ":type: :class:`bpy.types.NodeTree`\n");
191 void * /*closure*/)
192{
193 return pyrna_id_CreatePyObject(id_cast<ID *>(self->inline_node_tree));
194}
195
196static PyGetSetDef BPy_InlineShaderNodes_getseters[] = {
197 {"node_tree",
198 reinterpret_cast<getter>(BPy_InlineShaderNodes_get_node_tree),
199 nullptr,
200 bpy_inline_shader_nodes_node_tree_doc,
201 nullptr},
202 {nullptr},
203};
204
205#ifdef __GNUC__
206# ifdef __clang__
207# pragma clang diagnostic push
208# pragma clang diagnostic ignored "-Wcast-function-type"
209# else
210# pragma GCC diagnostic push
211# pragma GCC diagnostic ignored "-Wcast-function-type"
212# endif
213#endif
214
215static PyMethodDef BPy_InlineShaderNodes_methods[] = {
216 {"from_material",
217 reinterpret_cast<PyCFunction>(BPy_InlineShaderNodes_static_from_material),
218 METH_VARARGS | METH_KEYWORDS | METH_STATIC,
219 bpy_inline_shader_nodes_from_material_doc},
220 {"from_light",
221 reinterpret_cast<PyCFunction>(BPy_InlineShaderNodes_static_from_light),
222 METH_VARARGS | METH_KEYWORDS | METH_STATIC,
223 bpy_inline_shader_nodes_from_light_doc},
224 {"from_world",
225 reinterpret_cast<PyCFunction>(BPy_InlineShaderNodes_static_from_world),
226 METH_VARARGS | METH_KEYWORDS | METH_STATIC,
227 bpy_inline_shader_nodes_from_world_doc},
228 {nullptr},
229};
230
231#ifdef __GNUC__
232# ifdef __clang__
233# pragma clang diagnostic pop
234# else
235# pragma GCC diagnostic pop
236# endif
237#endif
238
240 /* Wrap. */
241 bpy_inline_shader_nodes_doc,
242 "An inlined shader node tree.\n");
244 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
245 /*tp_name*/ "InlineShaderNodes",
246 /*tp_basicsize*/ sizeof(BPy_InlineShaderNodes),
247 /*tp_itemsize*/ 0,
248 /*tp_dealloc*/ reinterpret_cast<destructor>(BPy_InlineShaderNodes_dealloc),
249 /*tp_vectorcall_offset*/ 0,
250 /*tp_getattr*/ nullptr,
251 /*tp_setattr*/ nullptr,
252 /*tp_as_async*/ nullptr,
253 /*tp_repr*/ nullptr,
254 /*tp_as_number*/ nullptr,
255 /*tp_as_sequence*/ nullptr,
256 /*tp_as_mapping*/ nullptr,
257 /*tp_hash*/ nullptr,
258 /*tp_call*/ nullptr,
259 /*tp_str*/ nullptr,
260 /*tp_getattro*/ nullptr,
261 /*tp_setattro*/ nullptr,
262 /*tp_as_buffer*/ nullptr,
263 /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
264 /*tp_doc*/ bpy_inline_shader_nodes_doc,
265 /*tp_traverse*/ nullptr,
266 /*tp_clear*/ nullptr,
267 /*tp_richcompare*/ nullptr,
268 /*tp_weaklistoffset*/ 0,
269 /*tp_iter*/ nullptr,
270 /*tp_iternext*/ nullptr,
271 /*tp_methods*/ BPy_InlineShaderNodes_methods,
272 /*tp_members*/ nullptr,
274 /*tp_base*/ nullptr,
275 /*tp_dict*/ nullptr,
276 /*tp_descr_get*/ nullptr,
277 /*tp_descr_set*/ nullptr,
278 /*tp_dictoffset*/ 0,
279 /*tp_init*/ nullptr,
280 /*tp_alloc*/ nullptr,
281 /*tp_new*/ nullptr,
282};
283
285{
286 if (PyType_Ready(&bpy_inline_shader_nodes_Type) < 0) {
287 return nullptr;
288 }
289 return reinterpret_cast<PyObject *>(&bpy_inline_shader_nodes_Type);
290}
291
292} // namespace blender
constexpr StringRef()
bNodeTree * node_tree_add_tree(Main *bmain, StringRef name, StringRef idname)
Definition node.cc:4992
bool inline_shader_node_tree(const bNodeTree &src_tree, bNodeTree &dst_tree, InlineShaderNodeTreeParams &params)
BMesh Iterators.
const char * BKE_idtype_idcode_to_name(short idcode)
Definition idtype.cc:261
PyObject * pyrna_id_CreatePyObject(ID *id)
Definition bpy_rna.cc:8978
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
static PyMethodDef BPy_InlineShaderNodes_methods[]
const char * BKE_id_name(const ID &id)
static BPy_InlineShaderNodes * BPy_InlineShaderNodes_static_from_world(PyObject *, PyObject *args, PyObject *kwds)
static BPy_InlineShaderNodes * BPy_InlineShaderNodes_static_from_material(PyObject *, PyObject *args, PyObject *kwds)
PyObject * BPyInit_inline_shader_nodes_type()
bool pyrna_id_FromPyObject(PyObject *obj, ID **id)
Definition bpy_rna.cc:8988
void BKE_id_free(Main *bmain, void *idv)
static PyObject * BPy_InlineShaderNodes_get_node_tree(BPy_InlineShaderNodes *self, void *)
static PyGetSetDef BPy_InlineShaderNodes_getseters[]
ID_Type GS(const char *name)
Definition DNA_ID.h:410
static BPy_InlineShaderNodes * BPy_InlineShaderNodes_static_from_light(PyObject *, PyObject *args, PyObject *kwds)
static void BPy_InlineShaderNodes_dealloc(BPy_InlineShaderNodes *self)
PyTypeObject bpy_inline_shader_nodes_Type
static BPy_InlineShaderNodes * create_from_shader_node_tree(const bNodeTree &tree)
PyObject_HEAD bNodeTree * inline_node_tree
char name[258]
Definition DNA_ID.h:433