2014-10-15 16:35:18 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2014-10-14 19:14:37 +02:00
|
|
|
# ***** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
#
|
|
|
|
# ***** END GPL LICENCE BLOCK *****
|
2014-10-14 19:09:17 +02:00
|
|
|
|
2014-10-15 16:35:18 +02:00
|
|
|
VERBOSE = True
|
|
|
|
|
|
|
|
|
2014-10-14 19:09:17 +02:00
|
|
|
class FilePath:
|
|
|
|
"""
|
|
|
|
Tiny filepath class to hide blendfile.
|
|
|
|
"""
|
|
|
|
__slots__ = (
|
|
|
|
"block",
|
|
|
|
"path",
|
2014-10-15 16:35:18 +02:00
|
|
|
# path may be relative to basepath
|
|
|
|
"basedir",
|
2014-10-14 19:09:17 +02:00
|
|
|
)
|
|
|
|
|
2014-10-15 16:35:18 +02:00
|
|
|
def __init__(self, block, path, basedir):
|
2014-10-14 19:09:17 +02:00
|
|
|
self.block = block
|
|
|
|
self.path = path
|
2014-10-15 16:35:18 +02:00
|
|
|
self.basedir = basedir
|
2014-10-14 19:09:17 +02:00
|
|
|
|
|
|
|
# --------
|
|
|
|
# filepath
|
|
|
|
#
|
|
|
|
@property
|
|
|
|
def filepath(self):
|
|
|
|
return self.block[self.path]
|
|
|
|
|
|
|
|
@filepath.setter
|
|
|
|
def filepath(self, filepath):
|
|
|
|
self.block[self.path] = filepath
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------
|
|
|
|
# Main function to visit paths
|
|
|
|
@staticmethod
|
2014-10-15 16:35:18 +02:00
|
|
|
def visit_from_blend(
|
|
|
|
filepath,
|
|
|
|
|
|
|
|
# never modify the blend
|
|
|
|
readonly=True,
|
|
|
|
# callback that creates a temp file and returns its path.
|
|
|
|
temp_remap_cb=None,
|
|
|
|
|
|
|
|
# recursive options
|
|
|
|
recursive=False,
|
|
|
|
# list of ID block names we want to load, or None to load all
|
|
|
|
block_codes=None,
|
|
|
|
# root when we're loading libs indirectly
|
|
|
|
rootdir=None,
|
|
|
|
level=0,
|
2014-10-15 19:44:10 +02:00
|
|
|
# dict of id's used so we don't follow these links again
|
|
|
|
# prevents cyclic references too!
|
|
|
|
# {lib_path: set([block id's ...])}
|
|
|
|
lib_visit=None,
|
2014-10-15 16:35:18 +02:00
|
|
|
):
|
|
|
|
|
2014-10-14 19:09:17 +02:00
|
|
|
import os
|
|
|
|
|
2014-10-15 16:35:18 +02:00
|
|
|
if VERBOSE:
|
|
|
|
indent_str = " " * level
|
|
|
|
print(indent_str + "Opening:", filepath)
|
|
|
|
print(indent_str + "... blocks:", block_codes)
|
|
|
|
|
|
|
|
|
2014-10-14 19:09:17 +02:00
|
|
|
basedir = os.path.dirname(os.path.abspath(filepath))
|
2014-10-15 16:35:18 +02:00
|
|
|
if rootdir is None:
|
|
|
|
rootdir = basedir
|
|
|
|
|
|
|
|
if recursive and (level > 0) and (block_codes is not None):
|
2014-10-16 20:01:22 +02:00
|
|
|
# prevent from expanding the
|
|
|
|
# same datablock more then once
|
2014-10-15 16:35:18 +02:00
|
|
|
expand_codes = set()
|
2014-10-16 20:01:22 +02:00
|
|
|
|
|
|
|
def expand_codes_add_test(block):
|
|
|
|
len_prev = len(expand_codes)
|
|
|
|
expand_codes.add(block[b'id.name'])
|
|
|
|
return (len_prev != len(expand_codes))
|
|
|
|
|
|
|
|
def block_expand(block, code):
|
|
|
|
if expand_codes_add_test(block):
|
|
|
|
yield block
|
|
|
|
|
|
|
|
fn = ExpandID.expand_funcs.get(code)
|
|
|
|
if fn is not None:
|
|
|
|
for sub_block in fn(block):
|
|
|
|
if sub_block is not None:
|
|
|
|
yield from block_expand(sub_block, sub_block.code)
|
|
|
|
else:
|
|
|
|
yield block
|
2014-10-15 16:35:18 +02:00
|
|
|
else:
|
|
|
|
expand_codes = None
|
2014-10-16 20:01:22 +02:00
|
|
|
def block_expand(block, code):
|
|
|
|
yield block
|
2014-10-15 16:35:18 +02:00
|
|
|
|
|
|
|
if block_codes is None:
|
2014-10-16 20:01:22 +02:00
|
|
|
def iter_blocks_id(code):
|
|
|
|
return blend.find_blocks_from_code(code)
|
2014-10-15 16:35:18 +02:00
|
|
|
else:
|
2014-10-16 20:01:22 +02:00
|
|
|
def iter_blocks_id(code):
|
|
|
|
for block in blend.find_blocks_from_code(code):
|
|
|
|
if block[b'id.name'] in block_codes:
|
|
|
|
yield from block_expand(block, code)
|
2014-10-15 16:35:18 +02:00
|
|
|
|
|
|
|
if expand_codes is None:
|
|
|
|
iter_blocks_lib = lambda: blend.find_blocks_from_code(b'ID')
|
|
|
|
else:
|
|
|
|
iter_blocks_lib = lambda: (block
|
|
|
|
for block in blend.find_blocks_from_code(b'ID')
|
|
|
|
if block[b'name'] in expand_codes)
|
|
|
|
|
|
|
|
|
|
|
|
if temp_remap_cb is not None:
|
|
|
|
filepath_tmp = temp_remap_cb(filepath)
|
|
|
|
else:
|
|
|
|
filepath_tmp = filepath
|
2014-10-14 19:09:17 +02:00
|
|
|
|
|
|
|
import blendfile
|
2014-10-15 16:35:18 +02:00
|
|
|
blend = blendfile.open_blend(filepath_tmp, "rb" if readonly else "r+b")
|
|
|
|
|
|
|
|
for block in iter_blocks_id(b'IM'):
|
|
|
|
yield FilePath(block, b'name', basedir), rootdir
|
|
|
|
|
2014-10-16 20:01:22 +02:00
|
|
|
# follow links (loop over non-filepath ID's)
|
|
|
|
if recursive:
|
|
|
|
for block in iter_blocks_id(b'GR'):
|
2014-10-16 20:26:02 +02:00
|
|
|
yield from FilePath.from_block(block, basedir, rootdir)
|
2014-10-16 20:01:22 +02:00
|
|
|
|
2014-10-15 16:35:18 +02:00
|
|
|
if recursive:
|
|
|
|
# look into libraries
|
|
|
|
lib_all = {}
|
2014-10-14 19:09:17 +02:00
|
|
|
|
2014-10-15 16:35:18 +02:00
|
|
|
for block in iter_blocks_lib():
|
|
|
|
lib_id = block[b'lib']
|
|
|
|
lib = blend.find_block_from_offset(lib_id)
|
|
|
|
lib_path = lib[b'name']
|
2014-10-14 19:09:17 +02:00
|
|
|
|
2014-10-15 16:35:18 +02:00
|
|
|
# import IPython; IPython.embed()
|
2014-10-14 19:09:17 +02:00
|
|
|
|
2014-10-15 16:35:18 +02:00
|
|
|
# get all data needed to read the blend files here (it will be freed!)
|
|
|
|
# lib is an address at the moment, we only use as a way to group
|
2014-10-15 19:44:10 +02:00
|
|
|
lib_all.setdefault(lib_path, set()).add(block[b'name'])
|
2014-10-15 16:35:18 +02:00
|
|
|
|
|
|
|
# do this after, incase we mangle names above
|
|
|
|
for block in iter_blocks_id(b'LI'):
|
2014-10-16 20:26:02 +02:00
|
|
|
yield from FilePath.from_block(block, basedir, rootdir)
|
2014-10-14 19:09:17 +02:00
|
|
|
|
|
|
|
blend.close()
|
|
|
|
|
2014-10-15 16:35:18 +02:00
|
|
|
# ----------------
|
|
|
|
# Handle Recursive
|
|
|
|
if recursive:
|
|
|
|
# now we've closed the file, loop on other files
|
|
|
|
for lib_path, lib_block_codes in lib_all.items():
|
|
|
|
lib_path_abs = utils.abspath(lib_path, basedir)
|
2014-10-15 19:44:10 +02:00
|
|
|
|
|
|
|
# if we visited this before,
|
|
|
|
# check we don't follow the same links more than once
|
|
|
|
lib_block_codes_existing = lib_visit.setdefault(lib_path_abs, set())
|
|
|
|
lib_block_codes -= lib_block_codes_existing
|
|
|
|
# don't touch them again
|
|
|
|
lib_block_codes_existing.update(lib_block_codes)
|
|
|
|
|
|
|
|
# import IPython; IPython.embed()
|
2014-10-15 16:35:18 +02:00
|
|
|
if VERBOSE:
|
|
|
|
print((indent_str + " "), "Library: ", filepath, " -> ", lib_path_abs, sep="")
|
|
|
|
print((indent_str + " "), lib_block_codes)
|
|
|
|
yield from FilePath.visit_from_blend(
|
|
|
|
lib_path_abs,
|
|
|
|
readonly=readonly,
|
|
|
|
temp_remap_cb=temp_remap_cb,
|
|
|
|
recursive=True,
|
2014-10-15 19:44:10 +02:00
|
|
|
block_codes=lib_block_codes,
|
2014-10-15 16:35:18 +02:00
|
|
|
rootdir=rootdir,
|
|
|
|
level=level + 1,
|
|
|
|
)
|
|
|
|
|
2014-10-16 20:26:02 +02:00
|
|
|
# ------------------------------------------------------------------------
|
|
|
|
# Direct filepaths from Blocks
|
|
|
|
#
|
|
|
|
# (no expanding or following references)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_block(block, basedir, rootdir):
|
|
|
|
print(block)
|
|
|
|
assert(block.code != b'DATA')
|
|
|
|
fn = FilePath._from_block_dict.get(block.code)
|
|
|
|
if fn is not None:
|
|
|
|
yield from fn(block, basedir, rootdir)
|
|
|
|
|
|
|
|
def _from_block_IM(block, basedir, rootdir):
|
|
|
|
yield FilePath(block, b'name', basedir), rootdir
|
|
|
|
|
|
|
|
def _from_block_LI(block, basedir, rootdir):
|
|
|
|
yield FilePath(block, b'name', basedir), rootdir
|
|
|
|
|
|
|
|
_from_block_dict = {
|
|
|
|
b'IM': _from_block_IM,
|
|
|
|
b'LI': _from_block_LI,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-14 19:09:17 +02:00
|
|
|
|
2014-10-16 20:01:22 +02:00
|
|
|
class bf_utils:
|
|
|
|
@staticmethod
|
|
|
|
def iter_ListBase(block):
|
|
|
|
while block:
|
|
|
|
yield block
|
|
|
|
block = block.file.find_block_from_offset(block[b'next'])
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# ID Expand
|
|
|
|
|
|
|
|
class ExpandID:
|
|
|
|
# fake module
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
raise RuntimeError("%s should not be instantiated" % cls)
|
|
|
|
|
|
|
|
def expand_OB(block):
|
|
|
|
yield block.get_pointer(b'data')
|
|
|
|
def expand_ME(block):
|
|
|
|
return
|
|
|
|
yield none
|
|
|
|
def expand_MA(block):
|
2014-10-16 20:26:02 +02:00
|
|
|
print(block)
|
2014-10-16 20:01:22 +02:00
|
|
|
return
|
|
|
|
yield none
|
|
|
|
def expand_TE(block):
|
|
|
|
return
|
|
|
|
yield none
|
|
|
|
def expand_GR(block):
|
|
|
|
sdna_index_GroupObject = block.file.sdna_index_from_id[b'GroupObject']
|
|
|
|
for gobj in bf_utils.iter_ListBase(block.get_pointer(b'gobject.first')):
|
|
|
|
yield gobj.get_pointer(b'ob', sdna_index_refine=sdna_index_GroupObject)
|
|
|
|
|
|
|
|
expand_funcs = {
|
|
|
|
b'OB': expand_OB,
|
|
|
|
b'ME': expand_ME,
|
|
|
|
b'MA': expand_MA,
|
|
|
|
b'TE': expand_TE,
|
|
|
|
b'GR': expand_GR,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Packing Utility
|
|
|
|
|
|
|
|
|
2014-10-14 19:09:17 +02:00
|
|
|
class utils:
|
|
|
|
# fake module
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
@staticmethod
|
2014-10-15 16:35:18 +02:00
|
|
|
def abspath(path, start, library=None):
|
2014-10-14 19:09:17 +02:00
|
|
|
import os
|
2014-10-15 16:35:18 +02:00
|
|
|
if path.startswith(b'//'):
|
2014-10-14 19:09:17 +02:00
|
|
|
# if library:
|
|
|
|
# start = os.path.dirname(abspath(library.filepath))
|
|
|
|
return os.path.join(start, path[2:])
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
def pack(blendfile_src, blendfile_dst):
|
2014-10-15 18:37:47 +02:00
|
|
|
|
|
|
|
# Internal details:
|
|
|
|
# - we copy to a temp path before operating on the blend file
|
|
|
|
# so we can modify in-place.
|
|
|
|
# - temp files are only created once, (if we never touched them before),
|
|
|
|
# this way, for linked libraries - a single blend file may be used
|
|
|
|
# multiple times, each access will apply new edits ontop of the old ones.
|
2014-10-15 19:44:10 +02:00
|
|
|
# - we track which libs we have touched (using 'lib_visit' arg),
|
|
|
|
# this means that the same libs wont be touched many times to modify the same data
|
|
|
|
# also prevents cyclic loops from crashing.
|
2014-10-15 18:37:47 +02:00
|
|
|
|
|
|
|
|
2014-10-14 19:09:17 +02:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
2014-10-15 18:37:47 +02:00
|
|
|
path_temp_files = set()
|
|
|
|
path_copy_files = set()
|
2014-10-14 19:09:17 +02:00
|
|
|
|
2014-10-15 16:35:18 +02:00
|
|
|
def temp_remap_cb(filepath):
|
|
|
|
"""
|
|
|
|
Create temp files in the destination path.
|
|
|
|
"""
|
|
|
|
filepath_tmp = os.path.join(base_dir_dst, os.path.basename(filepath)) + b'@'
|
2014-10-15 18:37:47 +02:00
|
|
|
# only overwrite once (allows us to )
|
|
|
|
if filepath_tmp not in path_temp_files:
|
|
|
|
shutil.copy(filepath, filepath_tmp)
|
|
|
|
path_temp_files.add(filepath_tmp)
|
2014-10-15 16:35:18 +02:00
|
|
|
return filepath_tmp
|
|
|
|
|
2014-10-14 19:09:17 +02:00
|
|
|
base_dir_src = os.path.dirname(blendfile_src)
|
|
|
|
base_dir_dst = os.path.dirname(blendfile_dst)
|
|
|
|
|
2014-10-15 19:44:10 +02:00
|
|
|
lib_visit = {}
|
|
|
|
|
2014-10-15 16:35:18 +02:00
|
|
|
for fp, rootdir in FilePath.visit_from_blend(
|
|
|
|
blendfile_src,
|
|
|
|
readonly=False,
|
|
|
|
temp_remap_cb=temp_remap_cb,
|
2014-10-15 19:44:10 +02:00
|
|
|
recursive=True,
|
|
|
|
lib_visit=lib_visit):
|
2014-10-15 16:35:18 +02:00
|
|
|
|
2014-10-14 19:09:17 +02:00
|
|
|
# assume the path might be relative
|
|
|
|
path_rel = fp.filepath
|
|
|
|
path_base = path_rel.split(b"\\")[-1].split(b"/")[-1]
|
2014-10-15 16:35:18 +02:00
|
|
|
path_src = utils.abspath(path_rel, fp.basedir)
|
2014-10-14 19:09:17 +02:00
|
|
|
path_dst = os.path.join(base_dir_dst, path_base)
|
|
|
|
|
|
|
|
# rename in the blend
|
|
|
|
fp.filepath = b"//" + path_base
|
|
|
|
|
|
|
|
# add to copylist
|
2014-10-15 18:37:47 +02:00
|
|
|
path_copy_files.add((path_src, path_dst))
|
2014-10-14 19:09:17 +02:00
|
|
|
|
2014-10-15 19:44:10 +02:00
|
|
|
del lib_visit
|
|
|
|
|
2014-10-16 20:01:22 +02:00
|
|
|
# handle the
|
2014-10-15 18:37:47 +02:00
|
|
|
blendfile_dst_tmp = temp_remap_cb(blendfile_src)
|
|
|
|
shutil.move(blendfile_dst_tmp, blendfile_dst)
|
|
|
|
path_temp_files.remove(blendfile_dst_tmp)
|
|
|
|
|
|
|
|
for fn in path_temp_files:
|
|
|
|
# strip '@'
|
|
|
|
shutil.move(fn, fn[:-1])
|
2014-10-14 19:09:17 +02:00
|
|
|
|
2014-10-15 18:37:47 +02:00
|
|
|
for src, dst in path_copy_files:
|
2014-10-14 19:09:17 +02:00
|
|
|
if not os.path.exists(src):
|
|
|
|
print(" Source missing! %r" % src)
|
|
|
|
else:
|
|
|
|
print(" Copying %r -> %r" % (src, dst))
|
|
|
|
shutil.copy(src, dst)
|
|
|
|
|
|
|
|
print(" Written:", blendfile_dst)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2014-10-15 16:35:18 +02:00
|
|
|
pack(b"/src/blendfile/test/paths.blend", b"/src/blendfile/test/out/paths.blend")
|