base_fly
ActuatorType
¶
Bases: Enum
Actuator types supported by MuJoCo. See MuJoCo XML reference for details on each type.
Source code in src/flygym/compose/fly/base_fly.py
BaseFly
¶
Bases: BaseCompositionElement
Abstract base for all fly body models (e.g. NeuroMechFly, FlyBody).
FlyGym supports multiple fly body models that can be used interchangeably within the same simulation API. Concrete subclasses provide model-specific geometry assets, anatomy classes, and parameter defaults, while all model-agnostic composition logic lives here. Choose the model that best fits your experiment:
NeuroMechFly— The default model, derived from micro-CT imaging. Suitable for most locomotion and sensorimotor experiments.FlyBody— A biomechanically detailed model from Vaxenburg et al. (2025), with wing and abdomen degrees of freedom and anatomically grounded parameters.
This class is not meant to be instantiated directly — use a concrete subclass.
Holds the model-agnostic composition logic shared by every fly. Concrete subclasses supply the model identity (asset paths, anatomy classes, scale) and may override individual build steps.
Represents a complete fly with body segments, joints, actuators, sensors, and cameras. The fly is built from mesh assets and configured via config files that define rigging (joint positions), visuals (colors/textures), and global MuJoCo parameters.
The fly uses a hierarchical body structure with a root segment (typically the thorax) from which all other segments branch. Joints and actuators are added separately after initialization to allow flexible model configurations.
PyMJCF -> MjSpec migration (v2.1.0)
FlyGym 2.1.0 dropped the PyMJCF backend in favour of MuJoCo's native
MjSpec API. If you are upgrading from an earlier version, see the
v2.1.0 changelog
for breaking changes and a migration guide.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Identifier for this fly instance. |
'fly'
|
rigging_config_path
|
PathLike
|
Path to YAML file defining body segment positions, orientations, and masses. |
required |
mesh_basedir
|
PathLike
|
Directory containing STL mesh files for body segments. |
required |
mujoco_globals_path
|
PathLike
|
Path to YAML file with global MuJoCo parameters (timestep, gravity, etc.). |
required |
root_segment
|
BodySegment | str
|
Root body segment for the kinematic tree (e.g., |
'c_thorax'
|
mirror_left2right
|
bool
|
If True, mirror left-side meshes for right side instead of loading separate mesh files. Reduces asset size and ensures symmetry. |
required |
mesh_type
|
MeshType
|
Mesh resolution to use. |
required |
geom_fitting_option
|
GeomFittingOption
|
How to fit collision geometries. |
UNMODIFIED
|
Attributes:
| Name | Type | Description |
|---|---|---|
skeleton |
Skeleton | None
|
Joint structure of the fly, set when add_joints() is called. |
bodyseg_to_mjcfmesh |
Maps body segments to MJCF mesh elements. |
|
bodyseg_to_mjcfbody |
Maps body segments to MJCF body elements. |
|
bodyseg_to_mjcfgeom |
Maps body segments to MJCF geometry elements. |
|
jointdof_to_mjcfjoint |
Maps joint DOFs to MJCF joint elements. |
|
anatomicaljoint_to_mjcfsites |
Maps anatomical joints to MJCF site elements. |
|
jointdof_to_mjcfactuator_by_type |
Maps actuator type to a further dictionary, which maps joint DOFs to MJCF actuator elements (only if the actuator exists). |
|
sensorname_to_mjcfsensor |
Maps sensor names to MJCF sensor elements. |
|
cameraname_to_mjcfcamera |
Maps camera names to MJCF camera elements. |
|
jointdof_to_neutralangle |
Neutral (resting) angle for each joint DOF. |
|
jointdof_to_neutralaction_by_type |
Neutral actuator input for each (actuator_type, joint_dof) pair. Maps actuator type to a further dictionary, which maps joint DOFs to their neutral actuator input (only if the actuator exists). |
Source code in src/flygym/compose/fly/base_fly.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 | |
name
property
¶
Name of this fly instance.
add_actuators(jointdofs, actuator_type, neutral_input=None, *, forcelimited=True, forcerange=(-30.0, 30.0), **kwargs)
¶
Add actuators to specified joints.
Creates actuators that can apply forces/torques to joints. Multiple actuator types can be added to the same joints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
jointdofs
|
Iterable[JointDOF]
|
Joint DOFs to actuate. |
required |
actuator_type
|
ActuatorType | str
|
Type of actuator (motor, position, velocity, etc.). |
required |
neutral_input
|
dict[str, float] | KinematicPose | KinematicPosePreset | None
|
Default actuator inputs. Accepts a |
None
|
forcelimited
|
bool
|
If True, actuators cannot exceed forcerange. |
True
|
forcerange
|
tuple[float, float]
|
Force limit as a (min, max) tuple. |
(-30.0, 30.0)
|
**kwargs
|
Any
|
Additional arguments passed to MJCF actuator creation (e.g., kp for position actuators, kv for velocity actuators). See MuJoCo XML reference for details on supported attributes. |
{}
|
Returns:
| Type | Description |
|---|---|
dict[JointDOF, MjsActuator]
|
Dictionary mapping JointDOF to created MJCF actuator elements. |
Source code in src/flygym/compose/fly/base_fly.py
add_joint_sites(anatomical_joints)
¶
Add MJCF sites at the origins of selected anatomical joints.
Each site is placed at (0, 0, 0) in the child body frame. Since body
origins are defined at their parent-child joint locations in this model,
these sites track anatomical joint positions in world coordinates during
simulation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
anatomical_joints
|
list[AnatomicalJoint]
|
Anatomical joints to materialize as MJCF sites. |
required |
Returns:
| Type | Description |
|---|---|
dict[AnatomicalJoint, MjsSite]
|
Dictionary mapping each anatomical joint to its created MJCF site |
dict[AnatomicalJoint, MjsSite]
|
element (same entries added into |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a site for a requested anatomical joint already exists. |
Source code in src/flygym/compose/fly/base_fly.py
add_joints(skeleton, neutral_pose=None, *, stiffness=10.0, damping=0.5, armature=1e-06, **kwargs)
¶
Add joints to the fly model based on a skeleton definition.
Creates hinge joints connecting body segments according to the skeleton's kinematic tree structure. Each joint is configured with passive spring-damper dynamics and a neutral (resting) angle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
skeleton
|
Skeleton
|
Skeleton defining which joints to create and their DOFs. |
required |
neutral_pose
|
KinematicPose | KinematicPosePreset | None
|
Resting angles for joints. If provided, must match skeleton's axis order. If not provided, all neutral angles default to 0. |
None
|
stiffness
|
float
|
Joint stiffness (spring constant). |
10.0
|
damping
|
float
|
Joint damping coefficient. |
0.5
|
armature
|
float
|
Additional inertia added to the joint for numerical stability. Should be small enough to not affect dynamics. |
1e-06
|
**kwargs
|
Any
|
Additional arguments passed to MJCF joint creation. See MuJoCo XML reference for details on supported attributes. |
{}
|
Returns:
| Type | Description |
|---|---|
dict[JointDOF, MjsJoint]
|
Dictionary mapping JointDOF to created MJCF joint elements. |
Source code in src/flygym/compose/fly/base_fly.py
add_leg_adhesion(gain=1.0)
¶
Add adhesion actuators to the tarsus5 segments of all legs.
Adhesion actuators apply a normal attraction force, enabling the fly to grip surfaces. The control input per leg ranges from 0 to 1, where 0 fully releases adhesion and 1 applies the configured gain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gain
|
float | dict[str, float]
|
Adhesion actuator gain. Either a single float applied to all legs, or a dict mapping leg position identifiers to per-leg gain values. |
1.0
|
Returns:
| Type | Description |
|---|---|
dict[str, MjsActuator]
|
Dict mapping leg position identifier to the created MJCF adhesion |
dict[str, MjsActuator]
|
actuator element (same as |
Raises:
| Type | Description |
|---|---|
ValueError
|
If adhesion actuators have already been added. |
Source code in src/flygym/compose/fly/base_fly.py
add_tracking_camera(name='trackcam', mode='track', pos_offset=(-0.5, -7.5, 5), rotation=Rotation3D('xyaxes', (1, 0, 0, 0, 0.6, 0.8)), fovy=30.0, **kwargs)
¶
Add a camera that tracks the fly's root body.
The camera is added inside the root segment's body element. MuJoCo's
track/trackcom modes follow the camera's parent body, so the camera
must be a child of the fly body to follow it; a camera placed in the world
body would stay put. track follows the body's position while keeping a
constant orientation in the world frame (a "follow" camera that pans but does
not rotate with the fly).
Warning
pos_offset is expressed in the root segment's (thorax) body frame, not
in world coordinates. This differs from FlyGym versions before the MjSpec
migration, where the tracking camera lived in the world body and the offset
was effectively a world-frame position. The default changed accordingly,
from (0, -7.5, 6) to (-0.5, -7.5, 5). Hard-coded pos_offset
values tuned for the old world-frame placement must be re-tuned: the root
segment sits roughly (0.5, 0, 1.3) mm from the fly's attachment point
(plus the spawn height) in the neutral pose, so the same offset now places
the camera higher and shifted toward the head. The upside is that a given
pos_offset now yields the same camera position relative to the fly in
every world and when the fly is compiled on its own.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Camera name. |
'trackcam'
|
mode
|
str
|
MuJoCo camera tracking mode ( |
'track'
|
pos_offset
|
Vec3
|
Camera position offset from the tracked root segment in mm. |
(-0.5, -7.5, 5)
|
rotation
|
Rotation3D
|
Camera orientation as a |
Rotation3D('xyaxes', (1, 0, 0, 0, 0.6, 0.8))
|
fovy
|
float
|
Vertical field of view in degrees. |
30.0
|
**kwargs
|
Any
|
Additional attributes passed to the MJCF camera element. See MuJoCo XML reference. |
{}
|
Returns:
| Type | Description |
|---|---|
MjsCamera
|
The created MJCF camera element. |
Source code in src/flygym/compose/fly/base_fly.py
colorize(visuals_config_path)
¶
Apply colors and textures to the fly model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
visuals_config_path
|
PathLike
|
Path to the YAML file defining per-segment material and texture assignments. |
required |
Source code in src/flygym/compose/fly/base_fly.py
compile()
¶
Compile the fly on its own (e.g. for preview_model or save_xml).
Disables fusestatic for the standalone compile. A lone fly has no free joint,
so its root segment is a static body that the optimization would fuse into the
worldbody -- which breaks the track-mode tracking camera parented to it (the
camera would fall back to tracking the worldbody and mis-place itself relative to
the fly). A standalone fly is only ever inspected/previewed, never simulated, so
the lost optimization does not matter here. The setting is applied to the
compiled copy only, leaving the live spec untouched so a world can still fuse
the fly's other static bodies once it is attached.
As in the base method, we always compile a copy rather than the live spec: compiling mutates the spec in place, which would invalidate the element references FlyGym holds.
Source code in src/flygym/compose/fly/base_fly.py
get_actuated_jointdofs_order(actuator_type)
¶
Same as get_jointdofs_order(), but only for the subset of joint DoFs that
are actuated by the specified actuator type. During simulation, the user should
provide control input in this order.
Source code in src/flygym/compose/fly/base_fly.py
get_bodysegs_order()
¶
Get the canonical order of body segments. The exact order is not important, but it should be respected consistently throughout. For example, during simulation, the fly body state returned by the simulator will be in this order.
Source code in src/flygym/compose/fly/base_fly.py
get_jointdofs_order()
¶
Same as get_bodysegs_order(), but for joint DoFs instead of body segments.
get_legs_order()
¶
get_pose_lookup(neutral_pose)
¶
Get a lookup dictionary mapping joint DOF names to neutral angles for a given neutral pose.
Source code in src/flygym/compose/fly/base_fly.py
get_sites_order()
¶
Get the canonical order of anatomical joints with associated MJCF sites.
This is the order used by simulation site-state readout methods such as
Simulation.get_site_positions.
Source code in src/flygym/compose/fly/base_fly.py
GeomFittingOption
¶
Bases: Enum
How to fit collision geometries to the mesh shapes.
Attributes:
| Name | Type | Description |
|---|---|---|
UNMODIFIED |
Keep the original mesh-based geometries. |
|
ALL_TO_CAPSULES |
Replace all geometries with capsule approximations. |
|
CLAWS_TO_CAPSULES |
Replace only tarsus5 geometries with capsules. |
Source code in src/flygym/compose/fly/base_fly.py
MeshType
¶
Bases: Enum
Mesh resolution to use for fly body geometry.
Attributes:
| Name | Type | Description |
|---|---|---|
FULLSIZE |
Original high-resolution meshes. |
|
SIMPLIFIED_MAX2000FACES |
Simplified meshes with at most 2000 faces per segment. Faster to render and simulate. Used by default. |