System Information
Operating system: Ubuntu 20.04.1 LTS
Graphics card: GTX 1070 + GTX 1070 + GTX 1050 Ti, NVIDIA 440.100
Blender Version
Broken: 2.90.1, Commit date: 2020-09-23 06:43, Hash 3e85bb34d0d7
Worked: 2.83.7
Short description of error
Blender crashes when using python to choose CUDA GPU for cycles. I don't know if this happens in systems with only 1 GPU.
Exact steps for others to reproduce the error
Run Blender from command line with python expression to select GPU:
blender --factory-startup --python-expr "import bpy; cuda_devices, opencl_devices = bpy.context.preferences.addons['cycles'].preferences.get_devices(); cuda_devices[0].use = True"
More information:
Using get_devices_for_type() instead of get_devices() does not crash.
blender --factory-startup --python-expr "import bpy; cuda_devices = bpy.context.preferences.addons['cycles'].preferences.get_devices_for_type('CUDA'); cuda_devices[0].use = True"
The specific sequence of device types used by get_devices() matters, CUDA, then OPTIX, then OPENCL. Changing the sequence to CUDA last does not crash. Something about OPTIX and OPENCL mutates the CUDA devices. See the dump of cuda_devices before and after getting OPENCL devices:
blender --factory-startup --python-expr "import bpy; \ cuda_devices = bpy.context.preferences.addons['cycles'].preferences.get_devices_for_type('CUDA'); \ optix_devices = bpy.context.preferences.addons['cycles'].preferences.get_devices_for_type('OPTIX'); \ print('CUDA devices before getting OPENCL devices', cuda_devices); \ opencl_devices = bpy.context.preferences.addons['cycles'].preferences.get_devices_for_type('OPENCL'); \ print('CUDA devices after getting OPENCL devices', cuda_devices); \ print('OPTIX devices', optix_devices); \ print('OPENCL devices', opencl_devices); \ cuda_devices[0].use = True" CUDA devices before getting OPENCL devices [<bpy_struct, CyclesDeviceSettings("GeForce GTX 1070")>, <bpy_struct, CyclesDeviceSettings("GeForce GTX 1070")>, <bpy_struct, CyclesDeviceSettings("GeForce GTX 1050 Ti")>, <bpy_struct, CyclesDeviceSettings("AMD Ryzen 9 3900X 12-Core Processor")>] CUDA devices after getting OPENCL devices [<bpy_struct, CyclesDeviceSettings("")>, <bpy_struct, CyclesDeviceSettings("")>, <bpy_struct, CyclesDeviceSettings("")>, <bpy_struct, CyclesDeviceSettings("")>] OPTIX devices [<bpy_struct, CyclesDeviceSettings("GeForce GTX 1070")>, <bpy_struct, CyclesDeviceSettings("GeForce GTX 1070")>, <bpy_struct, CyclesDeviceSettings("GeForce GTX 1050 Ti")>] OPENCL devices [<bpy_struct, CyclesDeviceSettings("AMD Ryzen 9 3900X 12-Core Processor")>]
Also, this seems to have something to do with OPTIX. Removing OPTIX from the sequence above also does not crash.