-
Notifications
You must be signed in to change notification settings - Fork 60
/
webify_models_v2.py
executable file
·99 lines (80 loc) · 3.1 KB
/
webify_models_v2.py
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
#!/usr/bin/env python3
# A script to update gazebo models to be web-friendly.
# It converts all textures to png format and make sure they are
# stored in the [model_name]/materials/textures/ directory.
# Texture references in the collada dae files are also updated
# to have the correct path.
# Usage: ./webify_models_v2.py path_to_model_directory
import os
import subprocess
import sys
import shutil
# explicitly set encoding in osx otherwise sometimes sed throws
# an illegal byte sequence error
if sys.platform == 'darwin':
os.environ['LANG'] = 'C'
path = sys.argv[1]
files = os.listdir(path)
find_cmd = ['find', path, '-name','*']
files = subprocess.check_output(find_cmd).split()
for file in files:
try:
path, filename = os.path.split(file)
name, format = filename.split(".")[-2:]
except:
continue # not a texture
try:
dest_dir = path
dest_path = "%s/%s.png" % (dest_dir, name)
cmd = None
# convert texture files to png
if format.lower() in ['tif', 'tga', 'tiff', 'jpeg', 'jpg', 'gif', 'png']:
if dest_path != file:
cmd = ['convert', file, dest_path]
subprocess.check_call(cmd)
cmd = ['rm', file]
subprocess.check_call(cmd)
# make sure texture files only exist in materials/textures dir
texture_dir = path
if (texture_dir.find('materials/textures') == -1 and
texture_dir.find('meshes') != -1):
model_path, other = texture_dir.split('meshes')
texture_dir = os.path.join(model_path, 'materials/textures')
cmd = ['mkdir', '-p', texture_dir]
subprocess.check_call(cmd)
if texture_dir != dest_dir:
cmd = ['mv', dest_path, texture_dir]
print(cmd)
subprocess.check_call(cmd)
# update texture path in dae files
if format.lower() in ['dae']:
sed_cmd = ["sed", "-i", "-e",
's/\(\.tga\|\.tiff\|\.tif\|\.jpg\|\.jpeg\|\.gif\)/\.png/g', file]
print(sed_cmd)
subprocess.check_call(sed_cmd)
# find relatvie path to texture dir
texture_dir = path
if (texture_dir.find('materials/textures') == -1 and
texture_dir.find('meshes') != -1):
model_path, other = texture_dir.split('meshes')
subdir_count = len(other.split('/'))
relative_path = ''
for i in range(0, subdir_count):
relative_path = relative_path + '\.\.\/'
# replace dae file png references to texture path
sed_cmd = ["sed", "-i","-e",
's/\(>\)\(.*\/\)\(.*\.png\)/\\1'+ relative_path + 'materials\/textures\/\\3/g', file]
print(sed_cmd)
subprocess.check_call(sed_cmd)
sed_cmd = ["sed", "-i","-e",
'/[a-zA-Z0-9_\.\/\-]\+materials\/textures/!s/\([a-zA-Z0-9_\-]\+\)\(\.png\W\)/'+ relative_path + 'materials\/textures\/\\1\\2/g', file]
print(sed_cmd)
subprocess.check_call(sed_cmd)
if format.lower() in ['material', 'txt', 'sdf']:
sed_cmd = ["sed", "-i", "-e",
's/\(\.tga\|\.tiff\|\.tif\|\.jpg\|\.jpeg\|\.gif\)/\.png/g', file]
print(sed_cmd)
subprocess.check_call(sed_cmd)
except Exception as e:
print("error %s" % e)
raise