math_vis: remove use of register_module
[blender-addons.git] / netrender / baking.py
blobf595a26bb55fafb8e38731eabe8100089057003c
1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
19 import bpy
20 import sys, subprocess, re
22 from netrender.utils import *
25 def commandToTask(command):
26 i = command.index("|")
27 ri = command.rindex("|")
28 return (command[:i], command[i+1:ri], command[ri+1:])
30 def taskToCommand(task):
31 return "|".join(task)
33 def bake(job, tasks):
34 main_file = job.files[0]
35 job_full_path = main_file.filepath
37 task_commands = []
38 for task in tasks:
39 task_commands.extend(task)
41 process = subprocess.Popen(
42 [bpy.app.binary_path,
43 "-b",
44 "-y",
45 "-noaudio",
46 job_full_path,
47 "-P", __file__,
48 "--",
49 ] + task_commands,
50 stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
53 return process
55 result_pattern = re.compile("BAKE FILE\[ ([0-9]+) \]: (.*)")
56 def resultsFromOuput(lines):
57 results = []
58 for line in lines:
59 match = result_pattern.match(line)
61 if match:
62 task_id = int(match.groups()[0])
63 task_filename = match.groups()[1]
65 results.append((task_id, task_filename))
67 return results
69 def bake_cache(obj, point_cache, task_index):
70 if point_cache.is_baked:
71 bpy.ops.ptcache.free_bake({"point_cache": point_cache})
73 point_cache.use_disk_cache = True
74 point_cache.use_external = False
76 bpy.ops.ptcache.bake({"point_cache": point_cache}, bake=True)
78 results = cache_results(obj, point_cache)
80 print()
82 for filename in results:
83 print("BAKE FILE[", task_index, "]:", filename)
86 def cache_results(obj, point_cache):
87 name = cacheName(obj, point_cache)
88 default_path = cachePath(bpy.data.filepath)
90 cache_path = bpy.path.abspath(point_cache.filepath) if point_cache.use_external else default_path
92 index = "%02i" % point_cache.index
94 if os.path.exists(cache_path):
95 pattern = re.compile(name + "_([0-9]+)_" + index + "\.bphys")
97 cache_files = []
99 for cache_file in sorted(os.listdir(cache_path)):
100 match = pattern.match(cache_file)
102 if match:
103 cache_files.append(os.path.join(cache_path, cache_file))
105 cache_files.sort()
107 return cache_files
109 return []
111 def process_generic(obj, index, task_index):
112 modifier = obj.modifiers[index]
113 point_cache = modifier.point_cache
114 bake_cache(obj, point_cache, task_index)
116 def process_smoke(obj, index, task_index):
117 modifier = obj.modifiers[index]
118 point_cache = modifier.domain_settings.point_cache
119 bake_cache(obj, point_cache, task_index)
121 def process_particle(obj, index, task_index):
122 psys = obj.particle_systems[index]
123 point_cache = psys.point_cache
124 bake_cache(obj, point_cache, task_index)
126 def process_paint(obj, index, task_index):
127 modifier = obj.modifiers[index]
128 for surface in modifier.canvas_settings.canvas_surfaces:
129 bake_cache(obj, surface.point_cache, task_index)
131 def process_null(obj, index, task_index):
132 raise ValueException("No baking possible with arguments: " + " ".join(sys.argv))
134 process_funcs = {}
135 process_funcs["CLOTH"] = process_generic
136 process_funcs["SOFT_BODY"] = process_generic
137 process_funcs["PARTICLE_SYSTEM"] = process_particle
138 process_funcs["SMOKE"] = process_smoke
139 process_funcs["DYNAMIC_PAINT"] = process_paint
141 if __name__ == "__main__":
142 try:
143 i = sys.argv.index("--")
144 except:
145 i = 0
147 if i:
148 task_args = sys.argv[i+1:]
149 for i in range(0, len(task_args), 3):
150 bake_type = task_args[i]
151 obj = bpy.data.objects[task_args[i+1]]
152 index = int(task_args[i+2])
154 process_funcs.get(bake_type, process_null)(obj, index, i)