Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing addons #14

Closed
dorp92 opened this issue Sep 12, 2018 · 18 comments
Closed

Importing addons #14

dorp92 opened this issue Sep 12, 2018 · 18 comments
Assignees
Labels
documentation Something in the readme, comments or docstrings could be improved good first issue Good for newcomers question Further information is requested

Comments

@dorp92
Copy link

dorp92 commented Sep 12, 2018

Hello,
I am trying to import an addon called BlendLuxCore (https://luxcorerender.org/download/)
What I tried:

import bpy
import addon_utils
addon_utils.enable(module_name="BlendLuxCore")

And I am getting:

Traceback (most recent call last):
File "C:\Users\dorp\AppData\Local\Continuum\Anaconda3\2.79\scripts\modules\addon_utils.py", line 351, in enable
mod = import(module_name)
File "C:\Users\dorp\AppData\Roaming\Blender Foundation\Blender\2.79\scripts\addons\BlendLuxCore_init_.py", line 13, in
from .bin import pyluxcore
ImportError: DLL load failed: The specified module could not be found.

Now the dll is where it should be:
explorer_2018-09-12_15-18-52
And when I try the same thing from inside blender built in interpreter it works flawlessly:
image
What could be the problem and how can I fix it?

@TylerGubala
Copy link
Owner

TylerGubala commented Sep 12, 2018

Interesting! I'm away on travel work and will have to look at this over the weekend.

So you are doing import bpy import addon_utils?

I think that's invalid syntax, are you actually doing from bpy import addon_utils?

One of the problems here I think is that the Blender python system works very differently than Blender built as a Python module.

That being said, I think your .dll is in the correct place for the Blender addon search method, but I'm not 100% certain that it is in the correct place for the Python .dll path.

I'm going to have to look into this more when I get back from my work trip, but maybe you could try copying the .dll into your site-packages directory.

Let me do more reading and investigation; I'm sure something can be done to simplify the process.

Thanks for your patience!

@TylerGubala
Copy link
Owner

Could you also make sure that your version of bpy is compatible with LuxCore?

import bpy
print (bpy.app.version_string)

@dorp92
Copy link
Author

dorp92 commented Sep 13, 2018

On LuxCore's website it says 2.79b is required,
Your code prints 2.79 (sub 6)
And inside blender it prints 2.79 (sub 0)
I am not sure it's OK but it could be.

import addon_utils works fine after importing bpy.
I will test copying the addon to site packages and report results.
Thanks a lot and keep up the good work!

@dorp92
Copy link
Author

dorp92 commented Sep 13, 2018

I tried to copy the addon to site-packages and some more tricks, nothing seems to work.
It always fail at from .bin import pyluxcore

@TylerGubala
Copy link
Owner

Looking at your image there, you have the LuxCore renderer directory at the Blender application level directory

C:/Users/dorp/AppData/Roaming/Blender Foundation/Blender/2.79/scripts/addons

But really shouldn't this be

{YOUR_VENV_DIR}/Scripts/2.79/addons?

Trying to eliminate everything that I could debug from here.

@dorp92
Copy link
Author

dorp92 commented Sep 13, 2018

I don't think so because blender itself put it there. (After installation from the "user preferences"->"addons" menu)

@TylerGubala
Copy link
Owner

Okay, I will have to look at this over the weekend, then. I'm... not exactly sure what the interplay between the Blender as a Python module and a typical Blender addon would be at the moment. I've been solely focused on getting as many builds available as possible for different python versions.

I'll let you know once I am back at my own machine for testing.

Thanks again!

@TylerGubala TylerGubala self-assigned this Sep 16, 2018
@TylerGubala TylerGubala added the documentation Something in the readme, comments or docstrings could be improved label Sep 16, 2018
@TylerGubala
Copy link
Owner

Here's what I did:

py

Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 27 2018, 03:37:03) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

import bpy

AL lib: (EE) UpdateDeviceParams: Failed to set 44100hz, got 48000hz instead

bpy.ops.wm.addon_install(filepath='C:/Users/TGubs/Downloads/BlendLuxCore/BlendLuxCore-v2.0-win64-opencl.zip')

addon_utils.disable: BlendLuxCore not disabled
Modules Installed (BlendLuxCore) from 'C:/Users/TGubs/Downloads/BlendLuxCore/BlendLuxCore-v2.0-win64-opencl.zip' into 'C:\Users\TGubs\AppData\Roaming\Blender Foundation\Blender\2.79\scripts\addons'
Info: Modules Installed (BlendLuxCore) from 'C:/Users/TGubs/Downloads/BlendLuxCore/BlendLuxCore-v2.0-win64-opencl.zip' into 'C:\Users\TGubs\AppData\Roaming\Blender Foundation\Blender\2.79\scripts\addons'
Info: Modules Installed (BlendLuxCore) from 'C:/Users/TGubs/Downloads/BlendLuxCore/BlendLuxCore-v2.0-win64-opencl.zip' into 'C:\Users\TGubs\AppData\Roaming\Blender Foundation\Blender\2.79\scripts\addons'
{'FINISHED'}

import addon_utils
addon_utils.enable(module_name="BlendLuxCore")

Traceback (most recent call last):
File "C:\Users\TGubs\Code\Python\blender_test\venv\Scripts\2.79\scripts\modules\addon_utils.py", line 351, in enable
mod = import(module_name)
File "C:\Users\TGubs\AppData\Roaming\Blender Foundation\Blender\2.79\scripts\addons\BlendLuxCore_init_.py", line 13, in
from .bin import pyluxcore
ImportError: DLL load failed: The specified module could not be found.

I think this is correct though because here:

LuxCoreRender/BlendLuxCore#138 (comment)

They are saying that the version of Python also matters; you need Python 3.5. I'll try to get the other python version builds working soon.

@dorp92
Copy link
Author

dorp92 commented Sep 17, 2018

Most of the addons are compiled to python 3.5 because that what blender uses at the moment.
Hence they will not work with blenderpy, I am not sure if there is a good solution.
But I would like to suggest that one of the builds should always be aligned with blenders python version. (3.5 at the moment)

Thanks a lot and keep up the good work!

@dorp92
Copy link
Author

dorp92 commented Sep 18, 2018

After compiling BlendLuxCore to python 3.6 it worked flawlessly, Thanks!

@TylerGubala
Copy link
Owner

@dorp92 Great! I'll still get those other builds working though.

Thanks for the awesome example case!

@TylerGubala TylerGubala added good first issue Good for newcomers question Further information is requested labels May 3, 2019
@caniko
Copy link

caniko commented Jul 23, 2019

Hey, I tried to run bpy.ops.wm.addon_enable(module="object_print3d_utils"), but I get:

Exception in module unregister(): 'C:\\Users\\Can\\Projects\\CINPLA\\MyoA\\test\\Scripts\\2.79\\scripts\\addons\\object_print3d_utils\\__init__.py'
Traceback (most recent call last):
  File "C:\Users\Can\Projects\CINPLA\MyoA\test\Scripts\2.79\scripts\modules\addon_utils.py", line 313, in enable
    mod.unregister()
  File "C:\Users\Can\Projects\CINPLA\MyoA\test\Scripts\2.79\scripts\addons\object_print3d_utils\__init__.py", 
line 203, in unregister
    bpy.utils.unregister_class(cls)
RuntimeError: unregister_class(...):, missing bl_rna attribute from 'RNAMeta' instance (may not be registered)

Error: Traceback (most recent call last):
  File "C:\Users\Can\Projects\CINPLA\MyoA\test\Scripts\2.79\scripts\modules\addon_utils.py", line 313, in enable
    mod.unregister()
  File "C:\Users\Can\Projects\CINPLA\MyoA\test\Scripts\2.79\scripts\addons\object_print3d_utils\__init__.py", 
line 203, in unregister
    bpy.utils.unregister_class(cls)
RuntimeError: unregister_class(...):, missing bl_rna attribute from 'RNAMeta' instance (may not be registered)

Error: Traceback (most recent call last):
  File "C:\Users\Can\Projects\CINPLA\MyoA\test\Scripts\2.79\scripts\modules\addon_utils.py", line 313, in enable
    mod.unregister()
  File "C:\Users\Can\Projects\CINPLA\MyoA\test\Scripts\2.79\scripts\addons\object_print3d_utils\__init__.py", 
line 203, in unregister
    bpy.utils.unregister_class(cls)
RuntimeError: unregister_class(...):, missing bl_rna attribute from 'RNAMeta' instance (may not be registered)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Can\Projects\CINPLA\MyoA\test\Scripts\2.79\scripts\modules\bpy\ops.py", line 189, in __call__    ret = op_call(self.idname_py(), None, kw)
RuntimeError: Error: Traceback (most recent call last):
  File "C:\Users\Can\Projects\CINPLA\MyoA\test\Scripts\2.79\scripts\modules\addon_utils.py", line 313, in enable    mod.unregister()  File "C:\Users\Can\Projects\CINPLA\MyoA\test\Scripts\2.79\scripts\addons\object_print3d_utils\__init__.py", line 203, in unregister
    bpy.utils.unregister_class(cls)RuntimeError: unregister_class(...):, missing bl_rna attribute from 'RNAMeta' instance (may not be registered)

Same error with:

import bpy
import addon_utils

addon_utils.enable(module_name="object_print3d_utils")

Error:

Exception in module unregister(): 'C:\\Users\\Can\\Projects\\CINPLA\\MyoA\\test\\Scripts\\2.79\\scripts\\addons\\object_print3d_utils\\__init__.py'
Traceback (most recent call last):
  File "C:\Users\Can\Projects\CINPLA\MyoA\test\Scripts\2.79\scripts\modules\addon_utils.py", line 313, in enable
    mod.unregister()
  File "C:\Users\Can\Projects\CINPLA\MyoA\test\Scripts\2.79\scripts\addons\object_print3d_utils\__init__.py", line 203, in unregister
    bpy.utils.unregister_class(cls)
RuntimeError: unregister_class(...):, missing bl_rna attribute from 'RNAMeta' instance (may not be registered)

@TylerGubala
Copy link
Owner

TylerGubala commented Jul 23, 2019

@caniko2 very strange, I get an altogether different error:

Microsoft Windows [Version 10.0.18362.239]
(c) 2019 Microsoft Corporation. All rights reserved.
C:\Users\TGubs>cd source\repos\blendertest
C:\Users\TGubs\source\repos\blendertest>"venv\scripts\activate"
(venv) C:\Users\TGubs\repos\blendertest>py
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 23 2018, 23:31:17) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> import bpy

AL lib: (EE) UpdateDeviceParams: Failed to set 44000hz, got 48000hz instead

>>>import addon_utils
>>>addon_utils.enable (module_name="object_print3d_utils")

[object_print3d_utils]
3d Print Toolbox: Updating Panel locations has failed
Error:
bpy_prop_collection[key]: key "object_print3d_utils" not found
<module 'object_print3d_utils' from 'C:\Users\TGubs\source\repos\blendertest\venv\Scripts\2.79\scripts\addons\object_print3d_utils\init.py'>

Though

3d Print Toolbox: Updating Panel locations has failed

Makes me think this is because the GUI isn't initialized.

@caniko
Copy link

caniko commented Jul 23, 2019

Hmm, very probable. This add-on runs in the 3D view pane. I need the volume calculation tool of this add-on. What about my error?

Another idea: Is there a way to get one function from the add-on? Maybe go into the source and grab it? Is there a more streamlined way? What do you think? I am talking about this function in particular (it calculates the volume of the active object): bpy.ops.mesh.print3d_info_volume()

I don't have any other option for getting the volume. All I found was this stack forum.

@TylerGubala
Copy link
Owner

TylerGubala commented Jul 23, 2019

@caniko2

If you only need the query volume portion, that should be easy enough to import by itself; the add-on initialization in the 3d view is limited to only the __init__.py of the package while the volume function is in operators.py.

However that doesn't tell the full story; what you actually probably want is the mesh_helpers.py function bmesh_copy_from_object (triangulates faces and cleans up the mesh a bit, providing you a safe handle) followed by bmesh's own calc_volume() function.

Here's what I would do (while being blissfully unaware of what your requirement actually is):

import bpy
import bmesh
from object_print3d_utils.mesh_helpers import bmesh_copy_from_object

# Create an empty mesh and the object.
mesh = bpy.data.meshes.new('Basic_Cube')
basic_cube = bpy.data.objects.new("Basic_Cube", mesh)

# Construct the bmesh cube and assign it to the blender mesh.
bm = bmesh.new()
bmesh.ops.create_cube(bm, size=1.0)
bm.to_mesh(mesh)
bm.free()

bmesh_copy_from_object(basic_cube).calc_volume()

Code for creating the new mesh taken from here

Import your required function, create the object and calculate the volume as necessary.

Some addons are specifically gui based, so your mileage may vary.

@caniko
Copy link

caniko commented Jul 24, 2019

Thanks for the help!

I found the same solution as you did while digging through the source of the add-on. We are doing research on muscle cells. Each blender vertex represents a fluorescence protein, confocal microscopy. I need to calculate the geodesic distance, and get some other futures. Almost done :)

@TylerGubala
Copy link
Owner

TylerGubala commented Jul 24, 2019 via email

@TylerGubala
Copy link
Owner

Information about importing addons has been added to the documentation, updating the readme with an FAQ now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Something in the readme, comments or docstrings could be improved good first issue Good for newcomers question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants