0
$\begingroup$

I have exactly 1 day experience of Blender so this is sure to be something really basic. I have created a scene with a light and a single cube with translucent material, when I look at the scene through the camera in the Blender UI I see this, which is what I would expect:

enter image description here

but when I look in the rendered PNG file I see this:

enter image description here

i.e. no shading or transparency. I just want the rendered image to match what I see in Blender.

One thing to note - I need to be able to render the image using code only, so telling me which menu/button to use within the UI won't help me.

My script is as follows:

import bpy
import mathutils, math

scene = bpy.context.scene

# Lights
light_data = bpy.data.lights.new('light', type='POINT')
light = bpy.data.objects.new('light', light_data)
light.location = mathutils.Vector((5,5,5))
bpy.context.collection.objects.link(light)

# Camera
cam_data = bpy.data.cameras.new('camera')
cam = bpy.data.objects.new('camera', cam_data)
bpy.context.collection.objects.link(cam)
scene.camera = cam
cam.location = mathutils.Vector((5, -5, 5))    
cam.rotation_euler = (1, -0.5, 1)

# Action
mat = bpy.data.materials.new(name='mat')
mat.diffuse_color = [1, 1, 0, 0.5]
bpy.ops.mesh.primitive_cube_add()
cube = bpy.context.object

cube.data.materials.append(mat)

scene.render.image_settings.file_format = 'PNG'
scene.render.filepath = "cube.png"
bpy.ops.render.render(write_still = 1)
$\endgroup$

2 Answers 2

1
$\begingroup$

For transparency make sure you have "Transparent" checked on: enter image description here

And for the shadows - it seems you don't have a light source next to the cube, if there's no light there can be no shadows, add a light by pressing SHIFT + A and clicking on light, I'd recommend getting a sun so everything is lit and you can check if there are shadows. enter image description here

$\endgroup$
1
  • $\begingroup$ thanks for your answer, changing the light source type of 'SUN' fixed the illumination problem but the transparency setting had no effect. $\endgroup$
    – codebox
    Jun 1, 2020 at 10:36
0
$\begingroup$

I found the answer to this with some help from r/blenderhelp

Adding the following line made the cube render with the correct transparency

bpy.context.scene.render.engine = 'BLENDER_WORKBENCH'
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .