2
$\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$
3
  • $\begingroup$ Have you ever tried to turn on some lights? ;-) $\endgroup$
    – Mechanic
    Commented Jan 5, 2019 at 10:13
  • $\begingroup$ Did you ever find a solution to this? $\endgroup$
    – Raleigh L.
    Commented Oct 10, 2023 at 0:24
  • $\begingroup$ @RaleighL. Hi there, it's been some time, but I think I was missing lights like Mechanic suggested $\endgroup$
    – AlonMln
    Commented Oct 11, 2023 at 5:14

0

You must log in to answer this question.

Browse other questions tagged .