-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathobj2urdf.py
90 lines (77 loc) · 2.5 KB
/
obj2urdf.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
import os
import sys
"""
Simple script to wrap an .obj file into an .urdf file.
"""
def split_filename(string):
path_to_file = os.path.dirname(string)
filename, extension = os.path.splitext(os.path.basename(string))
return path_to_file, filename, extension
def check_input():
if len(sys.argv) == 1:
print("Provide a <file.obj> as argument")
sys.exit()
elif len(sys.argv) > 2:
print("Too many arguments")
sys.exit()
_, _, ext = split_filename(sys.argv[1])
if ext != ".obj":
print("Incorrect extension (<{}> instead of <.obj>)".format(ext))
sys.exit()
if not os.path.exists(sys.argv[1]):
print("The file <{}> does not exist".format(sys.argv[1]))
sys.exit()
def generate_output_name():
path_to_file, filename, extension = split_filename(sys.argv[1])
if path_to_file == "":
new_name = filename + ".urdf"
else:
new_name = path_to_file + "/" + filename + ".urdf"
return new_name
def check_output_file():
output_name = generate_output_name()
if os.path.exists(output_name):
print("Warning: <{}> already exists. Do you want to continue and overwrite it? [y, n] > ".format(output_name), end="")
ans = input().lower()
if ans not in ["y", "yes"]:
sys.exit()
def write_urdf_text():
output_name = generate_output_name()
_, name, _ = split_filename(sys.argv[1])
print("Creation of <{}>...".format(output_name), end="")
with open(output_name, "w") as f:
text = """<?xml version="1.0" ?>
<robot name="{}.urdf">
<link name="baseLink">
<inertial>
<origin rpy="0 0 0" xyz="0 0 0"/>
<mass value="0.0"/>
<inertia ixx="0" ixy="0" ixz="0" iyy="0" iyz="0" izz="0"/>
</inertial>
<visual>
<origin rpy="0 0 0" xyz="0 0 0"/>
<geometry>
<mesh filename="{}.obj" scale="1.0 1.0 1.0"/>
</geometry>
<material name="white">
<color rgba="1 1 1 1"/>
</material>
</visual>
<collision>
<origin rpy="0 0 0" xyz="0 0 0"/>
<geometry>
<mesh filename="{}.obj" scale="1.0 1.0 1.0"/>
<!-- You could also specify the collision (for the {}) with a "box" tag: -->
<!-- <box size=".06 .06 .06"/> -->
</geometry>
</collision>
</link>
</robot>
""".format(name, name, name, name)
f.write(text)
print(" done")
if __name__ == "__main__":
check_input()
new_full_filename = generate_output_name()
check_output_file()
write_urdf_text()