Generatorsο
URDF, XACRO, and SRDF generators for converting Python objects to files.
Base XML Engineο
All XML-based generators in LinkForge inherit from a shared base engine that handles common geometry, inertial, and origin logic.
- class linkforge.core.RobotXMLGenerator(pretty_print=True, output_path=None)[source]ο
Bases:
RobotGenerator[str]Abstract base class for XML-based robotics format generators.
Abstract Generatorsο
- class linkforge.core.base.RobotGenerator[source]ο
-
Abstract base class for all Robot Generators.
- abstractmethod generate(robot, **kwargs)[source]ο
Generate the output representation from the Robot model.
- class linkforge.core.base.IResourceResolver(*args, **kwargs)[source]ο
Bases:
ProtocolProtocol for resolving resource URIs (e.g. package://, file://, https://).
- resolve(uri, relative_to=None)[source]ο
Resolve a URI to a local filesystem Path.
- Parameters:
- Return type:
- Returns:
The resolved absolute Path.
- Raises:
FileNotFoundError β If the resource cannot be located.
- __init__(*args, **kwargs)ο
URDF Generatorο
- class linkforge.core.URDFGenerator(pretty_print=True, output_path=None, use_ros2_control=True)[source]ο
Bases:
RobotXMLGeneratorUnified Robot Description Format (URDF) generator.
- __init__(pretty_print=True, output_path=None, use_ros2_control=True)[source]ο
Initialize URDF generator.
- Parameters:
pretty_print (
bool) β If True, format XML with indentation for readability (default: True)output_path (
Path|None) β Path where URDF will be saved. Used to calculate relative mesh paths. If None, mesh paths will be absolute or package:// URIs.use_ros2_control (
bool) β Whether to generate ros2_control blocks from transmissions. Set to False if you donβt use ROS2 Control or prefer manual configuration. (default: True)
Example
>>> from pathlib import Path >>> # Basic generator with defaults >>> generator = URDFGenerator() >>> >>> # Generator with relative mesh paths >>> generator = URDFGenerator(output_path=Path("/workspace/robot.urdf"))
- generate(robot, validate=True, **kwargs)[source]ο
Generate URDF XML string from robot.
- Parameters:
- Return type:
- Returns:
URDF XML as formatted string with proper indentation
- Raises:
RobotGeneratorError β If robot validation fails (checks for cycles, missing links, etc.)
- add_links_section(parent, robot)[source]ο
Add Links section to parent element.
This follows the Template Method pattern. It handles the section header (comment) and iteration, delegating the specific element creation to _add_link_to_xml.
- add_ros2_control(parent, robot)[source]ο
Add ros2_control to parent element.
This method centralizes the logic for choosing between parsed (centralized) ROS 2 Control configuration and standard transmission-based generation. It ensures consistent XML structure and comments across URDF and XACRO export.
XACRO Generatorο
- class linkforge.core.XACROGenerator(pretty_print=True, advanced_mode=True, extract_materials=True, extract_dimensions=True, generate_macros=False, split_files=False, output_path=None, use_ros2_control=True)[source]ο
Bases:
URDFGeneratorGenerate XACRO from Robot model.
Supports material and dimension extraction, auto-macro generation, and modular file splitting.
- Parameters:
- __init__(pretty_print=True, advanced_mode=True, extract_materials=True, extract_dimensions=True, generate_macros=False, split_files=False, output_path=None, use_ros2_control=True)[source]ο
Initialize XACRO generator.
- Parameters:
pretty_print (
bool) β If True, format XML with indentationadvanced_mode (
bool) β Enable advanced XACRO featuresextract_materials (
bool) β Extract material colors as propertiesextract_dimensions (
bool) β Extract common dimensions as propertiesgenerate_macros (
bool) β Auto-generate macros for repeated patternssplit_files (
bool) β Split into multiple files (materials, macros, robot)output_path (
Path|None) β Path where XACRO will be saved (for relative mesh paths)use_ros2_control (
bool) β Whether to generate ROS2 Control from transmissions
SRDF Generatorο
The SRDF generator is documented with the rest of the SRDF layer (models, parser, generator) on the dedicated SRDF reference page.
Usage Examplesο
Generate URDFο
from linkforge.core import Robot, Link, Inertial, InertiaTensor, URDFGenerator
# Create robot
robot = Robot(
name="my_robot",
links=[
Link(
name="base_link",
inertial=Inertial(
mass=10.0,
inertia=InertiaTensor(ixx=1.0, iyy=1.0, izz=1.0)
)
)
]
)
# Generate URDF
generator = URDFGenerator(pretty_print=True)
urdf_string = generator.generate(robot)
# Save to file
with open("robot.urdf", "w") as f:
f.write(urdf_string)
Generate XACROο
from linkforge.core import XACROGenerator
from pathlib import Path
# Generate XACRO with split files
generator = XACROGenerator(
split_files=True,
output_dir=Path("robot_description")
)
files = generator.generate(robot)
# Creates:
# - robot.xacro (main assembly)
# - robot_properties.xacro (materials & dimensions)
# - robot_macros.xacro (reusable geometries)
# - robot_ros2_control.xacro (hardware plugins & interfaces)
Export Optionsο
# Compact output (no pretty printing)
generator = URDFGenerator(pretty_print=False)
compact_urdf = generator.generate(robot)
# With custom URDF path (for relative mesh paths)
generator = URDFGenerator(urdf_path=Path("robots/my_robot.urdf"))
urdf = generator.generate(robot)
# Mesh paths will be relative to robots/ directory
Round-Trip Verificationο
from linkforge.core import URDFParser
# Original robot
robot1 = create_robot()
# Export to URDF
generator = URDFGenerator()
urdf = generator.generate(robot1)
# Re-import
robot2 = URDFParser().parse_string(urdf)
# Verify fidelity
assert robot2.name == robot1.name
assert len(robot2.links) == len(robot1.links)
assert len(robot2.joints) == len(robot1.joints)