pySimBlocks.project.generate_sofa_controller

pySimBlocks.project.generate_sofa_controller.detect_controller_file_from_scene(scene_file: Path) Path[source]

Determine the controller source file by loading the SOFA scene in a subprocess.

Parameters:

scene_file – Path to the SOFA Python scene file. The scene’s createScene function must return (root, controller).

Returns:

Path to the Python source file that defines the controller class.

Raises:

RuntimeError – If the controller file cannot be determined (e.g. the scene does not return a controller).

pySimBlocks.project.generate_sofa_controller.inject_base_dir(src: str) str[source]

Inject a BASE_DIR declaration after the last import statement if not present.

Parameters:

src – Source code string of the controller file.

Returns:

Source code with BASE_DIR = Path(__file__).resolve().parent injected.

pySimBlocks.project.generate_sofa_controller.inject_project_path_into_controller(controller_file: Path, project_yaml: Path) None[source]

Inject or update the self.project_yaml assignment in the controller __init__.

Parameters:
  • controller_file – Path to the controller Python source file.

  • project_yaml – Path to the project.yaml file to reference from the controller.

pySimBlocks.project.generate_sofa_controller.generate_sofa_controller(project_dir: Path | None = None, project_yaml: Path | None = None) None[source]

Update the SOFA controller file with the project YAML path.

Finds the SOFA scene file from the project, detects the controller class, and injects or replaces the self.project_yaml assignment so the controller can locate the project at runtime.

Exactly one of project_dir or project_yaml must be provided.

Parameters:
  • project_dir – Path to a project folder containing project.yaml.

  • project_yaml – Explicit path to a project.yaml file.

Raises:
  • ValueError – If both or neither of project_dir / project_yaml are given.

  • FileNotFoundError – If project.yaml or the scene file is not found.

  • RuntimeError – If no SOFA block is found in the project or the controller file cannot be detected.