Reputation: 91
I am trying to render images using the bpy python package in a celery worker. The setup is the following
celery_app.py
from celery import Celery
app = Celery('tasks', broker='pyamqp://guest@localhost//', include=['tasks'])
if __name__ == '__main__':
app.start()
main.py
from celery_app import app
if __name__ == '__main__':
print(app.send_task('tasks.render_task', args=[]))
tasks.py
import bpy
from os.path import abspath
from celery_app import app
@app.task(bind=True, acks_late=True, reject_on_worker_lost=True)
def render_task(self):
bpy.ops.wm.open_mainfile(filepath="/tmp/test/untitled.blend")
bpy.context.preferences.addons["cycles"].preferences.compute_device_type = "OPTIX"
bpy.context.preferences.addons["cycles"].preferences.get_devices()
for d in bpy.context.preferences.addons["cycles"].preferences.devices:
if d.type != "CPU":
d["use"] = 1
bpy.context.scene.render.filepath = abspath('test.png')
bpy.ops.render.render(write_still=True)
Note the blender file "untitled.blend" is the default scene with "Cycles" as the render engine and a high quality .exr as a lighting HDR.
If I run "main.py" the render starts but hangs at the updating of the HDR.
[2023-07-22 18:07:55,934: INFO/MainProcess] Task tasks.render_task[5238582a-94c2-4a06-bb3f-c9f540b44405] received
Read blend: "/tmp/test/untitled.blend"
Fra:1 Mem:9.28M (Peak 9.28M) | Time:00:00.16 | Mem:0.00M, Peak:0.00M | Scene, ViewLayer | Synchronizing object | Cube
Fra:1 Mem:9.28M (Peak 9.28M) | Time:00:00.16 | Mem:0.00M, Peak:0.00M | Scene, ViewLayer | Initializing
Fra:1 Mem:9.12M (Peak 9.28M) | Time:00:00.16 | Mem:0.00M, Peak:0.00M | Scene, ViewLayer | Waiting for render to start
Fra:1 Mem:9.12M (Peak 9.28M) | Time:00:00.16 | Mem:0.00M, Peak:0.00M | Scene, ViewLayer | Loading render kernels (may take a few minutes the first time)
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Scene
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Shaders
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Procedurals
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Background
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Camera
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Meshes Flags
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Objects
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Objects | Copying Transformations to device
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Objects | Applying Static Transformations
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Particle Systems
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Particle Systems | Copying Particles to device
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Meshes
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Mesh | Computing attributes
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Mesh | Copying Attributes to device
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Geometry BVH Cube 1/1 | Building BVH
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Geometry BVH Cube 1/1 | Building OptiX acceleration structure
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Scene BVH | Building
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Scene BVH | Building OptiX acceleration structure
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.24 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Scene BVH | Copying BVH to device
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.24 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Mesh | Computing normals
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.24 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Mesh | Copying Mesh to device
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.24 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Objects Flags
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.24 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Primitive Offsets
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.24 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Images
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.24 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Images | Loading HdrSkyCloudy004_HDR_8K.exr
A workaround to this problem is that i can import bpy inside of the render_task function, which allows the image to render in 22 seconds.
However an interesting observation is that if i where to remove the HDR from the blender file the image renders regardless of where the import statement is placed. But if the import is placed at the top it takes 40 seconds, while only taking 11 when it is placed in the function call.
This also only happens if Cycles is selected as the render engine.
So i am a little confused as to what is happening, there seems to be an issue with the import of the bpy module with celery. And i would really like to avoid having to import the bpy module locally in each function.
If anyone knows a way how I can avoid this, it would be highly appreciated, or is this a inherent blender issue and i should create an issue there?
Upvotes: 0
Views: 150
Reputation: 11
You can ignore my last post. Celery has two processes running. One always shows this file exists error but that's not bad. The other has a FUTEX error that indicates a threading problem.
SOLUTION: I have changed and tried various Blender performance parameters. After I disabled the auto tiling, it worked.
bpy.data.scenes["Scene"].cycles.use_auto_tile = False
Upvotes: 1