init
DEFAULT_MUSCULOSKELETAL_XML = MUSCULOSKELETAL_MODEL_DIR / 'best_combined_arm_damping_stiff_cvt3.xml'
module-attribute
¶
FlyMimic's muscle-driven fly with passive joint stiffness + spring refs.
This is the arm_damping_stiff variant: 15 left-front-leg muscles and body
geometry, plus biologically-motivated passive joint stiffness (0.4) and
per-joint spring reference angles.
DEFAULT_SCENE_CAMERA = 'scene'
module-attribute
¶
Name assigned to FlyMimic's (otherwise unnamed) world camera so it can be
selected for rendering via Simulation.set_renderer — e.g. when recording a
rollout video of a trained policy.
MUSCULOSKELETAL_MODEL_DIR = assets_dir / 'model/musculoskeletal'
module-attribute
¶
Directory holding FlyMimic's musculoskeletal MJCF. The body meshes it
references are not bundled here (see MUSCULOSKELETAL_MESH_DIR), and the
imitation-learning mocap clips live with the demo, in
flygym_demo.muscle_imitation.
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 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 | |
n_bodysegs
property
¶
Number of body segments in this fly.
n_jointdofs
property
¶
Number of joint DoFs in this fly.
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_n_actuated_jointdofs(actuator_type)
¶
Number of joint DoFs actuated by the specified actuator type.
Source code in src/flygym/compose/fly/base_fly.py
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
Fly
¶
Bases: NeuroMechFly
Deprecated alias for NeuroMechFly. Will be removed in a future release.
Source code in src/flygym/compose/fly/neuromechfly.py
FlyBody
¶
Bases: BaseFly
The FlyBody body model for Drosophila melanogaster.
FlyBody is published in:
Vaxenburg, R., et al. (2025). A whole-body model of *Drosophila* with
precise neuromuscular connectivity. *Nature*.
https://doi.org/10.1038/s41586-025-09029-4
Source code for the original model: https://github.com/TuragaLab/flybody
FlyBody provides an anatomically detailed model from the Turaga lab with wing and abdomen degrees of freedom and biomechanically grounded joint parameters, and integrates with the FlyGym API for scene composition, simulation, and control.
Warning
Support for the FlyBody body model is experimental. The API may
change in future releases, and not all features available for the
default NeuroMechFly model are currently supported.
Both FlyBody and NeuroMechFly inherit from BaseFly and expose the
same composition API, so they can be used interchangeably for locomotion
experiments. FlyBody additionally provides:
- Wing degrees of freedom (pitch, roll, yaw per wing).
- Abdomen degrees of freedom (pitch and yaw via tendon actuators).
- Biomechanically calibrated joint stiffness, damping, and ctrl-range parameters loaded from per-joint YAML config files.
- Tendon-coupled tarsus and abdomen joints via
add_tendons().
The FlyBody XML is already in mm units; mesh files use .obj format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Identifier for this fly instance. Defaults to |
'flybody'
|
rigging_config_path
|
PathLike
|
Path to YAML file defining body segment positions, orientations, and masses. |
FLYBODY_RIGGING_CONFIG_PATH
|
mesh_basedir
|
PathLike
|
Directory containing OBJ mesh files for body segments. |
FLYBODY_MESH_DIR
|
mujoco_globals_path
|
PathLike
|
Path to YAML file with global MuJoCo parameters. |
FLYBODY_MUJOCO_GLOBALS_PATH
|
all_geom_suffixes_path
|
PathLike
|
Path to YAML file listing per-segment geometry name suffixes (FlyBody uses multiple geoms per body segment). |
FLYBODY_ALL_GEOM_SUFFIXES_PATH
|
mirror_left2right
|
bool
|
If True, mirror left-side meshes for the right side.
Defaults to |
False
|
root_segment
|
FlyBodyBodySegment | str
|
Root body segment for the kinematic tree. |
'c_thorax'
|
mesh_type
|
MeshType
|
Mesh resolution to use. |
FULLSIZE
|
geom_fitting_option
|
GeomFittingOption
|
How to fit collision geometries. |
UNMODIFIED
|
joint_config_path
|
PathLike
|
Path to YAML file with per-joint parameters (stiffness, damping, range). |
FLYBODY_JOINT_CONFIG_PATH
|
actuator_config_path
|
PathLike
|
Path to YAML file with per-actuator gain parameters. |
FLYBODY_ACTUATOR_CONFIG_PATH
|
vision_config_path
|
PathLike
|
Path to YAML file with vision sensor configuration. |
FLYBODY_DEFAULT_VISION_CONFIG_PATH
|
Source code in src/flygym/compose/fly/flybody.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 | |
add_actuators(jointdofs, actuator_type, neutral_input=None, *, forcelimited=False, forcerange=(-0.3, 0.3), **kwargs)
¶
Add actuators to specified joints.
Like BaseFly.add_actuators, but actuator parameters default to the values in
the flybody actuator_config.yaml unless overridden. The FlyBody defaults
also differ: forcelimited is False and forcerange is (-0.3, 0.3).
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, clamp actuator force to |
False
|
forcerange
|
tuple[float, float]
|
Force limit as a (min, max) tuple. Only applied when
|
(-0.3, 0.3)
|
**kwargs
|
Any
|
MJCF actuator attributes (e.g. |
{}
|
Returns:
| Type | Description |
|---|---|
dict[JointDOF, MjsActuator]
|
Dictionary mapping JointDOF to created MJCF actuator elements. |
Source code in src/flygym/compose/fly/flybody.py
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 | |
add_joints(skeleton, neutral_pose=None, **kwargs)
¶
Add joints to the FlyBody model based on a skeleton definition.
Like BaseFly.add_joints, but instead of taking uniform stiffness,
damping, and armature arguments, the per-joint values are loaded from
the flybody joint config (via _resolve_joint_params). neutral_pose sets
each joint's springref (the angle at which passive spring forces are zero),
and kwargs override the config-derived values.
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 the skeleton's axis order; otherwise all neutral angles default to 0. |
None
|
**kwargs
|
Any
|
Per-joint MJCF attributes that override the config-derived values. See the MuJoCo XML reference for supported attributes. |
{}
|
Returns:
| Type | Description |
|---|---|
dict[JointDOF, MjsJoint]
|
Dictionary mapping JointDOF to created MJCF joint elements. |
Source code in src/flygym/compose/fly/flybody.py
add_leg_adhesion(gain=0.985, add_labrum=True, labrum_gain=1.0)
¶
Add adhesion actuators to the tarsus5 segments of all legs and optionally to the labrum.
Adhesion actuators apply a normal attraction force, enabling the fly to grip surfaces. The control input per leg ranges from 0 (released) to 1 (full 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. |
0.985
|
add_labrum
|
bool
|
Whether to also add an adhesion actuator for the labrum (mouthpart). |
True
|
labrum_gain
|
float
|
Adhesion actuator gain for the labrum (used when |
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/flybody.py
colorize(visuals_config_path=FLYBODY_VISUALS_CONFIG_PATH)
¶
Apply colors and textures to the FlyBody model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
visuals_config_path
|
PathLike
|
Path to the YAML file defining per-segment material and texture assignments. Defaults to the bundled FlyBody visuals. |
FLYBODY_VISUALS_CONFIG_PATH
|
Source code in src/flygym/compose/fly/flybody.py
translate_generalactparams_to_specificactparams(general_params, actuator_type)
¶
Full gainprm/biasprm/dynprm -> kp/kv/timeconst translation.
NOTE: As of v2.1.0 this is dead code -- nothing calls it. add_actuators
uses the simplified
translate_generaljointparams_to_specificjointparams_simplified instead.
It is intentionally retained as the intended complete implementation, to be
wired up in a future release. See #272 for details.
Source code in src/flygym/compose/fly/flybody.py
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 | |
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. |
Source code in src/flygym/compose/fly/base_fly.py
MjWarpCompatibilityReport
dataclass
¶
Outcome of probing whether MuJoCo-Warp accepts the muscle model.
Attributes:
| Name | Type | Description |
|---|---|---|
mjwarp_available |
bool
|
Whether |
put_model_ok |
bool | None
|
Whether |
error |
str | None
|
The exception text if |
message |
str
|
A human-readable summary. |
Source code in src/flygym/compose/fly/musculoskeletal.py
MusculoskeletalFly
¶
Bases: BaseCompositionElement
FlyGym-compatible wrapper around FlyMimic's musculoskeletal MJCF.
The musculoskeletal body model is published in:
Ozdil, P. G., et al. (2026). Musculoskeletal simulation of limb
movement biomechanics in *Drosophila melanogaster*. *ICLR 2026*.
https://arxiv.org/abs/2509.06426
Source code for the original model: https://github.com/gizemozd/FlyMimic
Plain flygym — not GPU-accelerated
MusculoskeletalFly works with plain flygym.Simulation (CPU,
single world). It does not require flygym.warp.
Unlike NeuroMechFly and FlyBody, which compose a body from meshes
and YAML rigging configs via BaseFly, this class loads FlyMimic's
pre-authored self-contained MJCF and wraps it so that Simulation and
its sensor suite work unchanged. Dict keys on all tracking attributes
are the model's own MJCF element-name strings (e.g. "LFFemur",
"joint_LFCoxa_yaw").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xml_path
|
PathLike
|
Path to the FlyMimic MJCF. Defaults to the bundled
|
DEFAULT_MUSCULOSKELETAL_XML
|
name
|
str
|
Logical fly name used by |
'nmf'
|
Attributes:
| Name | Type | Description |
|---|---|---|
bodyseg_to_mjcfbody |
dict[str, MjsBody]
|
Maps body-segment name → MJCF body element. |
bodyseg_to_mjcfgeom |
dict[str, list[MjsGeom]]
|
Maps body-segment name → list of MJCF geom elements (one entry per geom on that body). |
jointdof_to_mjcfjoint |
dict[str, MjsJoint]
|
Maps joint-DoF name → MJCF joint element. |
jointdof_to_mjcfactuator_by_type |
Maps |
|
leg_to_adhesionactuator |
dict[str, MjsActuator]
|
Always empty (FlyMimic has no adhesion). |
anatomicaljoint_to_mjcfsites |
dict[str, MjsSite]
|
Always empty. |
eyecameraname_to_mjcfcamera |
dict[str, MjsCamera]
|
Camera elements added via |
cameraname_to_mjcfcamera |
dict[str, MjsCamera]
|
All scene cameras, including the world
camera named |
muscle_names |
list[str]
|
Names of the 15 Hill-type muscle actuators, in MJCF order (read-only property). |
Source code in src/flygym/compose/fly/musculoskeletal.py
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 | |
muscle_names
property
¶
Names of the muscle actuators, in MJCF order.
add_vision(*, fovy=145.0, draw_sensor_markers=False)
¶
Attach left/right eye cameras to the model's eye bodies.
Enables Simulation.get_raw_vision / get_ommatidia_readouts. Note
that FlyGym's fisheye Retina is calibrated for FlyGym's own eye
placement, so ommatidia readouts on this body are approximate.
Source code in src/flygym/compose/fly/musculoskeletal.py
get_actuated_jointdofs_order(actuator_type)
¶
Return actuator names of the given type, in MJCF order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actuator_type
|
ActuatorType | str
|
An |
required |
Source code in src/flygym/compose/fly/musculoskeletal.py
get_bodysegs_order()
¶
get_jointdofs_order()
¶
get_legs_order()
¶
get_sites_order()
¶
NeuroMechFly
¶
Bases: BaseFly
The NeuroMechFly body model for Drosophila melanogaster.
NeuroMechFly is derived from a micro-CT scan of a real fly. It is the
default body model in FlyGym and supports all locomotion, sensorimotor,
and vision experiments. For an alternative anatomically detailed model
with wing and abdomen degrees of freedom, see FlyBody.
Both NeuroMechFly and FlyBody inherit from BaseFly and expose the
same composition API, so they can be used interchangeably.
See NeuroMechFlyWithSeparateTrochanterFemur for a variant of this model
where the trochanter and femur are separate body segments (instead of fused
into a single segment). The user can still effectively fuse the two by not
adding any joint between them (which is the case by default). When MuJoCo
compiler parameter fusestatic is set to true, the two segment will
actually be fused at compile time. Tentatively,
NeuroMechFlyWithSeparateTrochanterFemur will fully replace NeuroMechFly
in FlyGym 3.0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Identifier for this fly instance. Defaults to |
'nmf'
|
rigging_config_path
|
PathLike
|
Path to YAML file defining body segment positions, orientations, and masses. |
DEFAULT_RIGGING_CONFIG_PATH
|
mesh_basedir
|
PathLike
|
Directory containing STL mesh files for body segments. |
DEFAULT_MESH_DIR
|
mujoco_globals_path
|
PathLike
|
Path to YAML file with global MuJoCo parameters. |
DEFAULT_MUJOCO_GLOBALS_PATH
|
root_segment
|
BodySegment | str
|
Root body segment for the kinematic tree. |
'c_thorax'
|
mirror_left2right
|
bool
|
If True, mirror left-side meshes for the right side. |
True
|
mesh_type
|
MeshType
|
Mesh resolution to use. |
SIMPLIFIED_MAX2000FACES
|
geom_fitting_option
|
GeomFittingOption
|
How to fit collision geometries. |
UNMODIFIED
|
vision_config_path
|
PathLike
|
Path to YAML file with vision sensor configuration. |
DEFAULT_VISION_CONFIG_PATH
|
Source code in src/flygym/compose/fly/neuromechfly.py
colorize(visuals_config_path=DEFAULT_VISUALS_CONFIG_PATH)
¶
Apply colors and textures to the NeuroMechFly model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
visuals_config_path
|
PathLike
|
Path to the YAML file defining per-segment material and texture assignments. Defaults to the bundled NeuroMechFly visuals. |
DEFAULT_VISUALS_CONFIG_PATH
|
Source code in src/flygym/compose/fly/neuromechfly.py
NeuroMechFlyWithSeparateTrochanterFemur
¶
Bases: BaseFly
NeuroMechFly with the trochanter and the femur as separate body segments.
In NeuroMechFly, the trochanter and the femur form a single trochanterfemur
segment. Here, they are separate segments (e.g. lf_trochanter and lf_femur),
so that each has its own body and geom (e.g. for segmentation maps). Use
SeparateTrochanterFemurJointPreset and
SeparateTrochanterFemurContactBodiesPreset instead of JointPreset and
ContactBodiesPreset with this model.
The two are often considered to be fused in real flies (at least in Lobato-Rios et
al., 2022), so biological joint presets add no DoF between them and the kinematics
are the same as in NeuroMechFly. The physics is close but not bit-for-bit
identical, because the mass distribution of the two segments is derived from
separate meshes. Joint DoFs of the fused anatomy are renamed: the
coxa-trochanterfemur joint becomes the coxa-trochanter joint, and the
trochanterfemur-tibia joint becomes the femur-tibia joint (see
flygym.anatomy.separate_trochanterfemur_in_dof_name). Poses defined for the fused
anatomy, such as KinematicPosePreset.NEUTRAL, are renamed automatically.
The user can still effectively fuse the two segments by not adding any joint between
them (which is the case by default). When MuJoCo compiler parameter fusestatic
is set to true, the two segment will actually be fused at compile time.
This model will tentatively replace NeuroMechFly in FlyGym 3.0.
Source code in src/flygym/compose/fly/neuromechfly.py
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 | |
colorize(visuals_config_path=DEFAULT_VISUALS_CONFIG_PATH)
¶
get_pose_lookup(neutral_pose)
¶
Same as BaseFly.get_pose_lookup, but DoF names of the fused
trochanterfemur are renamed to their equivalents in this model.
Source code in src/flygym/compose/fly/neuromechfly.py
build_musculoskeletal_gpu_simulation(n_worlds, *, xml_path=DEFAULT_MUSCULOSKELETAL_XML, name='nmf', add_vision=False, **gpu_kwargs)
¶
Build a GPUSimulation of the muscle model with n_worlds parallel
copies for vectorized RL on a CUDA machine.
Call check_mjwarp_compatibility first to verify that the installed
mujoco_warp version supports the muscle model's Hill-type actuators,
spatial tendons, and joint-equality constraints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_worlds
|
int
|
Number of parallel simulation worlds. |
required |
xml_path
|
PathLike
|
Path to the FlyMimic MJCF. Defaults to
|
DEFAULT_MUSCULOSKELETAL_XML
|
name
|
str
|
Logical fly name. Defaults to |
'nmf'
|
add_vision
|
bool
|
Attach eye cameras (approximate; see
|
False
|
**gpu_kwargs
|
Any
|
Forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
GPUSimulation
|
|
MusculoskeletalFly
|
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If the |
Source code in src/flygym/compose/fly/musculoskeletal.py
build_musculoskeletal_simulation(*, xml_path=DEFAULT_MUSCULOSKELETAL_XML, name='nmf', add_vision=False)
¶
Build a MusculoskeletalFly + MusculoskeletalWorld + Simulation.
Equivalent to the standard composition flow::
fly = MusculoskeletalFly(xml_path, name=name)
world = MusculoskeletalWorld(fly)
sim = Simulation(world)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xml_path
|
PathLike
|
Path to the FlyMimic MJCF. Defaults to
|
DEFAULT_MUSCULOSKELETAL_XML
|
name
|
str
|
Logical fly name. Defaults to |
'nmf'
|
add_vision
|
bool
|
If True, attach left/right eye cameras so
|
False
|
Plain flygym — not GPU-accelerated
Returns a plain flygym.Simulation (CPU, single world). For the
GPU path use build_musculoskeletal_gpu_simulation with the
[warp] extra.
Returns:
| Type | Description |
|---|---|
Simulation
|
|
MusculoskeletalFly
|
and fly is the |
tuple[Simulation, MusculoskeletalFly]
|
inspecting |
Source code in src/flygym/compose/fly/musculoskeletal.py
check_mjwarp_compatibility(fly=None, *, xml_path=DEFAULT_MUSCULOSKELETAL_XML)
¶
Probe whether MuJoCo-Warp can ingest the muscle model.
Safe to call anywhere: if mujoco_warp is not installed (the usual case
off a CUDA machine), it returns a report with mjwarp_available=False
rather than raising.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fly
|
MusculoskeletalFly | None
|
A |
None
|
xml_path
|
PathLike
|
XML to use when |
DEFAULT_MUSCULOSKELETAL_XML
|
Returns:
| Type | Description |
|---|---|
MjWarpCompatibilityReport
|
A |