1
$\begingroup$

I am not very familiar with blender, more of a python programmer trying to use blender headless.

I want to import an STL to a scene and render an image from different angles. So I botched a code from here to do so, but I get a black picture.

This is my code:

import bpy
import os
from math import radians

context = bpy.context

#create a scene
scene = bpy.data.scenes.new("Scene")
camera_data = bpy.data.cameras.new("Camera")

camera = bpy.data.objects.new("Camera", camera_data)
camera.location = (-2.0, 3.0, 3.0)
camera.rotation_euler = ([radians(a) for a in (422.0, 0.0, 149)])
scene.objects.link(camera)

# do the same for lights etc
scene.update()

model_path = r"C:\Users\User\myfile.stl"
scene.camera = camera
path = os.path.join(models_path, model_path)
# make a new scene with cam and lights linked
context.screen.scene = scene
bpy.ops.scene.new(type='LINK_OBJECTS')
context.scene.name = model_path
cams = [c for c in context.scene.objects if c.type == 'CAMERA']
#import model
bpy.ops.import_mesh.stl(filepath=path)
for c in cams:
    context.scene.camera = c                                    
    print("Render ", model_path, context.scene.name, c.name)
    context.scene.render.filepath = r"C:\Users\User\myoutput.png"
    bpy.ops.render.render(write_still=True)

What am I doing wrong? Thanks!

$\endgroup$
  • $\begingroup$ Have you ever tried to turn on some lights? ;-) $\endgroup$ – Mechanic Jan 5 at 10:13

Your Answer

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

Browse other questions tagged or ask your own question.