Package: blender.distutils
This Python module adds a new command todistutilsto buildBlenderaddons:bdist_blender_addon. It also provides a simple mechanism to package extra modules not included with Blender’s Python distribution within the addon.ExampleSee theinfo_example_distutilsaddon to see how thisdistutilsmodule is used.InstallationInstalling theblender.distutilsmoduleThe module is available on PiPy and installable withpip.$pipinstallblender.distutilsIt is suggested to add arequirements.txtfile to the Blender addon plugin that lists the module dependencies.# This is requirements.txt

# This module adds the setup.py bdist_blender_addon command

blender.distutils

# This module is required by the addon, but not distributed with blender
# bdist_blender_addon will ship it if with the addon
# Dependencies to be included are listed in setup.cfg

dateutilsAdd a simplesetup.pyto the blender addonThebdist_blender_addonis adistutilscommand. As such, asetup.pyfile is required. Addon name and version are defined by this file. I suggest usingbumpversionto keepsetup.py,bl_infoand your git tags in sync.Thesetup.pyfor a blender addon is actually quite straightforward. Theinstall_requiresargument should only list the first-level dependencies needed by the addon: those may require their own dependencies. The actual modules to be shipped with the addon are cherry picked insetup.cfg.fromsetuptoolsimportsetup,find_packagessetup(name='info_example_distutils',version='1.0.0',description='Blender example distutils',long_description=open('README.md').read(),url='https://github.com/charlesfleche/blender.distutils/io_example_distutils',author='Charles Flèche',author_email='charles.fleche@free.fr',license='MIT',classifiers=['Development Status :: 5 - Production/Stable','Intended Audience :: End Users/Desktop','Topic :: Multimedia :: Graphics :: 3D Modeling','Topic :: Multimedia :: Graphics :: 3D Rendering','License :: OSI Approved :: MIT License','Programming Language :: Python :: 3 :: Only'],packages=find_packages(),keywords='blender',# Here are listed first level dependencies needed by the module. Themselves# may require dependencies. The actual modules to be shipped with the addon# are cherry picked in setup.cfginstall_requires=['dateutils'])Including third-party modules not shipped with blenderThebdist_blender_addoncommand allows to include additional python
modules that are not shipped with Blender. These modules will be
included in the root folder of the addon. Currently an explicit list of
modules, including their dependencies, needs to be configured.Cherry pick the modules to be shipped with the blender addonThe modules to be included to the blender addon are listed as an option
of the[bdist_blender_addon]section insetup.cfg. This list
includes all the modules and their dependencies.# This is in setup.cfg

[bdist_blender_addon]

# Here are listed the modules (and their dependencies) to be shipped
# with the blender module. In this example the addon requires `dateutils`,
# which in turns requires `dateutil`, `pytz` and `six`.
addon_require = dateutil,dateutils,pytz,sixInclude the additional modules folder in the addon codeThe addon needs to explicitly register the path to third party modules.
During development, those modules will be in a virtual environment. When
the addon is installed in production, those modules will be at the root
of the addon folder.importpathlibimportosimportsiteimportsysdefthird_party_modules_sitedir():# If we are in a VIRTUAL_ENV, while developing for example, we want the# addon to hit the modules installed in the virtual environmentif'VIRTUAL_ENV'inos.environ:env=pathlib.Path(os.environ['VIRTUAL_ENV'])v=sys.version_infopath=env/'lib/python{}.{}/site-packages'.format(v.major,v.minor)# However outside of a virtual environment, the additionnal modules not# shipped with Blender are expected to be found in the root folder of# the addonelse:path=pathlib.Path(__file__).parentreturnstr(path.resolve())# The additionnal modules location (virtual env or addon folder) is# appended heresite.addsitedir(third_party_modules_sitedir())# This module is not part of the standard blender distribution# It is shipped alongside the plugin when `python setup.py bdist_blender_addon`importdateutilsBuild the moduleThebdist_blender_addoncommand will copy the addon code, copy the
additional modules over, clean unneeded files (like the*.pycbytecode files) and package them all in a versioned zip archive under
thedistfolder.$pythonsetup.pybdist_blender_addonrunningbdist_blender_addonrunningbuildrunningbuild_pycreatingbuild/lib/info_example_distutilscopyinginfo_example_distutils/__init__.py->build/lib/info_example_distutilscreatingbuild/lib/info_example_distutils/dateutil[longlistoffilesbeingcopiedoraddedtotheaddonziparchive]$lsdist/info_example_distutils-v1.0.0.zip