-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlender.py
203 lines (159 loc) · 4.59 KB
/
Blender.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import bpy
_data_switch = {
#'LAMP': bpy.data.lights,
'MESH': bpy.data.meshes,
'CAMERA': bpy.data.cameras,
'CURVE': bpy.data.curves,
'OBJECT': bpy.data.objects,
'MATERIALS': bpy.data.materials,
'FONT': bpy.data.fonts,
#'SURFACE': bpy.data.surfaces
}
resolution = 1
auto_animate = True
scene = bpy.context.scene
data = bpy.data
#objects = bpy.context.scene.objects
objects = bpy.context.collection.objects
materials = bpy.data.materials
window_manager = bpy.context.window_manager
context = bpy.context
active_object = bpy.context.active_object
def getFrame():
return scene.frame_current
def setFrame(val):
assert isinstance(val, int)
scene.frame_set(val)
def nextFrame(off = 1):
setFrame(getFrame() + off)
def prevFrame(off):
setFrame(getFrame() - off)
def select_all(value = False):
for obj in objects:
obj.select = value
def clear_all(do_unlink=False):
for bpy_data_iter in _data_switch.values():
for id_data in bpy_data_iter:
bpy_data_iter.remove(id_data, do_unlink=do_unlink)
def clear_data(data=None, do_unlink=False):
if data == None:
clear_all()
elif isinstance(data, str):
clear_data(_data_switch[data], do_unlink)
else:
for id_data in data:
print("Remove", id_data, "from", data)
data.remove(id_data, do_unlink=do_unlink)
def clear_objects(pattern = None, exclude_type = None):
if not exclude_type:
exclude_type = ('LAMP', 'CAMERA', 'MATERIALS')
if pattern:
import re
prog = re.compile(pattern)
for obj in scene.objects:
if obj.type not in exclude_type:
if not pattern or prog.match(obj.name):
print("Remove ", obj.type, obj)
#scene.objects.unlink(obj)
if obj.data != None:
_data_switch[obj.type].remove(obj.data, do_unlink=True)
elif obj != None:
try:
scene.objects.unlink(obj)
except: pass
#del obj
#bpy.data.objects.remove(obj.data, do_unlink=True)
#bpy.data.objects.remove(obj.data)
"""
for k, v in _data_switch.items():
if k not in exclude_type:
clear_data(v, True)
"""
def clear_materials(do_unlink=False):
for mat in materials:
try:
bpy.data.materials.remove(mat, do_unlink=do_unlink)
except:
print("Can not clear material:", mat.name)
pass
def save_blend(save_path):
f = open(save_path, 'w')
f.close()
bpy.ops.wm.save_as_mainfile(filepath=save_path)
def render_image(save_path, resolution=None):
scene.render.filepath = save_path
if resolution is not None:
x,y = resolution
scene.render.resolution_x, scene.render.resolution_y = x, y
bpy.ops.render.render( write_still=True )
def get_max_threads():
""" Returns the number of available threads on a posix/win based system """
import os, sys
if sys.platform == 'win32':
return (int)(os.environ['NUMBER_OF_PROCESSORS'])
else:
return (int)(os.popen('grep -c cores /proc/cpuinfo').read())
__lib_config = {
'resolution': 1,
'auto_animate': True,
'auto_shade_smooth': True,
'nthreads': 1,
}
def get_lib_config(name = None):
global __lib_config
if name is None or not name: return __lib_config
elif name not in __lib_config: return None
else: return __lib_config[name]
def set_lib_config(name, value):
global __lib_config
__lib_config[name] = value
def set_nthreads(n):
global __nthreads
maxthreads = get_max_threads()
if n > maxthreads:
print("WARNING: setting %d threads exceeds number of available %d cores" % (n, maxthreads))
set_lib_config('nthreads', int(n))
def get_nthreads():
global __nthreads
n = get_lib_config('nthreads')
return n if n > 0 else get_max_threads()
def set_auto_shade_smooth(n):
global __auto_shade_smooth
set_lib_config('auto_shade_smooth', bool(n))
def auto_shade_smooth(value = None):
if value is None: return bool(get_lib_config('auto_shade_smooth'))
else: set_lib_config('auto_shade_smooth', bool(value))
def auto_animate(value = None):
if value is None: return bool(get_lib_config('auto_animate'))
else: set_lib_config('auto_animate', bool(value))
def nthreads(value = None):
if value is None: return int(get_lib_config('nthreads'))
else: set_nthreads(value)
def BlenderInit(frame=1, **kw):
setFrame(frame)
clear_objects()
clear_materials()
for k,v in kw.items():
set_lib_config(k, v)
def recurLayerCollection(layerColl, collName):
found = None
if (layerColl.name == collName):
return layerColl
for layer in layerColl.children:
found = recurLayerCollection(layer, collName)
if found:
return found
"""
@staticmethod
def getObjects():
return Blender.scene.objects
objects = property(getObjects)
@staticmethod
def getData():
return bpy.data
data = property(getData)
@staticmethod
def getMaterials(self):
return bpy.data.materials
materials = property(getMaterials)
"""