Skip to content

Commit b2015ae

Browse files
committed
improved script
1 parent 888fa24 commit b2015ae

File tree

1 file changed

+48
-39
lines changed

1 file changed

+48
-39
lines changed

AssetBatchConvertor.py

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,22 @@
1212
ASSETS=os.path.join(ROOT,'assetbundle')
1313
# destination folder
1414
DST = os.path.join(ROOT, 'extracted')
15+
# number of dirs to ignore
16+
# e.g. IGNOR_DIR_COUNT = 2 will reduce
17+
# 'assets/assetbundles/images/story_picture/small/15.png'
18+
# to
19+
# 'images/story_picture/small/15.png'
20+
21+
IGNOR_DIR_COUNT = 2
1522

1623
os.makedirs(DST, exist_ok=True)
1724

1825
debug = True
1926

2027
def main():
2128
for root, dirs, files in os.walk(ASSETS, topdown=False):
29+
if '.git' in root:
30+
continue
2231
for f in files:
2332
print(f)
2433
# load file
@@ -31,35 +40,36 @@ def main():
3140
# assets without container / internal path will be ignored for now
3241
if not asset.container:
3342
continue
34-
extracted = []
35-
local_path = []
43+
3644
# check which mode we will have to use
3745
num_cont = sum(1 for obj in asset.container.values() if obj.type in TYPES)
3846
num_objs = sum(1 for obj in asset.objects.values() if obj.type in TYPES)
3947

40-
# first we extract the assets in the container
41-
for asset_path, obj in asset.container.items():
42-
fp = os.path.join(DST, *asset_path.split('/'))
43-
export_obj(obj, fp)
48+
# check if container contains all important assets, if yes, just ignore the container
49+
if num_objs <= num_cont * 2:
50+
for asset_path, obj in asset.container.items():
51+
fp = os.path.join(DST, *asset_path.split('/')[IGNOR_DIR_COUNT:])
52+
export_obj(obj, fp)
53+
54+
# otherwise use the container to generate a path for the normal objects
55+
else:
56+
local_path = []
57+
extracted = []
4458

45-
local_path.append(
46-
os.path.splitext(asset_path)[0]
47-
if asset_path.endswith(".prefab")
48-
else
49-
os.path.dirname(asset_path)
59+
for asset_path in asset.container.keys():
60+
local_path.append(
61+
os.path.splitext(asset_path)[0]
5062
)
51-
52-
# way more assets without path then with path,
53-
# therefore we get the general path from the container
54-
# and then extract all assets depending on that
55-
if num_objs > num_cont * 2:
63+
5664
if len(local_path) > 1:
5765
if len(set(local_path)) == 1:
5866
local_path = local_path[0]
5967
else:
6068
local_path = mode(local_path)
6169
else:
62-
local_path = os.path.join(DST, *local_path[0].split('/'))
70+
local_path = local_path[0]
71+
local_path = os.path.join(DST, *local_path.split('/')[IGNOR_DIR_COUNT:])
72+
6373

6474
for obj in asset.objects.values():
6575
if obj.path_id not in extracted:
@@ -75,30 +85,29 @@ def export_obj(obj, fp : str, append_name : bool = False) -> list:
7585

7686
fp, extension = os.path.splitext(fp)
7787
os.makedirs(os.path.dirname(fp), exist_ok=True)
78-
try:
79-
if obj.type == 'TextAsset':
80-
if not extension:
81-
extension = '.txt'
82-
with open(f"{fp}{extension}", 'wb') as f:
83-
f.write(data.script)
84-
85-
elif obj.type == "Sprite":
86-
extension = ".png"
87-
data.image.save(f"{fp}{extension}")
88-
89-
return [obj.path_id, data.m_RD.texture.path_id, getattr(data.m_RD.alphaTexture,'path_id', None)]
90-
91-
elif obj.type == "Texture2D":
92-
extension = ".png"
93-
fp = f"{fp}{extension}"
94-
if not os.path.exists(fp):
88+
89+
if obj.type == 'TextAsset':
90+
if not extension:
91+
extension = '.txt'
92+
with open(f"{fp}{extension}", 'wb') as f:
93+
f.write(data.script)
94+
95+
elif obj.type == "Sprite":
96+
extension = ".png"
97+
data.image.save(f"{fp}{extension}")
98+
99+
return [obj.path_id, data.m_RD.texture.path_id, getattr(data.m_RD.alphaTexture,'path_id', None)]
100+
101+
elif obj.type == "Texture2D":
102+
extension = ".png"
103+
fp = f"{fp}{extension}"
104+
if not os.path.exists(fp):
105+
try:
95106
data.image.save(fp)
96-
97-
return [obj.path_id]
107+
except EOFError:
108+
pass
98109

99-
except Exception as e:
100-
print(fp, e)
101-
return([])
110+
return [obj.path_id]
102111

103112

104113
if __name__ == '__main__':

0 commit comments

Comments
 (0)