diff --git a/photon-lib/py/create_photonlib_pyi.py b/photon-lib/py/create_photonlib_pyi.py index b0cd2a645e..9a2447df2b 100644 --- a/photon-lib/py/create_photonlib_pyi.py +++ b/photon-lib/py/create_photonlib_pyi.py @@ -17,8 +17,9 @@ def write_stubgen(): # f.write(sg.get()) - # mess with argv + # mess with argv (and put it back when we're done) import sys + old_argv = sys.argv sys.argv = [ "", "--exit-code", @@ -29,8 +30,13 @@ def write_stubgen(): "photonlibpy", ] - import pybind11_stubgen - pybind11_stubgen.main() + try: + import pybind11_stubgen + pybind11_stubgen.main() + except Exception as e: + print(e) + + sys.argv = old_argv if __name__ == "__main__": diff --git a/photon-lib/py/photonlibpy/__init__.pyi b/photon-lib/py/photonlibpy/__init__.pyi index 9d688079a7..c249f382e9 100644 --- a/photon-lib/py/photonlibpy/__init__.pyi +++ b/photon-lib/py/photonlibpy/__init__.pyi @@ -2,6 +2,8 @@ from __future__ import annotations import hal as hal import ntcore as ntcore from photonlibpy._photonlibpy import PhotonPipelineMetadata +from photonlibpy._photonlibpy import Translation3d +from photonlibpy._photonlibpy import print_t import platform as platform import robotpy_apriltag as robotpy_apriltag import wpilib as wpilib @@ -9,4 +11,4 @@ import wpimath as wpimath import wpinet as wpinet import wpiutil as wpiutil from . import _photonlibpy -__all__ = ['PhotonPipelineMetadata', 'hal', 'ntcore', 'platform', 'robotpy_apriltag', 'wpilib', 'wpimath', 'wpinet', 'wpiutil'] +__all__ = ['PhotonPipelineMetadata', 'Translation3d', 'hal', 'ntcore', 'platform', 'print_t', 'robotpy_apriltag', 'wpilib', 'wpimath', 'wpinet', 'wpiutil'] diff --git a/photon-lib/py/photonlibpy/_photonlibpy.pyi b/photon-lib/py/photonlibpy/_photonlibpy.pyi index 66b5aa4213..3770b39491 100644 --- a/photon-lib/py/photonlibpy/_photonlibpy.pyi +++ b/photon-lib/py/photonlibpy/_photonlibpy.pyi @@ -2,8 +2,10 @@ C++ bindings for photonlib """ from __future__ import annotations -__all__ = ['PhotonPipelineMetadata'] +__all__ = ['PhotonPipelineMetadata', 'Translation3d', 'print_t'] class PhotonPipelineMetadata: + def __init__(self) -> None: + ... @property def captureTimestampMicros(self) -> int: ... @@ -13,3 +15,10 @@ class PhotonPipelineMetadata: @property def sequenceID(self) -> int: ... +class Translation3d: + def __init__(self, arg0: float, arg1: float, arg2: float) -> None: + ... +def print_t(arg0: Translation3d) -> None: + """ + Print a frc Translation3d + """ diff --git a/photon-lib/py/setup.py b/photon-lib/py/setup.py index 633f1820bb..048ef9cee5 100644 --- a/photon-lib/py/setup.py +++ b/photon-lib/py/setup.py @@ -68,14 +68,14 @@ def finalize_options(self): self.root_is_pure = False -# script_path = os.path.dirname(os.path.realpath(__file__)) -# if not os.path.exists(f"{script_path}/photonlibpy/_photonlibpy.pyi"): -# print("Generating typehints") -# # try: -# # from create_photonlib_pyi import write_stubgen -# # write_stubgen() -# # except Exception as e: -# # print(e) +script_path = os.path.dirname(os.path.realpath(__file__)) +if not os.path.exists(f"{script_path}/photonlibpy/_photonlibpy.pyi"): + print("Generating typehints") + try: + from create_photonlib_pyi import write_stubgen + write_stubgen() + except Exception as e: + print(e) setup( name="photonlibpy", diff --git a/photon-lib/src/main/pybindings/cpp/photonlib_pybind.cpp b/photon-lib/src/main/pybindings/cpp/photonlib_pybind.cpp index 8749937c57..6a5456bede 100644 --- a/photon-lib/src/main/pybindings/cpp/photonlib_pybind.cpp +++ b/photon-lib/src/main/pybindings/cpp/photonlib_pybind.cpp @@ -24,17 +24,34 @@ #include #include "photon/PhotonCamera.h" +#include + +void print_t(frc::Translation3d t) { + fmt::println("x {} y {} z {}", t.X(), t.Y(), t.Z()); +} // actual nanobind include #include + namespace py = pybind11; PYBIND11_MODULE(_photonlibpy, m) { m.doc() = "C++ bindings for photonlib"; + // hack -- is there any way to pull this from robotpy instead? + py::class_(m, "Translation3d") + .def(py::init([](double x, double y, double z) { return frc::Translation3d{ + units::meter_t{x}, + units::meter_t{y}, + units::meter_t{z}, + }; })); + + m.def("print_t", &print_t, "Print a frc Translation3d"); + py::class_(m, "PhotonPipelineMetadata") + .def(py::init<>()) .def_readonly("sequenceID", &photon::PhotonPipelineMetadata::sequenceID) .def_readonly("captureTimestampMicros", &photon::PhotonPipelineMetadata::captureTimestampMicros)