Light does not render on script

by czr   Last Updated August 13, 2019 19:15 PM - source

I am trying to develop a script that will render a lot of views from objects and output bounding boxes coordinates for those objects. I am a complete beginner in Blender and 3D in general, until now I came up with the following script:

import bpy
import sys
import os
import math
from mathutils import Vector
import mathutils

def get_center(o):
    local_bbox_center = 0.125 * sum((Vector(b) for b in o.bound_box), Vector())
    global_bbox_center = o.matrix_world @ local_bbox_center

    return global_bbox_center

def clear():
    # Select objects by type
    for o in bpy.context.scene.objects:
        o.select_set(True)
    # Call the operator
    bpy.ops.object.delete()


def update_camera(camera, focus_point=mathutils.Vector((0.0, 0.0, 0.0)), distance=10.0):
    """
    Focus the camera to a focus point and place the camera at a specific distance from that
    focus point. The camera stays in a direct line with the focus point.

    :param camera: the camera object
    :type camera: bpy.types.object
    :param focus_point: the point to focus on (default=``mathutils.Vector((0.0, 0.0, 0.0))``)
    :type focus_point: mathutils.Vector
    :param distance: the distance to keep to the focus point (default=``10.0``)
    :type distance: float
    """
    looking_direction = camera.location - focus_point
    rot_quat = looking_direction.to_track_quat('Z', 'Y')

    camera.rotation_euler = rot_quat.to_euler()
    camera.location = camera.location + rot_quat @ mathutils.Vector((0.0, 0.0, distance))


clear()

scene = bpy.context.scene

#load and position mesh
filepath = '/path/to/file.STL'
bpy.ops.import_mesh.stl(filepath=filepath)
obj = bpy.context.object
bpy.context.scene.cursor.location = get_center(obj)
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
#bpy.context.scene.tool_settings.transform_pivot_point= 'CURSOR'
obj.location=(0,0,0)
obj.rotation_euler = (math.pi/2, 0, 0)

# Create and position lamp
lamp_data = bpy.data.lights.new(name="Lamp", type='POINT')
lamp_data.energy = 100
lamp_object = bpy.data.objects.new(name="Lamp", object_data=lamp_data)
bpy.context.collection.objects.link(lamp_object)
bpy.context.view_layer.objects.active = lamp_object
lamp_object.location = (0, 0, 30)

cam = bpy.data.cameras.new("Camera")
cam_ob = bpy.data.objects.new("Camera", cam)
bpy.context.collection.objects.link(cam_ob)
scene.camera = cam_ob

cam_ob.location=(70, 0, 0)
update_camera(cam_ob, Vector((0, 0, 0)))

scene.render.resolution_x = 800
scene.render.resolution_y = 600
bpy.context.scene.render.engine = 'CYCLES'
bpy.context.scene.render.image_settings.file_format='JPEG'
bpy.data.scenes['Scene'].render.filepath = '/path/to/image.jpg'
bpy.ops.render.render(write_still=True)

It loads the mesh, positions it on the center and places a light source above the object. The problem is that the light is not showing up on the rendered output, it is too dark, changing energy parameter on the lamp_data makes no difference. What might be wrong?



Related Questions


What does the 'light path' node do?

Updated April 03, 2015 21:23 PM

Disadvantages of importance sampling

Updated April 22, 2015 21:06 PM

Managing noise in a night scene

Updated May 04, 2015 00:06 AM


How can I make an object emit light in Cycles?

Updated June 11, 2015 22:06 PM