Skip to content

Commit

Permalink
[SofaPython3] FIST Commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
damienmarchal committed May 21, 2019
1 parent a4db2fc commit caada90
Show file tree
Hide file tree
Showing 110 changed files with 7,168 additions and 0 deletions.
91 changes: 91 additions & 0 deletions CMakeLists.txt
@@ -0,0 +1,91 @@
cmake_minimum_required(VERSION 3.1)
project(SofaPython3 VERSION 1.0)

find_package(SofaFramework REQUIRED)
find_package(SofaSimulation REQUIRED)

set(PYTHON3_LIBRARY "/usr/lib/x86_64-linux-gnu/libpython3.6m.so")
set(PYTHON3_LIBRARIES "/usr/lib/x86_64-linux-gnu/libpython3.6m.so")
set(PYTHON3_INCLUDE_DIR "/usr/include/python3.6/")
set(PYTHON_LIBRARIES ${PYTHON3_LIBRARIES})
set(PYTHON_LIBRARY ${PYTHON3_LIBRARY})
set(PYTHON_INCLUDE_DIRS ${PYTHON3_INCLUDE_DIR})
set(PYTHON_EXECUTABLE python3.6m)

message("-- Python vars: ${PYTHON_INCLUDE_DIRS}")
message("-- Python vars: ${PYTHON_LIBRARIES}")
message("-- Python vars: ${PYTHON_LIBRARY}")

find_package(pybind11 QUIET)
if(NOT pybind11_FOUND)#
message("-- Cannot build the python module 'Sofa', missing the pybind software to generate the bindings.")
return()
endif()

message("-- Python vars: ${PYTHON_INCLUDE_DIRS}")
message("-- Python vars: ${PYTHON_LIBRARIES}")
message("-- Python vars: ${PYTHON_LIBRARY}")


#find_package(pybind11 QUIET)
#if(NOT pybind11_FOUND)#
# message("-- Cannot build the python module 'Sofa', missing the pybind software to generate the bindings.")
# return()
#endif()

set(HEADER_FILES
src/SofaPython3/config.h
src/SofaPython3/initModule.h
src/SofaPython3/PythonEnvironment.h
src/SofaPython3/PythonTest.h
src/SofaPython3/SceneLoaderPY3.h
)

set(SOURCE_FILES
src/SofaPython3/initModule.cpp
src/SofaPython3/PythonEnvironment.cpp
src/SofaPython3/PythonTest.cpp
src/SofaPython3/SceneLoaderPY3.cpp
)

set(EXTRA_FILES
SofaPython3Config.cmake.in
)

add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES} ${EXTRA_FILES})
set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-DSOFA_BUILD_SOFAPYTHON3")

target_link_libraries(${PROJECT_NAME} PUBLIC SofaCore SofaSimulationCore SofaSimulationGraph)

if(CMAKE_SYSTEM_NAME STREQUAL Linux)
# dlopen() is used on Linux for a workaround (see PythonEnvironnement.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE dl)
endif()

target_include_directories(${PROJECT_NAME} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/>")
target_include_directories(${PROJECT_NAME} PUBLIC "$<INSTALL_INTERFACE:include>")

target_link_libraries(${PROJECT_NAME} PRIVATE "${PYTHON_LIBRARY}" )
target_include_directories(${PROJECT_NAME} PRIVATE "${PYTHON_INCLUDE_DIR}")

## When installing, copy the content of the example directory into module/SofaPython3
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/examples DESTINATION modules/SofaPython3 COMPONENT resources)

## When installing, copy the target into module/SofaPython3
install(TARGETS ${PROJECT_NAME} DESTINATION modules/SofaPython3)

# Config files and install rules for pythons scripts
sofa_set_python_directory(${PROJECT_NAME} "python")

# The signature of sofa_create_package is
# sofa_create_package(package_name, version, the_targets, <include_subdir>)
# This function assumes that there is a FooConfig.cmake.in file template in the source directory.
# This function is an all in one replacement for install(TARGET), install(EXPORT), and also add
# the example directory in the INSTALL set.
sofa_create_package(${PROJECT_NAME} ${PROJECT_VERSION} SofaPython3 "SofaPython3")

### Tests
#add_subdirectory(tests)

### Python binding
add_subdirectory(bindings)
12 changes: 12 additions & 0 deletions SofaPython3Config.cmake.in
@@ -0,0 +1,12 @@
# CMake package configuration file for the @PROJECT_NAME@ module
@PACKAGE_INIT@
find_package(SofaFramework REQUIRED)

# If we are importing this config file and the target is not yet there this is indicating that
# target is an imported one. So we include it
if(NOT TARGET @PROJECT_NAME@)
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
endif()

# Check that the component/target is there.
check_required_components(@PROJECT_NAME@)
10 changes: 10 additions & 0 deletions bindings/CMakeLists.txt
@@ -0,0 +1,10 @@
find_package(pybind11 QUIET)
if(NOT pybind11_FOUND)#
message("-- Cannot build the python module 'Sofa', missing the pybind software to generate the bindings.")
return()
endif()

add_subdirectory(Sofa)

#add_subdirectory(SofaRuntime)
#add_subdirectory(SofaTypes)
91 changes: 91 additions & 0 deletions bindings/Sofa/CMakeLists.txt
@@ -0,0 +1,91 @@
cmake_minimum_required(VERSION 3.1)
project(SofaPython3_Sofa VERSION 1.0)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

####################################################################################################
### Module dependencies
####################################################################################################
find_package(SofaPython3 REQUIRED)

# This must be done before doing the find_package(pybind11)
#set(PYBIND11_PYTHON_VERSION 3.6m)
#set(PYTHON_EXECUTABLE python3)

set(HEADER_FILES
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Core/Binding_Base.h
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Core/Binding_BaseData.h
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Core/Binding_BaseObject.h
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Core/Binding_DataContainer.h
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Core/Binding_PythonController.h
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Core/Binding_ForceField.h
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Core/Binding_Visitor.h

${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Core/DataCache.h
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Core/DataHelper.h
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Core/Submodule_Core.h

${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Helper/Submodule_Helper.h

${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Simulation/Binding_Node.h
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Simulation/Binding_Simulation.h
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Simulation/Submodule_Simulation.h
)

set(SOURCE_FILES
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Core/Binding_Base.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Core/Binding_BaseData.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Core/Binding_BaseObject.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Core/Binding_DataContainer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Core/Binding_PythonController.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Core/Binding_ForceField.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Core/Binding_Visitor.cpp

${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Core/DataCache.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Core/DataHelper.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Core/Submodule_Core.cpp

${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Helper/Submodule_Helper.cpp

${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Simulation/Binding_Node.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Simulation/Binding_Simulation.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Simulation/Submodule_Simulation.cpp

${CMAKE_CURRENT_SOURCE_DIR}/src/SofaPython3/Sofa/Module_Sofa.cpp
)

set(PYTHON_FILES
package/__Sofa_Types__/__init__.py
package/__Sofa_Types__/RGBAColor.py
package/__Sofa_Types__/Vec3.py
)


sofa_add_pybind11_module(
TARGET SofaPython3_Sofa
SOURCES ${SOURCE_FILES} ${HEADER_FILES} ${PYTHON_FILES}
DEPENDS SofaCore SofaSimulationGraph SofaPython3 pybind11::module
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/package/"
NAME Sofa
)

target_include_directories(SofaPython3_Sofa PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>/src")
target_include_directories(SofaPython3_Sofa PUBLIC "$<INSTALL_INTERFACE:include>")

## Python configuration file (build tree), the lib in the source dir (easier while developping .py files)
file(WRITE "${CMAKE_BINARY_DIR}/etc/sofa/python.d/Sofa" "${CMAKE_CURRENT_SOURCE_DIR}/package/")

## Python configuration file (install tree)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/installed-Sofa-config"
"modules/Sofa/python/")

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/installed-Sofa-config"
DESTINATION "etc/sofa/python.d"
RENAME "Sofa")

configure_file("${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in"
"${CMAKE_BINARY_DIR}/cmake/${PROJECT_NAME}Config.cmake")

add_subdirectory(tests)
5 changes: 5 additions & 0 deletions bindings/Sofa/SofaPython3_SofaConfig.cmake.in
@@ -0,0 +1,5 @@
#CMake package configuration file for the @PROJECT_NAME@ plugin

@PACKAGE_INIT@

set(SOFAPYTHON3_BINDING_SOFA_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/package/Sofa")
14 changes: 14 additions & 0 deletions bindings/Sofa/examples/example1.py
@@ -0,0 +1,14 @@
print(".EXAMPLE SCENE.")

def createScene(rootNode):
print("COUCOU")
print("Type: "+str(type(rootNode)))
print("Name: "+str(type(rootNode.getName())))

one = rootNode.createChild("Child1")
two = rootNode.createChild("Child2")

print("T:" + str(one.getName()))
print("T:" + str(two.getName()))

return rootNode
52 changes: 52 additions & 0 deletions bindings/Sofa/package/__Sofa_Types__/RGBAColor.py
@@ -0,0 +1,52 @@
import numpy

class RGBAColor(numpy.ndarray):
"""A wrapping-type to manipulate Sofa data field as an RGBAColor.
Eg:
n = Sofa.Node("root")
m = n.addObject("MechanicalObject")
c = RGBAColor(m.showColor)
print("Red is: ", c.r())
"""

def __new__(cls, input_array=None):
import Sofa
if input_array is None:
obj = super(RGBAColor, cls).__new__(cls, shape=(4), dtype=float)
obj[0] = obj[1] = obj[2] = obj[3] = 0
return obj

if isinstance(input_array, list):
if len(input_array) not in [4]:
raise ValueError("The list is too long. Only size 4 list is supported")

obj = super(RGBAColor, cls).__new__(cls, shape=(4), dtype=float)
numpy.copyto(obj, numpy.asarray(input_array))
return obj

if isinstance(input_array, Sofa.Core.DataContainer):
cls.owner = input_array
input_array = input_array.toarray()

if input_array.ndim != 1:
raise ValueError("Invalid dimension, expecting a 1D array, got "+str(input_array.ndim)+"D")

# Input array is an already formed ndarray instance
# We first cast to be our class type
obj = numpy.asarray(input_array).view(cls)

# Finally, we must return the newly created object:
return obj

def r(self):
return self[0]

def g(self):
return self[1]

def b(self):
return self[2]

def a(self):
return self[3]

0 comments on commit caada90

Please sign in to comment.