Welcome to mirror list, hosted at ThFree Co, Russian Federation.

baking.py « netrender - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f3a759a43358d31de8de13105c3d4a0c2b0a9a4a (plain)
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
# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

import bpy
import sys, subprocess, re

from netrender.utils import *


def commandToTask(command):
    i = command.index("|")
    ri = command.rindex("|")
    return (command[:i], command[i+1:ri], command[ri+1:])
    
def taskToCommand(task):
    return "|".join(task)
                    
def bake(job, tasks):
    main_file = job.files[0]
    job_full_path = main_file.filepath
    
    task_commands = []
    for task in tasks:
        task_commands.extend(task)

    process = subprocess.Popen(
        [bpy.app.binary_path,
         "-b",
         "-y",
         "-noaudio",
         job_full_path,
         "-P", __file__,
         "--",
         ] + task_commands,
        stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
        )

    return process

result_pattern = re.compile("BAKE FILE\[ ([0-9]+) \]: (.*)")
def resultsFromOuput(lines):
    results = []
    for line in lines:
        match = result_pattern.match(line)

        if match:
            task_id = int(match.groups()[0])
            task_filename = match.groups()[1]
            
            results.append((task_id, task_filename))
            
    return results

def bake_cache(obj, point_cache, task_index):
    if point_cache.is_baked:
        bpy.ops.ptcache.free_bake({"point_cache": point_cache})
        
    point_cache.use_disk_cache = True
    point_cache.use_external = False
    
    bpy.ops.ptcache.bake({"point_cache": point_cache}, bake=True)
    
    results = cache_results(obj, point_cache)
    
    print()
    
    for filename in results:
        print("BAKE FILE[", task_index, "]:", filename)
  

def cache_results(obj, point_cache):
    name = cacheName(obj, point_cache)
    default_path = cachePath(bpy.data.filepath)

    cache_path = bpy.path.abspath(point_cache.filepath) if point_cache.use_external else default_path
    
    index = "%02i" % point_cache.index

    if os.path.exists(cache_path):
        pattern = re.compile(name + "_([0-9]+)_" + index + "\.bphys")

        cache_files = []

        for cache_file in sorted(os.listdir(cache_path)):
            match = pattern.match(cache_file)

            if match:
                cache_files.append(os.path.join(cache_path, cache_file))

        cache_files.sort()
        
        return cache_files
    
    return []

def process_generic(obj, index, task_index):
    modifier = obj.modifiers[index]
    point_cache = modifier.point_cache
    bake_cache(obj, point_cache, task_index)

def process_smoke(obj, index, task_index):
    modifier = obj.modifiers[index]
    point_cache = modifier.domain_settings.point_cache
    bake_cache(obj, point_cache, task_index)

def process_particle(obj, index, task_index):
    psys = obj.particle_systems[index]
    point_cache = psys.point_cache
    bake_cache(obj, point_cache, task_index)

def process_paint(obj, index, task_index):
    modifier = obj.modifiers[index]
    for surface in modifier.canvas_settings.canvas_surfaces:
        bake_cache(obj, surface.point_cache, task_index)

def process_null(obj, index, task_index):
    raise ValueException("No baking possible with arguments: " + " ".join(sys.argv))

process_funcs = {}
process_funcs["CLOTH"] = process_generic
process_funcs["SOFT_BODY"] = process_generic
process_funcs["PARTICLE_SYSTEM"] = process_particle
process_funcs["SMOKE"] = process_smoke
process_funcs["DYNAMIC_PAINT"] = process_paint

if __name__ == "__main__":
    try:
        i = sys.argv.index("--")
    except:
        i = 0
    
    if i:
        task_args = sys.argv[i+1:]
        for i in range(0, len(task_args), 3):
            bake_type = task_args[i]
            obj = bpy.data.objects[task_args[i+1]]
            index = int(task_args[i+2])
            
            process_funcs.get(bake_type, process_null)(obj, index, i)