diff options
author | Lukas Stockner <lukas.stockner@freenet.de> | 2020-10-02 01:48:01 +0300 |
---|---|---|
committer | Lukas Stockner <lukas.stockner@freenet.de> | 2020-10-02 20:26:35 +0300 |
commit | cfa101c22871c3d115f854e23f8b656b1c58a304 (patch) | |
tree | 33051b28379c0e23d0948c61ee63d9a95dd45266 /intern/cycles/blender/blender_python.cpp | |
parent | 90a27d5aa91a1b6a25ea14e11c889d47f77f4cf7 (diff) |
Cycles: Add command line option for overriding the compute device
The current way of setting the compute device makes sense for local
use, but for headless rendering it it a massive pain to get Cycles
to use the correct device, usually involving entire Python scripts.
Therefore, this patch adds a simple command-line option to Blender
for specifying the type of device that should be used. If the option
is present, the settings in the user preferences and the scene are
ignored, and instead all devices matching the specified type are used.
Differential Revision: https://developer.blender.org/D9086
Diffstat (limited to 'intern/cycles/blender/blender_python.cpp')
-rw-r--r-- | intern/cycles/blender/blender_python.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/intern/cycles/blender/blender_python.cpp b/intern/cycles/blender/blender_python.cpp index 25c77b74ce3..65337d1ba4f 100644 --- a/intern/cycles/blender/blender_python.cpp +++ b/intern/cycles/blender/blender_python.cpp @@ -968,6 +968,44 @@ static PyObject *get_device_types_func(PyObject * /*self*/, PyObject * /*args*/) return list; } +static PyObject *set_device_override_func(PyObject * /*self*/, PyObject *arg) +{ + PyObject *override_string = PyObject_Str(arg); + string override = PyUnicode_AsUTF8(override_string); + Py_DECREF(override_string); + + bool include_cpu = false; + const string cpu_suffix = "+CPU"; + if (string_endswith(override, cpu_suffix)) { + include_cpu = true; + override = override.substr(0, override.length() - cpu_suffix.length()); + } + + if (override == "CPU") { + BlenderSession::device_override = DEVICE_MASK_CPU; + } + else if (override == "OPENCL") { + BlenderSession::device_override = DEVICE_MASK_OPENCL; + } + else if (override == "CUDA") { + BlenderSession::device_override = DEVICE_MASK_CUDA; + } + else if (override == "OPTIX") { + BlenderSession::device_override = DEVICE_MASK_OPTIX; + } + else { + printf("\nError: %s is not a valid Cycles device.\n", override.c_str()); + Py_RETURN_FALSE; + } + + if (include_cpu) { + BlenderSession::device_override = (DeviceTypeMask)(BlenderSession::device_override | + DEVICE_MASK_CPU); + } + + Py_RETURN_TRUE; +} + static PyMethodDef methods[] = { {"init", init_func, METH_VARARGS, ""}, {"exit", exit_func, METH_VARARGS, ""}, @@ -1007,6 +1045,7 @@ static PyMethodDef methods[] = { /* Compute Device selection */ {"get_device_types", get_device_types_func, METH_VARARGS, ""}, + {"set_device_override", set_device_override_func, METH_O, ""}, {NULL, NULL, 0, NULL}, }; |