Set xacro arguments when parsing a file using Python (ROS2)
Is it possible to set arguments when parsing a file using the python xacro library?
In ROS2, a number of packages parse xacro files directly in the launch file using the python xacro library, with something along the lines of:
import xacro, os
xacro_file = "test.urdf.xacro"
doc = xacro.parse(open(xacro_file))
xacro.process_doc(doc)
robot_description_config = doc.toxml()
robot_description = {'robot_description': robot_description_config}
For some context, say I have a file, test.urdf.xacro
that takes an argument named input
:
<?xml version="1.0"?>
<robot name="test" xmlns:xacro="http://ros.org/wiki/xacro">
<xacro:arg name="input" defaut="default"/>
<link name="$(arg input)"/>
</robot>
Using the python code above, the argument would be the default value and the link would be named default
. But how can you pass in a custom value?
Using the command line interface, f I wanted to parse the file with some custom value, I could simply type the following in the terminal:
$ xacro test.urdf.xacro input:="new_input"
and get an output where my link is appropriately named new_input
:
<?xml version="1.0" ?>
<!-- =================================================================================== -->
<!-- | This document was autogenerated by xacro from test.urdf.xacro | -->
<!-- | EDITING THIS FILE BY HAND IS NOT RECOMMENDED | -->
<!-- =================================================================================== -->
<robot name="test">
<link name="new_input"/>
</robot>
But I'm at a loss when it comes to achieving the same directly using python. Anyone have any ideas?