17#include "util/colorspace.h"
48 if (
object == Py_None) {
51 return PyLong_AsVoidPtr(
object);
57 return PyUnicode_DecodeUTF8(
str, strlen(
str),
"ignore");
69 flags.
cpu.
avx2 = get_boolean(cscene,
"debug_use_cpu_avx2");
70 flags.
cpu.
sse42 = get_boolean(cscene,
"debug_use_cpu_sse42");
77 flags.
optix.
use_debug = get_boolean(cscene,
"debug_use_optix_debug");
96 const char *result = PyUnicode_AsUTF8(py_str);
104 if (PyBytes_Check(py_str)) {
105 return PyBytes_AS_STRING(py_str);
108 *coerce = PyUnicode_EncodeFSDefault(py_str);
110 return PyBytes_AS_STRING(*coerce);
125 if (!PyArg_ParseTuple(args,
136 PyObject *path_coerce =
nullptr;
137 PyObject *user_path_coerce =
nullptr;
140 Py_XDECREF(path_coerce);
141 Py_XDECREF(user_path_coerce);
164 PyObject *pypreferences;
172 if (!PyArg_ParseTuple(args,
208 const int width = region->
winx;
209 const int height = region->
winy;
214 blender::id_cast<blender::bScreen *>(bScreen),
222 session =
new BlenderSession(*engine, *preferences, *data, preview_osl);
225 return PyLong_FromVoidPtr(session);
238 PyObject *pydepsgraph;
240 if (!PyArg_ParseTuple(args,
250 blender::Depsgraph *b_depsgraph =
static_cast<blender::Depsgraph *
>(
251 PyLong_AsVoidPtr(pydepsgraph));
254 Py_BEGIN_ALLOW_THREADS;
255 session->
render(*b_depsgraph);
256 Py_END_ALLOW_THREADS;
265 if (!PyArg_ParseTuple(args,
"O" , &pysession)) {
272 Py_BEGIN_ALLOW_THREADS;
274 Py_END_ALLOW_THREADS;
281 PyObject *py_session;
284 PyObject *py_space_image;
286 if (!PyArg_ParseTuple(args,
304 pylong_as_voidptr_typesafe(py_space_image));
308 Py_BEGIN_ALLOW_THREADS;
309 session->
draw(blender::id_cast<blender::bScreen &>(*b_screen), *b_space_image);
310 Py_END_ALLOW_THREADS;
319 PyObject *pydepsgraph;
321 const char *pass_type;
326 if (!PyArg_ParseTuple(args,
347 blender::Depsgraph *b_depsgraph =
static_cast<blender::Depsgraph *
>(
348 PyLong_AsVoidPtr(pydepsgraph));
351 Py_BEGIN_ALLOW_THREADS;
352 session->
bake(*b_depsgraph, *b_object, pass_type, pass_filter, width, height);
353 Py_END_ALLOW_THREADS;
365 if (!PyArg_ParseTuple(args,
380 if (PyLong_AsVoidPtr(pyrv3d)) {
385 Py_BEGIN_ALLOW_THREADS;
386 session->
view_draw(viewport[2], viewport[3]);
387 Py_END_ALLOW_THREADS;
398 if (!PyArg_ParseTuple(args,
"Op", &pysession, &pause)) {
403 Py_BEGIN_ALLOW_THREADS;
405 Py_END_ALLOW_THREADS;
414 PyObject *pydepsgraph;
416 if (!PyArg_ParseTuple(args,
430 blender::Depsgraph *b_depsgraph =
static_cast<blender::Depsgraph *
>(
431 PyLong_AsVoidPtr(pydepsgraph));
433 Py_BEGIN_ALLOW_THREADS;
435 Py_END_ALLOW_THREADS;
443 PyObject *pydepsgraph;
445 if (!PyArg_ParseTuple(args,
456 blender::Depsgraph *b_depsgraph =
static_cast<blender::Depsgraph *
>(
457 PyLong_AsVoidPtr(pydepsgraph));
459 Py_BEGIN_ALLOW_THREADS;
461 Py_END_ALLOW_THREADS;
468 const char *type_name;
469 if (!PyArg_ParseTuple(args,
"s" , &type_name)) {
475 if ((type ==
DEVICE_NONE) && (strcmp(type_name,
"NONE") != 0)) {
476 PyErr_Format(PyExc_ValueError,
"Device \"%s\" not known.", type_name);
484 PyObject *ret = PyTuple_New(devices.size());
486 for (
size_t i = 0; i < devices.size(); i++) {
489 PyObject *device_tuple = PyTuple_New(10);
490 PyTuple_SET_ITEM(device_tuple, 0, pyunicode_from_string(device.
description.c_str()));
491 PyTuple_SET_ITEM(device_tuple, 1, pyunicode_from_string(type_name.c_str()));
492 PyTuple_SET_ITEM(device_tuple, 2, pyunicode_from_string(device.
id.c_str()));
493 PyTuple_SET_ITEM(device_tuple, 3, PyBool_FromLong(device.
has_peer_memory));
501 PyTuple_SET_ITEM(ret, i, device_tuple);
509static PyObject *osl_compile_func(PyObject * , PyObject *args)
511 const char *inputfile =
nullptr;
512 const char *outputfile =
nullptr;
514 if (!PyArg_ParseTuple(args,
524 if (!OSLManager::osl_compile(inputfile, outputfile)) {
535 return pyunicode_from_string(system_info.c_str());
540 if (PyUnicode_Check(pyfilepaths)) {
541 const char *filepath = PyUnicode_AsUTF8(pyfilepaths);
542 filepaths.push_back(filepath);
546 PyObject *sequence = PySequence_Fast(pyfilepaths,
547 "File paths must be a string or sequence of strings");
548 if (sequence ==
nullptr) {
552 for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(sequence); i++) {
553 PyObject *item = PySequence_Fast_GET_ITEM(sequence, i);
554 const char *filepath = PyUnicode_AsUTF8(item);
555 if (filepath ==
nullptr) {
556 PyErr_SetString(PyExc_ValueError,
"File paths must be a string or sequence of strings.");
560 filepaths.push_back(filepath);
567static PyObject *
denoise_func(PyObject * , PyObject *args, PyObject *keywords)
569 static const char *keyword_list[] = {
570 "preferences",
"scene",
"view_layer",
"input",
"output",
nullptr};
571 PyObject *pypreferences;
573 PyObject *pyviewlayer;
575 PyObject *pyoutput =
nullptr;
577 if (!PyArg_ParseTupleAndKeywords(args,
585 (
char **)keyword_list,
597 PyLong_AsVoidPtr(pypreferences));
602 *b_preferences, *b_scene,
true,
true, preferences_device);
606 PyLong_AsVoidPtr(pyviewlayer));
609 *b_scene, b_view_layer,
true, preferences_device);
630 PyErr_SetString(PyExc_ValueError,
"No input file paths specified.");
633 if (input.size() !=
output.size()) {
634 PyErr_SetString(PyExc_ValueError,
"Number of input and output file paths does not match.");
643 denoiser.
input = input;
647 if (!denoiser.
run()) {
648 PyErr_SetString(PyExc_ValueError, denoiser.
error.c_str());
655static PyObject *
merge_func(PyObject * , PyObject *args, PyObject *keywords)
657 static const char *keyword_list[] = {
"input",
"output",
nullptr};
659 PyObject *pyoutput =
nullptr;
661 if (!PyArg_ParseTupleAndKeywords(args,
665 (
char **)keyword_list,
679 if (!PyUnicode_Check(pyoutput)) {
680 PyErr_SetString(PyExc_ValueError,
"Output must be a string.");
683 const string output = PyUnicode_AsUTF8(pyoutput);
687 merger.
input = input;
691 PyErr_SetString(PyExc_ValueError, merger.
error.c_str());
701 if (!PyArg_ParseTuple(args,
"O" , &pyscene)) {
707 debug_flags_sync_from_scene(*b_scene);
709 debug_flags_set =
true;
717 if (debug_flags_set) {
718 debug_flags_set =
false;
732 bool has_cuda =
false;
733 bool has_optix =
false;
734 bool has_hip =
false;
735 bool has_metal =
false;
736 bool has_oneapi =
false;
737 bool has_hiprt =
false;
738 for (
const DeviceType device_type : device_types) {
746 PyObject *list = PyTuple_New(6);
747 PyTuple_SET_ITEM(list, 0, PyBool_FromLong(has_cuda));
748 PyTuple_SET_ITEM(list, 1, PyBool_FromLong(has_optix));
749 PyTuple_SET_ITEM(list, 2, PyBool_FromLong(has_hip));
750 PyTuple_SET_ITEM(list, 3, PyBool_FromLong(has_metal));
751 PyTuple_SET_ITEM(list, 4, PyBool_FromLong(has_oneapi));
752 PyTuple_SET_ITEM(list, 5, PyBool_FromLong(has_hiprt));
758 PyObject *override_string = PyObject_Str(arg);
759 string override = PyUnicode_AsUTF8(override_string);
760 Py_DECREF(override_string);
762 bool include_cpu =
false;
763 const string cpu_suffix =
"+CPU";
766 override =
override.substr(0,
override.
length() - cpu_suffix.length());
769 if (
override ==
"CPU") {
772 else if (
override ==
"CUDA") {
775 else if (
override ==
"OPTIX") {
778 else if (
override ==
"HIP") {
781 else if (
override ==
"METAL") {
784 else if (
override ==
"ONEAPI") {
788 LOG_ERROR <<
override <<
" is not a valid Cycles device.";
800static PyObject *
maketx_func(PyObject * , PyObject *args, PyObject *keywords)
802 static const char *keyword_list[] = {
803 "filepath",
"colorspace",
"alpha_type",
"cache_dir",
nullptr};
805 const char *filepath =
nullptr;
807 const char *alpha_type_str =
"auto";
808 const char *cache_dir =
"";
810 if (!PyArg_ParseTupleAndKeywords(args,
817 (
char **)keyword_list,
828 if (strcmp(alpha_type_str,
"straight") == 0) {
831 else if (strcmp(alpha_type_str,
"premultiplied") == 0) {
834 else if (strcmp(alpha_type_str,
"channel_packed") == 0) {
837 else if (strcmp(alpha_type_str,
"none") == 0) {
840 else if (strcmp(alpha_type_str,
"auto") == 0) {
844 PyErr_Format(PyExc_ValueError,
"Unknown alpha type: %s", alpha_type_str);
854 ccl::ImageMetaData out_metadata;
863 if (out_filepath.empty()) {
864 LOG_ERROR <<
"Source image not found: " << filepath;
865 PyErr_Format(PyExc_RuntimeError,
"Source image not found");
872 Py_BEGIN_ALLOW_THREADS;
874 Py_END_ALLOW_THREADS;
877 LOG_ERROR <<
"Failed to generate tx file";
878 PyErr_Format(PyExc_RuntimeError,
"Failed to generate tx file");
883 return pyunicode_from_string(out_filepath.c_str());
888# pragma clang diagnostic push
889# pragma clang diagnostic ignored "-Wcast-function-type"
891# pragma GCC diagnostic push
892# pragma GCC diagnostic ignored "-Wcast-function-type"
910 {
"osl_compile", osl_compile_func, METH_VARARGS,
""},
916 {
"denoise", (PyCFunction)
denoise_func, METH_VARARGS | METH_KEYWORDS,
""},
917 {
"merge", (PyCFunction)
merge_func, METH_VARARGS | METH_KEYWORDS,
""},
931 {
"maketx", (PyCFunction)
maketx_func, METH_VARARGS | METH_KEYWORDS,
""},
933 {
nullptr,
nullptr, 0,
nullptr},
938# pragma clang diagnostic pop
940# pragma GCC diagnostic pop
947 "Blender cycles render integration",
960 PyObject *
mod = PyModule_Create(&ccl::module);
968 const int curversion = OSL_LIBRARY_VERSION_CODE;
969 PyModule_AddObjectRef(
mod,
"with_osl", Py_True);
973 Py_BuildValue(
"(iii)", curversion / 10000, (curversion / 100) % 100, curversion % 100));
976 "osl_version_string",
977 PyUnicode_FromFormat(
978 "%2d, %2d, %2d", curversion / 10000, (curversion / 100) % 100, curversion % 100));
980 PyModule_AddObjectRef(
mod,
"with_osl", Py_False);
981 PyModule_AddStringConstant(
mod,
"osl_version",
"unknown");
982 PyModule_AddStringConstant(
mod,
"osl_version_string",
"unknown");
985 if (ccl::guiding_supported()) {
986 PyModule_AddObjectRef(
mod,
"with_path_guiding", Py_True);
989 PyModule_AddObjectRef(
mod,
"with_path_guiding", Py_False);
993 PyModule_AddObjectRef(
mod,
"with_embree", Py_True);
995 PyModule_AddObjectRef(
mod,
"with_embree", Py_False);
998#ifdef WITH_EMBREE_GPU
999 PyModule_AddObjectRef(
mod,
"with_embree_gpu", Py_True);
1001 PyModule_AddObjectRef(
mod,
"with_embree_gpu", Py_False);
1004 if (ccl::openimagedenoise_supported()) {
1005 PyModule_AddObjectRef(
mod,
"with_openimagedenoise", Py_True);
1008 PyModule_AddObjectRef(
mod,
"with_openimagedenoise", Py_False);
1012 PyModule_AddObjectRef(
mod,
"with_dlss", Py_True);
1014 PyModule_AddObjectRef(
mod,
"with_dlss", Py_False);
1017#ifdef WITH_CYCLES_DEBUG
1018 PyModule_AddObjectRef(
mod,
"with_debug", Py_True);
1020 PyModule_AddObjectRef(
mod,
"with_debug", Py_False);
DeviceInfo blender_device_info(blender::UserDef &b_preferences, blender::Scene &b_scene, bool background, bool preview, DeviceInfo &preferences_device)
static bool print_render_stats
void draw(blender::bScreen &b_screen, blender::SpaceImage &space_image)
void synchronize(blender::Depsgraph &b_depsgraph)
void view_pause(const bool pause)
void render(blender::Depsgraph &b_depsgraph)
void bake(blender::Depsgraph &b_depsgraph_, blender::Object &b_object, const string &bake_type, const int bake_filter, const int bake_width, const int bake_height)
void reset_session(blender::Main &b_data, blender::Depsgraph &b_depsgraph)
void view_draw(const int w, const int h)
BlenderSession(blender::RenderEngine &b_engine, blender::UserDef &b_userpref, blender::Main &b_data, bool preview_osl)
void render_frame_finish()
static DeviceTypeMask device_override
static DenoiseParams get_denoise_params(blender::Scene &b_scene, blender::ViewLayer *b_view_layer, bool background, const DeviceInfo &denoise_device)
static void free_memory()
TextureCache texture_cache
bool has_execution_optimization
bool meets_driver_requirement
DenoiserTypeMask denoisers
bool use_hardware_raytracing
static void free_memory()
static DeviceType type_from_string(const char *name)
static string device_capabilities(const uint device_type_mask=DEVICE_MASK_ALL)
static vector< DeviceType > available_types()
static string string_from_type(DeviceType type)
static vector< DeviceInfo > available_devices(const uint device_type_mask=DEVICE_MASK_ALL)
static void free_memory()
static void free_memory()
CCL_NAMESPACE_BEGIN ustring u_colorspace_auto
DebugFlags & DebugFlagsRef
@ DENOISER_OPENIMAGEDENOISE
#define CCL_NAMESPACE_END
#define DEVICE_MASK(type)
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
float length(VecOp< float, D >) RET
bool resolve_tx(const string &filepath, const string &texture_cache_path, ustring colorspace, const ImageAlphaType alpha_type, const ImageFormatType format_type, string &out_filepath, ImageMetaData &out_metadata)
bool make_tx(const string &filepath, const string &out_filepath, ustring colorspace, const ImageAlphaType alpha_type, const ImageFormatType format_type)
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
void debug_flags_sync_from_scene(blender::Scene &b_scene)
void * pylong_as_voidptr_typesafe(PyObject *object)
PyObject * pyunicode_from_string(const char *str)
void GPU_viewport_size_get_i(int coords[4])
void * CCL_python_module_init()
KernelBVHLayout BVHLayout
void path_init(const string &path, const string &user_path)
static PyObject * init_func(PyObject *, PyObject *args)
static PyObject * merge_func(PyObject *, PyObject *args, PyObject *keywords)
static PyObject * sync_func(PyObject *, PyObject *args)
static PyObject * denoise_func(PyObject *, PyObject *args, PyObject *keywords)
static PyObject * system_info_func(PyObject *, PyObject *)
static PyObject * set_device_override_func(PyObject *, PyObject *arg)
static PyObject * free_func(PyObject *, PyObject *value)
static PyObject * view_pause_func(PyObject *, PyObject *args)
static struct PyModuleDef module
static bool image_parse_filepaths(PyObject *pyfilepaths, vector< string > &filepaths)
static PyMethodDef methods[]
static PyObject * available_devices_func(PyObject *, PyObject *args)
static PyObject * maketx_func(PyObject *, PyObject *args, PyObject *keywords)
static PyObject * bake_func(PyObject *, PyObject *args)
static PyObject * reset_func(PyObject *, PyObject *args)
static PyObject * view_draw_func(PyObject *, PyObject *args)
static PyObject * enable_print_stats_func(PyObject *, PyObject *)
static PyObject * debug_flags_reset_func(PyObject *, PyObject *)
static PyObject * render_func(PyObject *, PyObject *args)
static PyObject * debug_flags_update_func(PyObject *, PyObject *args)
static PyObject * draw_func(PyObject *, PyObject *args)
static PyObject * get_device_types_func(PyObject *, PyObject *)
static const char * PyC_UnicodeAsBytes(PyObject *py_str, PyObject **coerce)
static PyObject * render_frame_finish_func(PyObject *, PyObject *args)
static PyObject * create_func(PyObject *, PyObject *args)
static PyObject * exit_func(PyObject *, PyObject *)
bool string_endswith(const string_view s, const string_view end)
@ IMAGE_ALPHA_CHANNEL_PACKED
@ IMAGE_ALPHA_UNASSOCIATED