math_vis: remove use of register_module
[blender-addons.git] / netrender / repath.py
blob348f7426db58cd80d44f6e8feb6937f525600281
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 sys, os
20 import subprocess
22 import bpy
24 DEBUG = False
26 from netrender.utils import *
29 def reset(job):
30 main_file = job.files[0]
32 job_full_path = main_file.filepath
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)
38 def update(job):
39 paths = []
41 main_file = job.files[0]
43 job_full_path = main_file.filepath
46 path, ext = os.path.splitext(job_full_path)
48 new_path = path + ".remap" + ext
50 original_path = main_file.original_path
52 # Disable for now. Partial repath should work anyway
53 #all = main_file.filepath != main_file.original_path
54 all = False
56 for rfile in job.files[1:]:
57 if all or rfile.original_path != rfile.filepath:
58 paths.append(rfile.original_path)
59 paths.append(rfile.filepath)
61 # Only update if needed
62 if paths:
63 process = subprocess.Popen(
64 [bpy.app.binary_path,
65 "-b",
66 "-y",
67 "-noaudio",
68 job_full_path,
69 "-P", __file__,
70 "--",
71 new_path,
72 original_path,
73 ] + paths,
74 stdout=sys.stdout,
75 stderr=subprocess.STDOUT,
77 process.wait()
79 os.renames(job_full_path, job_full_path + ".bak")
80 os.renames(new_path, job_full_path)
82 def process(original_path, paths):
83 if DEBUG: print("==========================================================")
84 original_directory = os.path.dirname(original_path)
85 path_map = {}
86 for i in range(0, len(paths), 2):
87 # special case for point cache
88 if paths[i].endswith(".bphys"):
89 path, filename = os.path.split(paths[i+1])
90 cache_name = filename.split("_")[0]
91 if DEBUG: print(cache_name, path)
92 path_map[cache_name] = path
93 # special case for fluids
94 elif paths[i].endswith(".bobj.gz"):
95 if DEBUG: print(os.path.split(paths[i])[0], os.path.split(paths[i+1])[0])
96 path_map[os.path.split(paths[i])[0]] = os.path.split(paths[i+1])[0]
97 else:
98 if DEBUG: print(paths[i], paths[i+1])
99 path_map[paths[i]] = paths[i+1]
101 if DEBUG: print("----------------------------------------------------------")
103 # TODO original paths aren't really the original path, they are the normalized path
104 # so we repath using the filenames only.
106 ###########################
107 # LIBRARIES
108 ###########################
109 for lib in bpy.data.libraries:
110 file_path = bpy.path.abspath(lib.filepath, start=original_directory)
111 new_path = path_map.get(file_path, None)
112 if DEBUG: print(file_path, new_path)
113 if new_path:
114 lib.filepath = new_path
116 ###########################
117 # IMAGES
118 ###########################
119 for image in bpy.data.images:
120 if image.source == "FILE" and not image.packed_file:
121 file_path = bpy.path.abspath(image.filepath, start=original_directory)
122 new_path = path_map.get(file_path, None)
123 if DEBUG: print(file_path, new_path)
124 if new_path:
125 image.filepath = new_path
128 ###########################
129 # FLUID + POINT CACHE
130 ###########################
131 def pointCacheFunc(object, owner, point_cache):
132 if not point_cache.use_disk_cache:
133 return
135 cache_name = cacheName(object, point_cache)
136 new_path = path_map.get(cache_name, None)
137 if DEBUG: print(cache_name, new_path)
138 if new_path:
139 point_cache.use_external = True
140 point_cache.filepath = new_path
141 point_cache.name = cache_name
143 def fluidFunc(object, modifier, cache_path):
144 fluid = modifier.settings
145 new_path = path_map.get(cache_path, None)
146 if new_path:
147 fluid.path = new_path
149 def multiresFunc(object, modifier, cache_path):
150 new_path = path_map.get(cache_path, None)
151 if new_path:
152 modifier.filepath = new_path
154 processObjectDependencies(pointCacheFunc, fluidFunc, multiresFunc)
155 if DEBUG: print("==========================================================")
158 if __name__ == "__main__":
159 try:
160 i = sys.argv.index("--")
161 except:
162 i = 0
164 if i:
165 new_path, original_path, *args = sys.argv[i+1:]
167 process(original_path, args)
169 bpy.ops.wm.save_as_mainfile(filepath=new_path, check_existing=False)