The script
blender --background --python myscript.py
Let’s walk through what myscript.py
could look like:
'Cube'], do_unlink = True) bpy.data.objects.remove(bpy.data.objects[
=1000,location=(0, 0, 0), scale=(1, 1, 1)) bpy.ops.mesh.primitive_plane_add(size
Importing Mesh
def create_bunny():
# Load the mesh
=os.getcwd()+"/stanford_bunny.obj")
bpy.ops.import_scene.obj(filepath= bpy.data.objects["stanford_bunny"]
ob = (10,10,10)
ob.scale = (1,0,-0.35)
ob.location = 'Bunny'
ob.name
#Perform subdivision
= ob.modifiers.new('Subsurf', 'SUBSURF')
bevel_mod = 3 bevel_mod.render_levels
Materials
def create_material(object_name,material_name):
= bpy.data.materials.new(name=material_name)
mat = mat
bpy.data.objects[object_name].active_material
= True
mat.use_nodes = mat.node_tree.nodes
nodes return(nodes)
def create_ground_plane_material(object_name,material_name):
= create_material(object_name,material_name)
nodes
"Principled BSDF"].inputs['Base Color'].default_value = (0.7,0.7,0.7,1)
nodes["Principled BSDF"].inputs['Specular'].default_value = 1
nodes["Principled BSDF"].inputs['Roughness'].default_value = 0.1
nodes[
"Plane","Plane_material") create_ground_plane_material(
def create_bunny_material(object_name,material_name):
= create_material(object_name,material_name)
nodes
"Principled BSDF"].inputs['Base Color'].default_value = (0.603828, 1, 0.707399, 1)
nodes["Principled BSDF"].inputs['Roughness'].default_value = 0.1
nodes["Principled BSDF"].inputs['IOR'].default_value = 1.5
nodes["Principled BSDF"].inputs['Transmission'].default_value = 1
nodes["Principled BSDF"].inputs['Transmission Roughness'].default_value = 0.75
nodes[
"Bunny","Bunny_Material") create_bunny_material(
Lights, Camera, Render!
def configure_light():
"Light"].data.type = 'AREA'
bpy.data.objects["Light"].scale = (10,10,1)
bpy.data.objects["Light"].location = (0,0,6)
bpy.data.objects["Light"].rotation_euler = (0,0,0)
bpy.data.objects[
def configure_camera():
"Camera"].location = (0.7, -4, 3)
bpy.data.objects["Camera"].rotation_euler = (np.radians(60),0,0)
bpy.data.objects[
def configure_render():
= 'CYCLES'
bpy.context.scene.render.engine = os.getcwd()+"/render.png"
bpy.context.scene.render.filepath = 1600
bpy.context.scene.render.resolution_x = 1200
bpy.context.scene.render.resolution_y = 2560
bpy.context.scene.cycles.samples
configure_light()
configure_camera()
configure_render()
=True) bpy.ops.render.render(write_still