Skip to content

Commit 3e55efa

Browse files
authored
modularize_urdf: Added --macro-name option to specify the macro name (#610)
1 parent a66be38 commit 3e55efa

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

skrobot/apps/modularize_urdf.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ def main():
1616
parser.add_argument("--no-prefix", action="store_true",
1717
help="Remove 'prefix' parameter and do not use ${prefix} in names")
1818
parser.add_argument("--output", help="Output file path")
19+
parser.add_argument("--macro-name", help="Name for the xacro macro. Defaults to the robot name in the URDF.")
1920
args = parser.parse_args()
2021

2122
root_link = find_root_link(args.input_urdf)
22-
xacro_root, robot_name = transform_urdf_to_macro(args.input_urdf, root_link, args.no_prefix)
23+
xacro_root, final_macro_name = transform_urdf_to_macro(
24+
args.input_urdf, root_link, args.no_prefix, macro_name=args.macro_name)
2325

2426
if args.output:
2527
output_path = args.output
@@ -29,7 +31,7 @@ def main():
2931

3032
etree.ElementTree(xacro_root).write(output_path, pretty_print=True, xml_declaration=True, encoding="utf-8")
3133

32-
print_xacro_usage_instructions(output_path, robot_name, args.no_prefix)
34+
print_xacro_usage_instructions(output_path, final_macro_name, args.no_prefix)
3335

3436

3537
if __name__ == "__main__":

skrobot/urdf/modularize_urdf.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def find_root_link(input_path):
105105
raise ValueError("Could not determine root link. Check that the URDF contains valid joint definitions.")
106106

107107

108-
def transform_urdf_to_macro(input_path, connector_link, no_prefix):
108+
def transform_urdf_to_macro(input_path, connector_link, no_prefix, macro_name=None):
109109
"""Transform a URDF file into a Xacro macro with connector joint.
110110
111111
This function converts a URDF file into a reusable Xacro macro that can be
@@ -120,11 +120,13 @@ def transform_urdf_to_macro(input_path, connector_link, no_prefix):
120120
Name of link to connect to parent robot
121121
no_prefix : bool
122122
If True, don't add prefixes to element names
123+
macro_name : str, optional
124+
Name for the xacro macro. If None, uses the robot name from the URDF.
123125
124126
Returns
125127
-------
126128
tuple[lxml.etree.Element, str]
127-
Tuple of (xacro_root_element, robot_name)
129+
Tuple of (xacro_root_element, final_macro_name)
128130
"""
129131
XACRO_NS = "http://ros.org/wiki/xacro"
130132
NSMAP = {"xacro": XACRO_NS}
@@ -155,9 +157,12 @@ def transform_urdf_to_macro(input_path, connector_link, no_prefix):
155157
# Provide a default empty string for the prefix.
156158
macro_params.insert(0, "prefix:=''")
157159

160+
# Use provided macro_name, otherwise fall back to the robot name
161+
final_macro_name = macro_name if macro_name else robot_name
162+
158163
# Create macro element
159164
macro = etree.Element("{}macro".format("{" + XACRO_NS + "}"))
160-
macro.set("name", robot_name)
165+
macro.set("name", final_macro_name)
161166
macro.set("params", " ".join(macro_params))
162167
xacro_root.append(macro)
163168

@@ -216,7 +221,7 @@ def transform_urdf_to_macro(input_path, connector_link, no_prefix):
216221
indent_element(xacro_root, level=0)
217222
xacro_root.tail = None # Ensure no tail text at the root level
218223

219-
return xacro_root, robot_name
224+
return xacro_root, final_macro_name
220225

221226

222227
def print_xacro_usage_instructions(output_path, robot_name, no_prefix=False):

0 commit comments

Comments
 (0)