2

I am developing an addon that uses some modules that are not installed in Python in Blender by default such as cv2 and PIL. On my pc the script can run due to the fact I installed both modules in Python, however, when installing the addon on other pcs, blender raises the ModuleNotFoundError because such modules are not loaded into that specific Python.

ModuleNotFoundError

How could I handle this situation? I have been trying to figure out how could I install the modules before the program execution. Yet I have not been able to solve it.

The code to this extent is structured like this:

import bpy
import os
from PIL import Image
import cv2 as cv

bl_info = { "name": "New Object", "author": "Your Name Here", "version": (1, 0), "category": "Add Mesh" )

class MESH_OT_pinguin(bpy.types.Operator): ... def execute(self, context): ... #Here goes the script that uses such libraries

def menu_func(self, context): self.layout.operator(MESH_OT_pinguin.bl_idname, icon='MESH_CUBE')

def register(): bpy.utils.register_class(MESH_OT_pinguin) bpy.types.VIEW3D_MT_mesh_add.append(menu_func)

def unregister(): bpy.utils.unregister_class(MESH_OT_pinguin) bpy.types.VIEW3D_MT_mesh_add.remove(menu_func)

if name == "main": register()

George
  • 33
  • 3
  • How did you install those modules? via pip install? can u not run pip install while installing your addon? – Harry McKenzie Apr 20 '23 at 16:24
  • As far as I know this question doesn't have a good answer at the moment. Probably the best option is to tell the user to manually download the dependencies using pip. There is an old open issue here with some possible solutions: https://projects.blender.org/blender/blender/issues/71420 but it doesn't appear anyone is working actively on it – Linus Apr 26 '23 at 21:03
  • I have figured to use the subprocess module to use pip installation at the beginning of the script. As soon as the checkbox of the addon is toggled, the program tries to load the aforementioned modules if the error is raised it uses the subprocess function to launch the installation of the modules automatically. Currently, it seems to be working for my objective. try: from PIL import Image # pip install Pillow except: subprocess.check_call([sys.executable, '-m', 'pip', 'install',"Pillow"]) from PIL import Image – George Apr 28 '23 at 14:44

0 Answers0