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

Installing the Blender extension programmatically #79

Closed
set-soft opened this issue Nov 6, 2024 · 12 comments
Closed

Installing the Blender extension programmatically #79

set-soft opened this issue Nov 6, 2024 · 12 comments
Assignees
Labels
question Further information is requested

Comments

@set-soft
Copy link

set-soft commented Nov 6, 2024

Hi!
I'm trying to create fresh docker images for KiBot using the last release and Blender 4.2.3 LTS.
I want to programmatically install the addon.
The code for Blender 3.5 doesn't work:

import bpy

# Register the addon and enable it
bpy.context.preferences.filepaths.script_directory = '/usr/bin/4.2/scripts/'
bpy.ops.preferences.addon_install(filepath='./pcb2blender_importer_2-14.zip', target='PREFS')
bpy.ops.preferences.addon_enable(module='pcb2blender_importer')
bpy.ops.wm.save_userpref()

It complains on the first assignment. I couldn't find much information about how to do it with the new API, and ChatGPT suggests using:

import bpy

addon_path = "/var/pcb3d_importer.zip"
addon_name = "pcb3d_importer"
bpy.ops.preferences.addon_install(filepath=addon_path)
bpy.ops.preferences.addon_enable(module=addon_name)
bpy.ops.wm.save_userpref()
# Verify the addon is enabled
addon = bpy.context.preferences.addons.get(addon_name)
if addon:
    print(f"Addon '{addon_name}' installed and enabled successfully!")
else:
    print(f"Failed to enable '{addon_name}'.")

Which kind of work ...
I'm installing the dependencies by hand, so I don't need the magic that installs them.
The problems are:

  1. This method needs the old zip layout, the addon must be inside a folder, not at the root of the zip file
  2. The init.py must contain bl_info, otherwise is installed but fails to be detected correctly

I'm repacking the zip file and patching it with the bl_info (adapter from older releases), it works, but I wonder if you know a better way that doesn't need this manipulation.
I

@30350n
Copy link
Owner

30350n commented Nov 6, 2024

I couldn't find much information about how to do it with the new API ...

You can install extensions via the Blender CLI, either by name from the official repository (recommended) or directly from a file.

blender --command extension install --enable pcb3d-importer should do the trick I'm pretty sure.

I'm installing the dependencies by hand, so I don't need the magic that installs them.

There's no "magic" anymore, the dependency wheels are bundled in the extension now and get installed by Blender. Installing them manually might or might not cause issues.

Don't think LLMs are even remotely useful for stuff like this btw 😉

@30350n 30350n self-assigned this Nov 6, 2024
@30350n 30350n added the question Further information is requested label Nov 6, 2024
@30350n 30350n changed the title [Question] Installing the Blender extension programmatically Nov 6, 2024
@set-soft
Copy link
Author

You can install extensions via the Blender CLI, either by name from the official repository (recommended) or directly from a file.

blender --command extension install --enable pcb3d-importer should do the trick I'm pretty sure.

Thanks for the pointer, I had to use:

blender --online-mode --command extension sync 
blender --online-mode --command extension install --enable pcb3d_importer

Note the change in the name

@30350n
Copy link
Owner

30350n commented Nov 13, 2024

Ah yeah, that makes sense. You need --online-mode and --command extension sync because you are probably on a fresh install.

Is everything working now for you? Any issues with dependencies?

@set-soft
Copy link
Author

Is everything working now for you? Any issues with dependencies?

Nope, isn't working (from CLI):

Blender 4.2.3 LTS (hash 0e22e4fcea03 built 2024-10-14 23:31:34)
Importing PCB3D file ...
Exception in module register(): /root/.config/blender/4.2/extensions/blender_org/pcb3d_importer/__init__.py
Traceback (most recent call last):
  File "/usr/bin/4.2/scripts/modules/addon_utils.py", line 488, in enable
    mod.register()
  File "/root/.config/blender/4.2/extensions/blender_org/pcb3d_importer/__init__.py", line 12, in register
    _modules.append(importlib.import_module(f".{module_name}", package=__package__))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/bin/4.2/python/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/root/.config/blender/4.2/extensions/blender_org/pcb3d_importer/importer.py", line 9, in <module>
    from error_helper import error, warning
ModuleNotFoundError: No module named 'error_helper'

And seems to be a reported bug: Calling addon_utils.enable(..) fails on extensions with wheels

I'll look for workarounds

@30350n
Copy link
Owner

30350n commented Nov 13, 2024

Ah okay, well seems like they fixed it already at least. (Should be part of 4.3, as well as the next 4.2 bugfix release).

@set-soft
Copy link
Author

Ah okay, well seems like they fixed it already at least. (Should be part of 4.3, as well as the next 4.2 bugfix release).

Has milestone set to 4.3 ... I hope they include it in the LTS.

@set-soft
Copy link
Author

I manually installed the dependencies and I got a little bit farther, now I must figure out how to call importer.
In previous versions I used bpy.ops.pcb2blender.import_pcb3d but now bpy.ops.pcb3d_importer.import_pcb3d says:

Traceback (most recent call last):
  File "/home/salvador/0Data/Eccosur/kibot/kibot/blender_scripts/blender_export.py", line 443, in <module>
    main()
  File "/home/salvador/0Data/Eccosur/kibot/kibot/blender_scripts/blender_export.py", line 406, in main
    bpy.ops.pcb3d_importer.import_pcb3d(**ops)
  File "/usr/bin/4.2/scripts/modules/bpy/ops.py", line 109, in __call__
    ret = _op_call(self.idname_py(), kw)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: Calling operator "bpy.ops.pcb3d_importer.import_pcb3d" error, could not be found

@30350n
Copy link
Owner

30350n commented Nov 13, 2024

Yeah, it's in here.

image

@30350n
Copy link
Owner

30350n commented Nov 13, 2024

I manually installed the dependencies and I got a little bit farther, now I must figure out how to call importer.
In previous versions I used bpy.ops.pcb2blender.import_pcb3d

That should still work.

@set-soft
Copy link
Author

Ok, it works with the old name.

I'm installing the wheels manually to workaround the 4.2.3 bug.

Thanks!

@30350n
Copy link
Owner

30350n commented Nov 22, 2024

4.2.4 and 4.3 are out now btw. 4.2.4 should work flawlessly, while 4.3 is not supported yet.

I will try to keep supporting 4.2 LTS from now on btw, as well as the newest non LTS version.

@set-soft
Copy link
Author

Just a note: when I tried 4.2.3 and v2.16 the name changed from "pcb3d_importer" to "bl_ext.blender_org.pcb3d_importer"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants