Adding settings with cmake to compile C-Python modules and install them directly in the addon folder
Needs ReviewPublic

Authored by Germano Cavalcante (mano-wii) on Sep 11 2017, 4:23 PM.

Details

Summary

With this patch, python modules made in C can be built together with Blender.

For this, the addon author should follow some small steps:

  1. Add a file with the source code and a CMakeLists.txt containing the macro: blender_add_python_module(name local_dir sources includes includes_sys)
  2. Add a line with add_subdirectory(source_dir) to the CMakeLists.txt which is located in script/addons or script/addons_contrib directory.

It is important to note that the file with the CMakeLists.txt from the addon will be ignored in the installation. Then you should not mix the codes in *.c | *.cpp | *.h with other addon codes in same file.

The final module will be located in the parent directory of the CMakeLists.txt plus the local_dir of macro.

Example:

Diff Detail

Repository
rB Blender
Branch
arcpatch-D2835_1
Build Status
Buildable 1134
Build 1134: arc lint + arc unit
LazyDodo (LazyDodo) added inline comments.
source/blender/python/addon_modules/CMakeLists.txt
32 ↗(On Diff #9254)
  • this should probably move to buildfiles/cmake/macros.cmake
  • also i'd probably prefer blender_add_python_module as macro name to stay in line with existing macro's such as blender_add_library

For now its easiest to review using this layout, longer term though:

What about adding a CMakeLists.txt to the add-ons repository, so each add-on can define it's own C modules in it's own subdirectory.

I expect this would be used mainly for importers parsing file contents which can be slow with Python.
This way authors don't need to commit access to both Blender and the add-ons repository, their modules can be self contained too.

Germano Cavalcante (mano-wii) retitled this revision from Adding settings in cmake to compile C-Python modules and install them directly in addons/modules to Adding settings with cmake to compile C-Python modules and install them directly in the addon folder.
Germano Cavalcante (mano-wii) edited the summary of this revision. (Show Details)
  • New patch following the suggestions mentioned and based on a code provided by @LazyDodo (LazyDodo) on IRC
CMakeLists.txt
1518

There is no need to cache this, it can be a global property, see: DELAYED_INSTALL_FILES

build_files/cmake/macros.cmake
1582–1586

This should take SRC, INC, INC_SYS arguments, since its possible the module has own includes.

The macro can include ${PYTHON_INCLUDE_DIRS}, ${PYTHON_INC} too.

1588

module is too generic, suggest cpython_module_ or py_capi_module_ prefix.

1600

Can't PYTHON_LIBPATH be used here?

1631

Typically you dont need to do this kind of manipulation for installation.
It's also error prone to do this kind of replacement (paths may not be exact duplicates, include /./ or some case difference on WIN32).

Check how this is done elsewhere.

1640

Use if(NOT DEFINED ...)

Germano Cavalcante (mano-wii) marked 4 inline comments as done.
Germano Cavalcante (mano-wii) edited the summary of this revision. (Show Details)
  • use global property;
  • take SRC, INC, INC_SYS arguments in macro;
  • the target was renamed to py_capi_module_<name>;
  • The python includes are taken from the macro (they will stay in the main CMakeLists.txt);
  • IThe install function now has a unique EXCLUDE option with a REGEX that matches all directories that will be skipped in the installation.
Germano Cavalcante (mano-wii) marked an inline comment as done.Sep 14 2017, 6:46 PM
CMakeLists.txt
1529

picky, should be "|/addons_contrib/*"

source/creator/CMakeLists.txt
335

Would rather keep existing PATTERN's - and add REGEX as an extra argument/

Otherwise logic is spread over different places.

  • Minor correction, sync with master
  • Merge branch 'master' into arcpatch-D2835
  • Don't use /Z7 flag on non-win32 OS

This instruction does not work for me. I am trying to modify addon io_mesh_stl for my GSoC 2018 proposal, but my code isn't compiling.
There wasn't CMakeLists.txt in scripts/addons folder, at least in my version of blender which I get by this instruction.
I've created this file by example and modified code. I wrote code to send message to console when stl is importing, but got an exception: ImportError: cannot import name 'io_mesh_stl_c'.
I built code using make command.
When i put manually compiled io_mesh_stl_c.so in build directory, it have worked fine. So, the problem is in building process.
Here is diff of my changes.

@Pavel Krasnov (PKras), it's been a while since I've been working on this patch. I already forgot how it works.
I believe you have to add the CMakeLists.txt file as in the template in the description, (with editions in the directories).

Do you intend to complete this patch? That would be good =)

be sure to run 'make install' so that the files get copied to the right place.

Ok, I see what's going on here. :) This patch is not in master yet.
What I didn't see is where is this patch? In Diff Details says:

Repository rB Blender
Branch arcpatch-D2835_1

but I can't checkout to this branch or find it on this tracker.

Upper right corner there should be a download raw diff link, you can also use arc to apply the patch. That being said, this site is not the right place for basic developer support, you are probably better off on https://devtalk.blender.org.