I am new to blender and trying to render this file. Trying to render using script gives a very different result (everything purple) as compared to what I see in Blender GUI. What am I doing wrong?
Here is the script I am using
import os
import bpy
bpy.ops.wm.open_mainfile(filepath="materials.blend")
VIEWS = 1
RESOLUTION = 200
RESULTS_PATH = '.'
DEPTH_SCALE = 1.4
COLOR_DEPTH = 8
FORMAT = 'PNG'
RANDOM_VIEWS = True
UPPER_VIEWS = True
CIRCLE_FIXED_START = (.3, 0, 0)
fp = bpy.path.abspath(f"//{RESULTS_PATH}")
if not os.path.exists(fp):
os.makedirs(fp)
Render Optimizations
bpy.context.scene.render.use_persistent_data = True
Set up rendering of depth map.
bpy.context.scene.use_nodes = True
tree = bpy.context.scene.node_tree
links = tree.links
Add passes for additionally dumping albedo and normals.
bpy.context.scene.view_layers["RenderLayer"].use_pass_normal = True
bpy.context.scene.render.image_settings.file_format = str(FORMAT)
bpy.context.scene.render.image_settings.color_depth = str(COLOR_DEPTH)
Background
bpy.context.scene.render.dither_intensity = 0.0
bpy.context.scene.render.film_transparent = True
Create collection for objects not to render with background
objs = [ob for ob in bpy.context.scene.objects if ob.type in ('EMPTY') and 'Empty' in ob.name]
bpy.ops.object.delete({"selected_objects": objs})
def parent_obj_to_camera(b_camera):
origin = (0, 0, 0)
b_empty = bpy.data.objects.new("Empty", None)
b_empty.location = origin
b_camera.parent = b_empty # setup parenting
scn = bpy.context.scene
scn.collection.objects.link(b_empty)
bpy.context.view_layer.objects.active = b_empty
# scn.objects.active = b_empty
return b_empty
scene = bpy.context.scene
scene.render.resolution_x = RESOLUTION
scene.render.resolution_y = RESOLUTION
scene.render.resolution_percentage = 100
cam = scene.objects['Camera']
cam.location = (0, 4.0, 0.5)
cam_constraint = cam.constraints.new(type='TRACK_TO')
cam_constraint.track_axis = 'TRACK_NEGATIVE_Z'
cam_constraint.up_axis = 'UP_Y'
b_empty = parent_obj_to_camera(cam)
cam_constraint.target = b_empty
b_empty.rotation_euler = CIRCLE_FIXED_START
scene.render.image_settings.file_format = FORMAT
scene.render.filepath = fp + '/r_0'
bpy.ops.render.render(write_still=True) # render still
Output of blender --background --python script.py
Screenshot of Blender GUI

