-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
utils.py
127 lines (108 loc) · 4.17 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import os
import bpy
import bmesh
def simple_material(diffuse_color):
mat = bpy.data.materials.new('Material')
# Diffuse
if bpy.app.version < (2, 80, 0):
mat.diffuse_shader = 'LAMBERT'
mat.diffuse_intensity = 0.9
mat.diffuse_color = diffuse_color
else:
_diffuse_color = (diffuse_color + (0.9,))
mat.diffuse_color = _diffuse_color
# Specular
mat.specular_intensity = 0
return mat
def bmesh_to_object(bm, name='Object'):
mesh = bpy.data.meshes.new(name+'Mesh')
bm.to_mesh(mesh)
bm.free()
obj = bpy.data.objects.new(name, mesh)
if bpy.app.version < (2, 80, 0):
bpy.context.scene.objects.link(obj)
bpy.context.scene.update()
else:
bpy.context.collection.objects.link(obj)
bpy.context.view_layer.update()
return obj
def track_to_constraint(obj, target):
constraint = obj.constraints.new('TRACK_TO')
constraint.target = target
constraint.track_axis = 'TRACK_NEGATIVE_Z'
constraint.up_axis = 'UP_Y'
def create_target(origin=(0,0,0)):
tar = bpy.data.objects.new('Target', None)
if bpy.app.version < (2, 80, 0):
bpy.context.scene.objects.link(tar)
else:
bpy.context.collection.objects.link(tar)
tar.location = origin
return tar
def create_camera(origin=(0,0,0), target=None, lens=35, clip_start=0.1, clip_end=200, camera_type='PERSP', ortho_scale=6):
# Create object and camera
camera = bpy.data.cameras.new("Camera")
camera.lens = lens
camera.clip_start = clip_start
camera.clip_end = clip_end
camera.type = camera_type # 'PERSP', 'ORTHO', 'PANO'
if camera_type == 'ORTHO':
camera.ortho_scale = ortho_scale
# Link object to scene
obj = bpy.data.objects.new("CameraObj", camera)
obj.location = origin
if bpy.app.version < (2, 80, 0):
bpy.context.scene.objects.link(obj)
else:
bpy.context.collection.objects.link(obj)
bpy.context.scene.camera = obj # Make this the current camera
if target: track_to_constraint(obj, target)
return obj
def create_lamp(origin, type='POINT', energy=1, color=(1,1,1), target=None):
# Lamp types: 'POINT', 'SUN', 'SPOT', 'HEMI', 'AREA'
if bpy.app.version < (2, 80, 0):
bpy.ops.object.add(type='LAMP', location=origin)
obj = bpy.context.object
obj.data.type = type
obj.data.energy = energy
obj.data.color = color
else:
light_data = bpy.data.lights.new(name='New light', type=type)
obj = bpy.data.objects.new(name='New light', object_data=light_data)
obj.location = origin
light_data.energy = energy
light_data.color = color
if target: track_to_constraint(obj, target)
return obj
def render_to_folder(render_folder='render', render_name='render', res_x=800, res_y=800, res_percentage=100, animation=False, frame_end=None, render_opengl=False):
print('renderToFolder called')
print('render_folder : {}'.format(render_folder))
print('render_name : {}'.format(render_name))
scene = bpy.context.scene
scene.render.resolution_x = res_x
scene.render.resolution_y = res_y
scene.render.resolution_percentage = res_percentage
if frame_end:
scene.frame_end = frame_end
# Check if script is executed inside Blender
if (bpy.context.space_data is None) or render_opengl:
# Specify folder to save rendering and check if it exists
render_folder = os.path.join(os.getcwd(), render_folder)
if(not os.path.exists(render_folder)):
os.mkdir(render_folder)
if animation:
# Render animation
scene.render.filepath = os.path.join(render_folder,
render_name)
if render_opengl:
bpy.ops.render.opengl(animation=True, view_context=False)
else:
bpy.ops.render.render(animation=True)
else:
# Render still frame
scene.render.filepath = os.path.join(render_folder,
render_name + '.png')
if render_opengl:
bpy.ops.render.opengl(write_still=True, view_context=False)
else:
bpy.ops.render.render(write_still=True)