Skip to content

Instantly share code, notes, and snippets.

@enric1994
Last active October 10, 2023 15:59
Show Gist options
  • Save enric1994/40e6e70a70930782ba0d998c01d5ac48 to your computer and use it in GitHub Desktop.
Save enric1994/40e6e70a70930782ba0d998c01d5ac48 to your computer and use it in GitHub Desktop.
Introduction to Blender's Python API
"""
Required files:
https://github.com/mrdoob/three.js/blob/c8549a524bde688e8a55b32faa0a69b3308c4034/examples/models/stl/binary/pr2_head_tilt.stl
https://sipi.usc.edu/database/download.php?vol=misc&img=4.2.03
https://sipi.usc.edu/database/database.php?volume=misc&image=5#top
"""
import bpy
base_path = '/Users/enric/Downloads/'
model_path = base_path + 'pr2_head_tilt.stl'
texture_path = base_path + '4.2.03.tiff'
background_path = base_path + '4.1.05.tiff'
bpy.data.objects["Cube"].hide = True
bpy.ops.import_mesh.stl(filepath=model_path)
model=bpy.data.objects["Pr2 Head Tilt"]
model.scale=(10,10,10)
bpy.ops.object.lamp_add(type="SUN")
bpy.ops.object.lamp_add(type="HEMI")
bpy.data.objects["Hemi"].rotation_euler = (0.78, 0.78, 0.78)
mat = bpy.data.materials.new(name="material")
model.data.materials.append(mat)
tex = bpy.data.textures.new("texture", "IMAGE")
slot = mat.texture_slots.add()
slot.texture = tex
bpy.data.images.new("image", 0, 0)
bpy.data.images["image"].source = "FILE"
tex.image = bpy.data.images["image"]
tex.image.filepath = texture_path
mat.texture_slots[0].texture_coords = "ORCO"
bpy.ops.image.reload()
cube = bpy.data.objects["Cube"]
cube.hide = False
cube.scale = (8, 8, 0.1)
cube.location = (-2.5, 2, -1)
cube.rotation_euler = (1.22, 0, 0.78)
mat_bg = bpy.data.materials.new(name="material_bg")
cube.material_slots[0].material = mat_bg
tex_bg = bpy.data.textures.new("texture_bg", "IMAGE")
slot = mat_bg.texture_slots.add()
slot.texture = tex_bg
bpy.data.images.new("image_bg", 0, 0)
bpy.data.images["image_bg"].source = "FILE"
tex_bg.image = bpy.data.images["image_bg"]
tex_bg.image.filepath = background_path
mat_bg.texture_slots[0].texture_coords = "ORCO"
bpy.ops.image.reload()
bpy.data.scenes["Scene"].render.resolution_percentage = 100
bpy.data.scenes["Scene"].render.resolution_x = 512
bpy.data.scenes["Scene"].render.resolution_y = 512
bpy.context.scene.render.filepath = base_path + "first_render.png"
bpy.ops.render.render(write_still=True)
cube.hide = True
cube.hide_render = True
bpy.data.worlds["World"].horizon_color = (0,0,0)
mat_mask = bpy.data.materials.new(name="material_mask")
mat_mask.diffuse_color = (1,1,1)
mat_mask.use_shadeless = True
model.material_slots[0].material = mat_mask
bpy.context.scene.render.filepath = base_path + "mask.png"
bpy.ops.render.render(write_still=True)
model.rotation_euler = (1.5, 2, -1)
model.location = (2, 0.5, 2)
model.scale = (7, 12, 8)
bpy.context.scene.objects.active = model
bpy.ops.object.modifier_add(type="CAST")
model.modifiers["Cast"].factor = 1
bpy.data.cameras["Camera"].lens = 40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment