2
$\begingroup$

I am familiar with python but completely new to Blender. I want to create a python script to read an obj file and render it from a fixed camera with a fixed lighting and save it to an image. Is there an introductory script that I can see?

My constraints are that I am on a remote server without an X connection, so I can't launch a GUI.

I ran into this question, but it doesn't tell me how to set up lighting: Blender script import model and render it

Thanks!

$\endgroup$
  • $\begingroup$ As well as running a python script from the cli, you can start blender as a python console and dynamically type in commands, benders modules don't get auto imported this way so you still need to import bpy to get started. $\endgroup$ – sambler Apr 21 '17 at 6:24
2
$\begingroup$

Blender's object oriented approach uses a generic object for items in a scene with a varied data block that varies for each type of object. Using python we can create the lamp data and use it when adding an object to the scene, which can then be positioned where you want it. You could add a sun lamp and rotate it as desired, or if you wanted a spot light you could use a trackto constraint to point it at the object.

import bpy
from math import radians

# sun lamp
light_data = bpy.data.lamps.new('sun', 'SUN')
light_obj = bpy.data.objects.new('sun', light_data)
bpy.context.scene.objects.link(light_obj)

light_obj.location = (2.0,2.0,2.0)
light_obj.rotation_euler = (radians(10), radians(15), radians(20))

# spotlight
light_data = bpy.data.lamps.new('light', 'SPOT')
light_obj = bpy.data.objects.new('light', light_data)
bpy.context.scene.objects.link(light_obj)

light_obj.location = (2.0,2.0,2.0)
light_constr = light_obj.constraints.new('TRACK_TO')

light_constr.target = bpy.data.objects['Cube']
light_constr.track_axis = 'TRACK_NEGATIVE_Z'
light_constr.up_axis = 'UP_X'

light_data.spot_size = radians(25)
$\endgroup$
  • $\begingroup$ I am importing many .obj files sequentially. Very quickly, the import time increases significantly to the point that it takes minutes for each object (even the ones with low-complexity) to get imported. Could you please take a look at my question here and see if you can offer a solution? Appreciate it. $\endgroup$ – Amir Mar 4 '18 at 20:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.