xacro4sdf
xacro4sdf is a simple tool to define and parse XML macro for sdf (sdformat), you can use xacro4sdf to write modularized SDF xml (not nest model)
- xacro4sdf is similar, but different from ros/xacro which is desiged for urdf.
- xacro4sdf is more simple, but it's also more easy to use.
Reference: ros/xacro
- With Xacro, you can construct shorter and more readable XML files by using macros that expand to larger XML expressions.
Attention:
<xacro_include_model>is departed after version2.0.0, you can use the tag with version1.2.6.
1. Usage
Installation
#install by pip
pip install xacro4sdf
# or install from source code
# git clone https://github.com/gezp/xacro4sdf.git
# cd xacro4sdf && sudo python3 setup.py install
create model.sdf.xmacro file
- refer to files in
test/(for exampletest/model.sdf.xmacro)
generate model.sdf
#cd test
xacro4sdf model.sdf.xmacro
- it will generate model.sdf (the result should be same as test/model.sdf)
- more examples can be found
testfolder.
summary of XML Tags
- definition of property and macro : core function
<xacro_define_property>and<xacro_define_macro>
- include
<xacro_include_definition>:include definition of property and macro from other xmacro file
- use of property and macro:
${xxx}: use of property ,it's very useful to use math expressions.<xacro_macro>: use of macro, it's very useful for modular modeling.
Tip:
- the xacro defination (
<xacro_define_property>,<xacro_define_macro>and<xacro_include_definition>) must be child node of root node<sdf>.
2. Features
- Properties
- Macros
- Math expressions
- Include
2.1. Properties
Properties are named values that can be inserted anywhere into the XML document
xacro definition
<!--definition of properties -->
<xacro_define_property name="radius" value="4.3" />
<!--use of properties-->
<circle diameter="${2 * radius}" />
generated xml
<circle diameter="8.6" />
2.2. Macros
The main feature of xacro4sdf is macros.
Define macros with the macro tag <xacro_define_property>, then specify the macro name and a list of parameters. The list of parameters should be whitespace separated.
The usage of Macros is to define <xacro_macro> which will be replaced with <xacro_define_property> block according to the param name.
xacro definition
<!--definition of macro-->
<xacro_define_property name="mass" value="0.2" />
<xacro_define_macro macro_name="box_inertia" params="m x y z">
<mass>${m}</mass>
<inertia>
<ixx>${m*(y*y+z*z)/12}</ixx>
<ixy>0</ixy>
<ixz>0</ixz>
<iyy>${m*(x*x+z*z)/12}</iyy>
<iyz>0</iyz>
<izz>${m*(x*x+z*z)/12}</izz>
</inertia>
</xacro_macro_define>
<!--use of macro-->
<inertial>
<pose>0 0 0.02 0 0 0</pose>
<xacro_macro name="box_inertia" m="${mass}" x="0.3" y="0.1" z="0.2"/>
</inertial>
generated xml
<inertial>
<pose>0 0 0.02 0 0 0</pose>
<mass>0.2</mass>
<inertia>
<ixx>0.0008333333333333335</ixx>
<ixy>0</ixy>
<ixz>0</ixz>
<iyy>0.002166666666666667</iyy>
<iyz>0</iyz>
<izz>0.002166666666666667</izz>
</inertia>
</inertial>
- only support simple parameters (string and number),but block parameters isn't supported.
- it's supported to use other
xacro_macroinxacro_define_macrowhich is recursive definition.
it's not recommended to define macro recursively (only support <=5).
2.3. Math expressions
- within dollared-braces
${xxxx}, you can also write simple math expressions. - refer to examples of Properties and Macros
- it's implemented by calling
eval()in python, so it's unsafe for some cases.
2.4. Including other xmacro files
definition include
You can include other xmacro files using the <xacro_include_definition> tag ,include other xmacro files according to param uri.
- it will only include the definition of properties with tag
<xacro_define_property>and macros with tag<xacro_define_macro>.
<xacro_include_definition uri="model://simple_car/model.sdf.xmacro"/>
<xacro_include_definition uri="file://simple_car/model.sdf.xmacro"/>
- The uri for
modelmeans to search file in a list of folders which are defined by environment variableIGN_GAZEBO_RESOURCE_PATHandGAZEBO_MODEL_PATH - The uri for
filemeans to open the file directly.- it try to open the file with relative path
simple_car/model.sdf.xmacro. - you can also try to open file with absolute path
/simple_car/model.sdf.xmacrowith urifile:///simple_car/model.sdf.xmacro.
- it try to open the file with relative path
Tips:
<xacro_include_definition>supports to include recursively.- Don't use same name for xacro definition (the param
nameof<xacro_define_property>and<xacro_define_macro>) , otherwise the priority of xacro definition need be considered.- Be carefully when using
<xacro_include_definition>and<xacro_include_model>
2.5 pre-defined common.xmacro
<!--macro defination:inertia-->
<xacro_define_macro name="inertia_cylinder" params="m r l">
<xacro_define_macro name="inertia_box" params="m x y z">
<xacro_define_macro name="inertia_sphere" params="m r">
<!--macro defination:geometry-->
<xacro_define_macro name="geometry_cylinder" params="r l">
<xacro_define_macro name="geometry_box" params="x y z">
<xacro_define_macro name="geometry_sphere" params="r">
<xacro_define_macro name="geometry_mesh" params="uri">
<!--macro defination:visual_collision_with_mesh-->
<xacro_define_macro name="visual_collision_with_mesh" params="prefix uri">
- you can directly use these macros in your xmacro file.
3. Custom usage in python
you can use xacro4sdf in python easily
from xacro4sdf.xacro4sdf import XMLMacro
xmacro=XMLMacro()
#case1 parse from file
xmacro.set_xml_file(inputfile)
xmacro.generate()
xmacro.to_file(outputfile)
#case2 parse from xml string
xmacro.set_xml_string(xml_str)
xmacro.generate()
xmacro.to_file(outputfile)
#case3 generate to string
xmacro.set_xml_file(inputfile)
xmacro.generate()
xmacro.to_string()
#case4 custom property
xmacro.set_xml_file(inputfile)
# use custom property dictionary to overwrite global property
# defined by <xacro_define_property> (only in 'inputfile')
kv={"rplidar_a2_h":0.8}
xmacro.generate(custom_property=kv)
xmacro.to_file(outputfile)
#case5 set model static
xmacro.set_xml_file(inputfile)
xmacro.generate()
xmacro.set_static(True)
xmacro.to_file(outputfile)
4. Maintainer and License
maintainer : Zhenpeng Ge, zhenpeng.ge@qq.com
xacro4sdf is provided under MIT License.
Metadata
Release files for xacro4sdf 2.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| xacro4sdf-2.1.0.tar.gz | 10.4 kB | Without provenance | Details for xacro4sdf-2.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| xacro4sdf-2.1.0-py3-none-any.whl | Python 3 | none | any | Details for xacro4sdf-2.1.0-py3-none-any.whl |
Total release size: 19.6 kB
Release files / xacro4sdf-2.1.0.tar.gz
| Download URL | xacro4sdf-2.1.0.tar.gz |
|---|---|
| Size | 10.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
01a857ecb71a97d95577af3fef913961c8cb462cf968de0ec92c23837050efaf
|
|
BLAKE2b-256 checksum How to use checksums |
1a36ef8c6ed8ced211319a5f6a097128ff0c511cde9a44b5113d1531f475af39
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
|
Release files / xacro4sdf-2.1.0-py3-none-any.whl
| Download URL | xacro4sdf-2.1.0-py3-none-any.whl |
|---|---|
| Size | 9.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ad22bc0f2233ac3e3ec154eca0d5efeab6b06141b17c73359220461e8f9dee4e
|
|
BLAKE2b-256 checksum How to use checksums |
3240e05a6795b8cc96117c09990970ca70282866cb42199bfa0a3f7d1fa256da
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
|
Security for xacro4sdf 2.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.