17 print "[urdf_to_xacro.py] " +
' '.join(args)
29 self.paths.insert(0, path)
33 self.
rospkgs[pkg].insert(0, path)
38 env_str = os.environ.get(
"GAZEBO_MODEL_PATH",
None)
41 for path
in env_str.split(
':'):
42 if os.path.isdir(path):
46 cmd =
"rospack plugins --attrib=gazebo_model_path gazebo_ros" 47 lines = subprocess.check_output(cmd, shell=
True)
49 for line
in lines.split(os.linesep):
52 pkg, path = line.split()
53 if os.path.isdir(path):
56 self.
rospkgs[pkg] = self.rospack.get_path(pkg)
60 if path.startswith(
"model://"):
61 spath = path[len(
"model://"):].split(
'/')
63 path =
'/'.join(spath[1:])
67 for base_path
in self.
paths:
68 path = os.path.join(base_path, key)
69 if os.path.isdir(path):
73 for pkg, pkg_path
in self.rospkgs.items():
74 if path.startswith(pkg_path):
75 return "package://" + pkg + path[len(pkg_path):]
87 raise Exception(
"path %s is not found" % path)
93 return _gazebo.resolve_path(path)
96 def __init__(self, urdf_path, xacro_path, force_rename=None):
97 printf(
"loading", urdf_path)
98 self.
root = lxml.etree.parse(urdf_path,
99 parser=lxml.etree.XMLParser(remove_blank_text=
True))
104 links = self.root.xpath(
"//robot/link")
107 elif len(links) == 1:
110 link_names = [l.get(
"name")
for l
in links]
111 joints = self.root.xpath(
"//robot/joint")
113 child_link_name = j.find(
"child").get(
"link")
114 if child_link_name
in link_names:
115 link_names.remove(child_link_name)
116 if len(link_names) != 1:
118 return self.root.xpath(
"//robot/link[@name='%s']" % link_names[0])[0]
121 ns_str =
"${%s}_" % ns
123 links = self.root.xpath(
"//robot/link")
125 l.set(
"name", ns_str + l.get(
"name"))
127 gazebo = self.root.xpath(
"//robot/gazebo")
129 if g.get(
"reference")
is not None:
130 g.set(
"reference", ns_str + g.get(
"reference"))
132 joints = self.root.xpath(
"//robot/joint")
134 j.set(
"name", ns_str + j.get(
"name"))
135 j_parent = j.find(
"parent")
136 j_parent.set(
"link", ns_str + j_parent.get(
"link"))
137 j_child = j.find(
"child")
138 j_child.set(
"link", ns_str + j_child.get(
"link"))
140 transmissions = self.root.xpath(
"//robot/transmission")
141 for t
in transmissions:
142 t.set(
"name", ns_str + t.get(
"name"))
143 t_joint = t.find(
"joint")
144 t_joint.set(
"name", ns_str + t_joint.get(
"name"))
145 t_actuator = t.find(
"actuator")
146 t_actuator.set(
"name", ns_str + t_actuator.get(
"name"))
150 for param
in [
"visual",
"collision",
"inertial"]:
151 for m
in self.root.xpath(
"//robot/link/%s/geometry/mesh" % param):
152 if "filename" in m.attrib:
153 modelpath = m.attrib[
"filename"]
154 if self.
force_rename and modelpath.startswith(
"model://"):
155 pkgpath = self.
force_rename + modelpath[len(
"model://"):]
158 printf(modelpath,
"->", pkgpath)
159 m.attrib[
"filename"] = pkgpath
166 robot = self.root.getroot()
167 robot_name = robot.get(
"name")
168 first_joint = robot.find(
"joint")
169 virtual_joint = lxml.etree.Element(
"joint",
170 attrib={
"name":
"${name}_root_parent_joint",
"type":
"fixed"})
171 virtual_joint.append(lxml.etree.Element(
"parent", attrib={
"link":
"${parent}"}))
172 virtual_joint.append(lxml.etree.Element(
"child", attrib={
"link": root_link.get(
"name")}))
173 virtual_joint.append(lxml.etree.Element(
"insert_block", attrib={
"name":
"origin"}))
174 if first_joint
is not None:
175 first_joint.addprevious(virtual_joint)
177 robot.append(virtual_joint)
178 macro = lxml.etree.Element(
"macro",
179 attrib={
"name": robot_name,
"params":
"name parent *origin"})
180 for e
in robot.getchildren():
182 for e
in robot.getchildren():
188 if not os.path.exists(os.path.dirname(out_path)):
189 os.makedirs(os.path.dirname(out_path))
190 xmlstring = lxml.etree.tostring(self.
root,
192 xml_declaration=
True,
195 with open(out_path,
"w")
as f:
197 printf(
"saved to", out_path)
203 if __name__ ==
'__main__':
205 p = argparse.ArgumentParser(description=
"xacrify urdf file")
206 p.add_argument(
"urdf", type=str, help=
"path to input urdf file")
207 p.add_argument(
"xacro", type=str, help=
"path to output xacro file")
208 p.add_argument(
"-f",
"--force-rename", type=str,
209 default=
None, help=
"force replace model:// tag with specified name")
210 args = p.parse_args()
def load_path_from_env(self)
def resolve_path(self, path)
def add_ros_pkg(self, pkg, path)
def search_path(self, key)
def __init__(self, urdf_path, xacro_path, force_rename=None)
def resolve_gazebo_model_path(path)
def add_namespace(self, ns)
def parse_model_path(self, path)
def load_path_from_plugin(self)
def pack_ros_path(self, path)
def replace_model_path(self)