Skip to content

Commit

Permalink
Added Python bindings for the Dulcificum bot library
Browse files Browse the repository at this point in the history
Python binding for Dulcificum bot library has been initialized. This includes necessary includes and setup in `pyDulcificum.cpp` file, modification in `CMakeLists.txt` for option to build with Python bindings, and updating the Conan file to include Python bindings option and requirements necessary for these bindings. This will allow Python scripts to directly use Dulcificum's bot commands and functionalities.

Contributes to CURA-10561
  • Loading branch information
jellespijker committed Oct 12, 2023
1 parent 24c2f97 commit 3f593af
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
12 changes: 11 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ find_package(standardprojectsettings REQUIRED)
option(EXTENSIVE_WARNINGS "Build with all warnings" ON)
option(ENABLE_TESTS "Build with unit test" ON)
option(WITH_APPS "Build with apps" ON)
option(WITH_PYTHON_BINDINGS "Build with Python bindings: `pyDulcificum`" ON)

find_package(nlohmann_json REQUIRED)
find_package(spdlog REQUIRED)
Expand Down Expand Up @@ -35,7 +36,16 @@ if (${EXTENSIVE_WARNINGS})
endif ()

# --- Setup Python bindings ---

if (WITH_PYTHON_BINDINGS)
find_package(pybind11 REQUIRED)
find_package(cpython REQUIRED)

pybind11_add_module(pyDulcificum pyDulcificum/pyDulcificum.cpp)
target_link_libraries(pyDulcificum PUBLIC dulcificum pybind11::pybind11 cpython::cpython)
if(NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo)
pybind11_strip(pyArcus)
endif()
endif ()

# --- Setup tests ---
if (ENABLE_TESTS)
Expand Down
18 changes: 18 additions & 0 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ class DulcificumConan(ConanFile):
"fPIC": [True, False],
"enable_extensive_warnings": [True, False],
"with_apps": [True, False],
"with_python_bindings": [True, False],
}
default_options = {
"shared": False,
"fPIC": True,
"enable_extensive_warnings": False,
"with_apps": True,
"with_python_bindings": True,
}

def set_version(self):
Expand Down Expand Up @@ -71,6 +73,7 @@ def export_sources(self):
copy(self, "*", os.path.join(self.recipe_folder, "include"), os.path.join(self.export_sources_folder, "include"))
copy(self, "*", os.path.join(self.recipe_folder, "test"), os.path.join(self.export_sources_folder, "test"))
copy(self, "*", os.path.join(self.recipe_folder, "apps"), os.path.join(self.export_sources_folder, "apps"))
copy(self, "*", os.path.join(self.recipe_folder, "pyDulcificum"), os.path.join(self.export_sources_folder, "pyDulcificum"))
copy(self, "*", os.path.join(self.recipe_folder, "templates"), os.path.join(self.export_sources_folder, "templates"))

def config_options(self):
Expand All @@ -90,6 +93,9 @@ def requirements(self):
self.requires("spdlog/1.10.0")
if self.options.with_apps:
self.requires("docopt.cpp/0.6.3")
if self.options.with_python_bindings:
self.requires("cpython/3.10.4")
self.requires("pybind11/2.10.4")

def build_requirements(self):
self.test_requires("standardprojectsettings/[>=0.1.0]@ultimaker/stable")
Expand All @@ -115,7 +121,19 @@ def generate(self):
tc = CMakeToolchain(self)
tc.variables["ENABLE_TESTS"] = not self.conf.get("tools.build:skip_test", False, check_type = bool)
tc.variables["EXTENSIVE_WARNINGS"] = self.options.enable_extensive_warnings

tc.variables["WITH_APPS"] = self.options.with_apps

tc.variables["WITH_PYTHON_BINDINGS"] = self.options.with_python_bindings
if self.options.with_python_bindings:
tc.variables["Python_EXECUTABLE"] = self.deps_user_info["cpython"].python.replace("\\", "/")
tc.variables["Python_USE_STATIC_LIBS"] = not self.options["cpython"].shared
tc.variables["Python_ROOT_DIR"] = self.deps_cpp_info["cpython"].rootpath.replace("\\", "/")
tc.variables["Python_FIND_FRAMEWORK"] = "NEVER"
tc.variables["Python_FIND_REGISTRY"] = "NEVER"
tc.variables["Python_FIND_IMPLEMENTATIONS"] = "CPython"
tc.variables["Python_FIND_STRATEGY"] = "LOCATION"

if is_msvc(self):
tc.variables["USE_MSVC_RUNTIME_LIBRARY_DLL"] = not is_msvc_static_runtime(self)
tc.cache_variables["CMAKE_POLICY_DEFAULT_CMP0077"] = "NEW"
Expand Down
25 changes: 25 additions & 0 deletions pyDulcificum/pyDulcificum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <dulcificum/miracle_jtp/mgjtp_command_to_json.h>
#include <dulcificum/miracle_jtp/mgjtp_json_to_command.h>
#include <pybind11/pybind11.h>

namespace py = pybind11;

PYBIND11_MODULE(pyDulcificum, module)
{
module.doc() = R"pbdoc(exit
pyDulcificum
-----------------------
.. currentmodule:: Python bindings for Dulcificum
.. autosummary::
:toctree: _generate
add
subtract
)pbdoc";

py::class_<dulcificum::botcmd::Move>(module, "Move")
.def(py::init<>())
.def_readwrite("point", &dulcificum::botcmd::Move::point, "The point of the move command")
.def_readwrite("feedrate", &dulcificum::botcmd::Move::feedrate, "The feedrate of the move command")
.def_readwrite("is_point_relative", &dulcificum::botcmd::Move::is_point_relative, "Components of the point relative")
;
}

0 comments on commit 3f593af

Please sign in to comment.