2
$\begingroup$

EDIT (Solution):

I was able to fix this by adding the following line:

bpy.context.scene.render.engine = 'CYCLES'

This works fine for me as I need to use the cycles engine anyways

Original Post:

I'm writing a standalone python script to render .OBJ files automatically, without a .blend file or anything. I installed bpy 2.82 on my windows machine using the instructions found here. And I have python 3.7.9 installed.

Currently, as just a test to get my bearings, I tried the following code to try to setup a simple scene, load in a .OBJ, and render it out:

    import bpy
    import os
    from math import radians

    maplet_path = 'obj_files\\bunny.obj'
    image_out_path = 'render.png'

    # Create a Scene:
    scene       = bpy.data.scenes.new("Scene")
    camera_data = bpy.data.cameras.new("Camera")
    light_data  = bpy.data.lights.new(name="Light", type='SUN')

    # Import the maplet data:
    local_path = os.getcwd()
    bpy.ops.import_scene.obj(filepath=os.path.join(local_path,maplet_path))
    scene.render.filepath = os.path.join(local_path,image_out_path)
    scene.render.image_settings.file_format='PNG'

    # Get handles for all objects:
    maplet = bpy.context.selected_objects[0] 
    scene = bpy.data.scenes.new("Scene")
    camera = bpy.data.objects.new("Camera", camera_data)
    sun    = bpy.data.objects.new(name="Light", object_data=light_data)

    # Setup camera parameters:
    scene.render.resolution_x = 1920
    scene.render.resolution_y = 1080
    camera.location = (-2.0, 3.0, 3.0)
    camera.rotation_euler = ([radians(a) for a in (422.0, 0.0, 149)])

    # Setup scene:
    sun.location = (2.0,2.0,2.0)
    sun.rotation_euler = (radians(10), radians(15), radians(20))
    maplet.location = (0,0,0)
    maplet.rotation_euler = (0,0,0)
        
    # Add camera to the scene:
    scene.camera = camera

    # Update the scene:
    bpy.context.view_layer.update()

    # Render:
    bpy.ops.render.render("EXEC_DEFAULT",write_still=True)
    bpy.data.images['Render Result'].save_render(os.path.join(local_path,image_out_path))
    print('hi')

Unfortunately, the program runs perfectly fine, but once it gets to the line:

bpy.ops.render.render("EXEC_DEFAULT",write_still=True)

it hangs for a moment, and then just exits. It never prints 'hi', and it never writes any image. It also doesn't print out any errors or anything else, it just stops running.

I tried stepping through with pdb and it appears to hang/fail at:

c:\......\python\python37\2.82\scripts\modules\bpy\ops.py(182)idname_py()-'render.render'
-> return self._module + "." + self._func

Is there something wrong with how I went about installing bpy? Or something wrong with the script I've written?

$\endgroup$
3
  • $\begingroup$ Can you start python 3.7.9 from terminal, and do a bare bones import bpy followed by bpy.ops.render.render() ? Just checked (have bpy 2.90.2 built against python 3.8.2 (ubuntu) and that simple test segfaults) $\endgroup$
    – batFINGER
    Feb 14 at 15:15
  • $\begingroup$ @batFINGER I just tried that, and you're right, it fails. It doesn't give me any segfault errors, it just exits (so thats likely whats going on). Which is weird... because I have 100% used the my computers python37/bpy install to render from a .blend file (for some automation I needed before). I'm not sure what could have changed... $\endgroup$
    – Chris Gnam
    Feb 14 at 23:21
  • 1
    $\begingroup$ @batFINGER I was able to fix this issue by adding: bpy.context.scene.render.engine = 'CYCLES' to the script. Which is a desireable fix for me anyways, since I want to be using cycles for this project anyways. Unsure if that'll help those looking for eevee $\endgroup$
    – Chris Gnam
    Feb 14 at 23:28
1
$\begingroup$

Adding

bpy.context.scene.render.engine = 'CYCLES'

Fixed this issue

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.