rna data path names which are more likely to break animations.
[blender-staging.git] / release / scripts / io / netrender / repath.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
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.
7 #
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.
12 #
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.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 import sys, os
20 import subprocess
21
22 import bpy
23
24 from netrender.utils import *
25 import netrender.model
26
27 BLENDER_PATH = sys.argv[0]
28
29 def reset(job):
30     main_file = job.files[0]
31     
32     job_full_path = main_file.filepath
33
34     if os.path.exists(job_full_path + ".bak"):
35         os.remove(job_full_path) # repathed file
36         os.renames(job_full_path + ".bak", job_full_path)
37
38 def update(job):
39     paths = []
40     
41     main_file = job.files[0]
42     
43     job_full_path = main_file.filepath
44
45         
46     path, ext = os.path.splitext(job_full_path)
47     
48     new_path = path + ".remap" + ext 
49     
50     # Disable for now. Partial repath should work anyway
51     #all = main_file.filepath != main_file.original_path
52     all = False 
53     
54     for rfile in job.files[1:]:
55         if all or rfile.original_path != rfile.filepath:
56             paths.append(rfile.original_path)
57             paths.append(rfile.filepath)
58     
59     # Only update if needed
60     if paths:        
61         process = subprocess.Popen([BLENDER_PATH, "-b", "-noaudio", job_full_path, "-P", __file__, "--", new_path] + paths, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
62         process.wait()
63         
64         os.renames(job_full_path, job_full_path + ".bak")
65         os.renames(new_path, job_full_path)
66
67 def process(paths):
68     def processPointCache(point_cache):
69         point_cache.use_external = False
70
71     def processFluid(fluid):
72         new_path = path_map.get(fluid.filepath, None)
73         if new_path:
74             fluid.path = new_path
75     
76     path_map = {}
77     for i in range(0, len(paths), 2):
78         # special case for point cache
79         if paths[i].endswith(".bphys"):
80             pass # Don't need them in the map, they all use the default external path
81             # NOTE: This is probably not correct all the time, need to be fixed.
82         # special case for fluids
83         elif paths[i].endswith(".bobj.gz"):
84             path_map[os.path.split(paths[i])[0]] = os.path.split(paths[i+1])[0]
85         else:
86             path_map[os.path.split(paths[i])[1]] = paths[i+1]
87             
88     # TODO original paths aren't really the orignal path (they are the normalized path
89     # so we repath using the filenames only. 
90     
91     ###########################
92     # LIBRARIES
93     ###########################
94     for lib in bpy.data.libraries:
95         file_path = bpy.path.abspath(lib.filepath)
96         new_path = path_map.get(os.path.split(file_path)[1], None)
97         if new_path:
98             lib.filepath = new_path
99
100     ###########################
101     # IMAGES
102     ###########################
103     for image in bpy.data.images:
104         if image.source == "FILE" and not image.packed_file:
105             file_path = bpy.path.abspath(image.filepath)
106             new_path = path_map.get(os.path.split(file_path)[1], None)
107             if new_path:
108                 image.filepath = new_path
109             
110
111     ###########################
112     # FLUID + POINT CACHE
113     ###########################
114     for object in bpy.data.objects:
115         for modifier in object.modifiers:
116             if modifier.type == 'FLUID_SIMULATION' and modifier.settings.type == "DOMAIN":
117                 processFluid(settings)
118             elif modifier.type == "CLOTH":
119                 processPointCache(modifier.point_cache)
120             elif modifier.type == "SOFT_BODY":
121                 processPointCache(modifier.point_cache)
122             elif modifier.type == "SMOKE" and modifier.smoke_type == "TYPE_DOMAIN":
123                 processPointCache(modifier.domain_settings.point_cache_low)
124                 if modifier.domain_settings.use_high_resolution:
125                     processPointCache(modifier.domain_settings.point_cache_high)
126             elif modifier.type == "MULTIRES" and modifier.is_external:
127                 file_path = bpy.path.abspath(modifier.filepath)
128                 new_path = path_map.get(file_path, None)
129                 if new_path:
130                     modifier.filepath = new_path
131
132         # particles modifier are stupid and don't contain data
133         # we have to go through the object property
134         for psys in object.particle_systems:
135             processPointCache(psys.point_cache)
136                 
137
138 if __name__ == "__main__":
139     try:
140         i = sys.argv.index("--")
141     except:
142         i = 0
143     
144     if i:
145         new_path = sys.argv[i+1]
146         args = sys.argv[i+2:]
147         
148         process(args)
149         
150         bpy.ops.wm.save_as_mainfile(filepath=new_path, check_existing=False)