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

Fix #168: avoid creating duplicate link for addon #172

Merged
merged 49 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
e6f2f34
Fix #138: avoid creating duplicate link for addon
Mateusz-Grzelinski Jul 22, 2024
408442f
Rever capitalization of global vars
Mateusz-Grzelinski Jul 25, 2024
7c62eca
Support loading from externsion directory
Mateusz-Grzelinski Jul 25, 2024
6866abb
Add patch based tests
Mateusz-Grzelinski Jul 28, 2024
bf3d5a4
Fix tests and compability with python 3.7
Mateusz-Grzelinski Jul 28, 2024
99c7873
Fix addon reloading
Mateusz-Grzelinski Jul 29, 2024
869f0df
Fix type hint
Mateusz-Grzelinski Jul 29, 2024
74f9585
Fix logic for setting up addon links
Mateusz-Grzelinski Jul 29, 2024
3e9e7ee
Remove comment
Mateusz-Grzelinski Jul 29, 2024
b7d756d
Mock call to sys.path
Mateusz-Grzelinski Jul 30, 2024
f7b4f7e
Deduplicate patch calls
Mateusz-Grzelinski Jul 30, 2024
a0f99b6
Update comment
Mateusz-Grzelinski Jul 30, 2024
445778f
Extensions probably does not need sys.path
Mateusz-Grzelinski Jul 30, 2024
750fc41
Remove no longer needed gitignore
Mateusz-Grzelinski Jul 30, 2024
469435c
Fix missing directory
Mateusz-Grzelinski Jul 31, 2024
b803c97
Fix: prevent type hint from failing older verions of blender
Mateusz-Grzelinski Jul 31, 2024
c2e4330
Merge remote-tracking branch 'refs/remotes/jack/master' into master-n…
Mateusz-Grzelinski Aug 2, 2024
af8167a
Cover corner case with extension that can be an addon
Mateusz-Grzelinski Aug 2, 2024
21c4e78
Fix test and test new edge case
Mateusz-Grzelinski Aug 3, 2024
c6ab0fc
Remove link after blender exits
Mateusz-Grzelinski Aug 3, 2024
bbe0c9e
Fix: sys.path was not handled correctly for fresh blender install
Mateusz-Grzelinski Aug 3, 2024
702e4ea
Revert "Remove link after blender exits"
Mateusz-Grzelinski Aug 3, 2024
25b1454
Fix tests
Mateusz-Grzelinski Aug 3, 2024
ee612fe
Fix corner cases
Mateusz-Grzelinski Aug 8, 2024
3b29b68
Merge remote-tracking branch 'refs/remotes/jack/master' into master-c…
Mateusz-Grzelinski Aug 8, 2024
a95d6ee
Fix tests
Mateusz-Grzelinski Aug 9, 2024
50e1d01
Fix blender 2.8 compatibility
Mateusz-Grzelinski Aug 9, 2024
7377fe1
Update changelog and readme
Mateusz-Grzelinski Aug 11, 2024
e901876
Improve error message
Mateusz-Grzelinski Aug 11, 2024
5e5d0f9
Change ugly text rendering
Mateusz-Grzelinski Aug 11, 2024
1cc45ad
Fix blender 2.8 compability
Mateusz-Grzelinski Aug 11, 2024
e958c83
Update README
Mateusz-Grzelinski Aug 11, 2024
26972be
Update README
Mateusz-Grzelinski Aug 11, 2024
7a6c1e8
Update README
Mateusz-Grzelinski Aug 11, 2024
237de93
Update README
Mateusz-Grzelinski Aug 11, 2024
7c17ca3
Update README
Mateusz-Grzelinski Aug 12, 2024
fee5b4c
Resolve junction using windows cmd as fallback method of getting junc…
Mateusz-Grzelinski Aug 12, 2024
4e8601b
Fix tests and blender 2.8 compatibility
Mateusz-Grzelinski Aug 12, 2024
4f45939
Update readme
Mateusz-Grzelinski Aug 12, 2024
6f0dc8f
Merge branch 'master' into master
Mateusz-Grzelinski Aug 17, 2024
ddfc085
Merge remote-tracking branch 'refs/remotes/jack/master'
Mateusz-Grzelinski Aug 17, 2024
6c62c5e
Silence error on linux
Mateusz-Grzelinski Aug 17, 2024
ee18ca1
Update changelog
Mateusz-Grzelinski Aug 17, 2024
4e6bff7
Silence all errors when resolving link
Mateusz-Grzelinski Aug 17, 2024
38e6451
Update README
Mateusz-Grzelinski Aug 17, 2024
ef81feb
Use vscode URI for settings
Mateusz-Grzelinski Aug 14, 2024
3d8c665
Update README
Mateusz-Grzelinski Aug 17, 2024
426d8de
Update README
Mateusz-Grzelinski Aug 17, 2024
21bb909
Update README
Mateusz-Grzelinski Aug 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pythonFiles/include/blender_vscode/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
from dataclasses import dataclass
from typing import List
from pathlib import Path

import bpy
Expand All @@ -11,7 +12,7 @@ class AddonInfo:
module_name: str


def startup(editor_address, addons_to_load: list[AddonInfo], allow_modify_external_python):
def startup(editor_address, addons_to_load: List[AddonInfo], allow_modify_external_python: bool):
if bpy.app.version < (2, 80, 34):
handle_fatal_error("Please use a newer version of Blender")

Expand Down
7 changes: 4 additions & 3 deletions pythonFiles/include/blender_vscode/communication.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
from typing import Dict
import flask
import debugpy
import random
Expand Down Expand Up @@ -88,12 +89,12 @@ def handle_get():
return "OK"


def register_post_handler(type, handler):
def register_post_handler(type: str, handler):
assert type not in post_handlers
post_handlers[type] = handler


def register_post_action(type, handler):
def register_post_action(type: str, handler):
def request_handler_wrapper(data):
run_in_main_thread(partial(handler, data))
return "OK"
Expand All @@ -105,7 +106,7 @@ def request_handler_wrapper(data):
###############################


def send_connection_information(path_mappings):
def send_connection_information(path_mappings: Dict):
send_dict_as_json(
{
"type": "setup",
Expand Down
2 changes: 1 addition & 1 deletion pythonFiles/include/blender_vscode/installation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
cwd_for_subprocesses = python_path.parent


def ensure_packages_are_installed(package_names, allow_modify_external_python):
def ensure_packages_are_installed(package_names, allow_modify_external_python: bool):
if packages_are_installed(package_names):
return

Expand Down
57 changes: 36 additions & 21 deletions pythonFiles/include/blender_vscode/load_addons.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import traceback
from pathlib import Path
from typing import List, Union, Optional, Dict

import bpy

Expand All @@ -11,25 +12,28 @@
from .utils import is_addon_legacy


def setup_addon_links(addons_to_load: list[AddonInfo]):

path_mappings = []
def setup_addon_links(addons_to_load: List[AddonInfo]) -> List[Dict]:
path_mappings: List[Dict] = []

for addon_info in addons_to_load:
user_addon_directory = get_user_addon_directory(Path(addon_info.load_dir))
print(f"USER ADDON: {user_addon_directory}")

if not os.path.exists(user_addon_directory):
os.makedirs(user_addon_directory)

if is_addon_legacy(Path(addon_info.load_dir)) and not str(user_addon_directory) in sys.path:
sys.path.append(str(user_addon_directory))

if is_in_any_addon_directory(addon_info.load_dir):
load_path = addon_info.load_dir
default_directory = get_user_addon_directory(Path(addon_info.load_dir))
if is_addon_legacy(Path(addon_info.load_dir)):
if is_in_any_addon_directory(addon_info.load_dir):
# blender knows about addon and can load it
load_path = addon_info.load_dir
else: # is in external dir of is in extensions dir
load_path = os.path.join(default_directory, addon_info.module_name)
if str(load_path) not in sys.path:
sys.path.append(str(load_path))
Mateusz-Grzelinski marked this conversation as resolved.
Show resolved Hide resolved
create_link_in_user_addon_directory(addon_info.load_dir, load_path)
else:
load_path = os.path.join(user_addon_directory, addon_info.module_name)
create_link_in_user_addon_directory(addon_info.load_dir, load_path)
if is_in_any_extension_directory(Path(addon_info.load_dir)):
# blender knows about extension and can load it
load_path = addon_info.load_dir
else:
os.makedirs(default_directory, exist_ok=True)
load_path = os.path.join(default_directory, addon_info.module_name)
create_link_in_user_addon_directory(addon_info.load_dir, load_path)

path_mappings.append({"src": str(addon_info.load_dir), "load": str(load_path)})

Expand All @@ -44,23 +48,24 @@ def get_user_addon_directory(source_path: Path):
return Path(bpy.utils.user_resource("EXTENSIONS", path="user_default"))


def load(addons_to_load: list[AddonInfo]):
def load(addons_to_load: List[AddonInfo]):
for addon_info in addons_to_load:
if is_addon_legacy(Path(addon_info.load_dir)):
bpy.ops.preferences.addon_refresh()
addon_name = addon_info.module_name
else:
bpy.ops.extensions.repo_refresh_all()
addon_name = "bl_ext.user_default." + addon_info.module_name

repo = is_in_any_extension_directory(addon_info.load_dir)
module = getattr(repo, "module", "user_default")
addon_name = ".".join(("bl_ext", module, addon_info.module_name))
try:
bpy.ops.preferences.addon_enable(module=addon_name)
except Exception:
traceback.print_exc()
send_dict_as_json({"type": "enableFailure", "addonPath": str(addon_info.load_dir)})


def create_link_in_user_addon_directory(directory, link_path):
def create_link_in_user_addon_directory(directory: Union[str, os.PathLike], link_path: Union[str, os.PathLike]):
if os.path.exists(link_path):
os.remove(link_path)

Expand All @@ -72,8 +77,18 @@ def create_link_in_user_addon_directory(directory, link_path):
os.symlink(str(directory), str(link_path), target_is_directory=True)


def is_in_any_addon_directory(module_path):
def is_in_any_addon_directory(module_path: Path) -> bool:
for path in addon_directories:
if path == module_path.parent:
return True
return False


def is_in_any_extension_directory(module_path: Path) -> Optional[bpy.types.UserExtensionRepo]:
for repo in bpy.context.preferences.extensions.repos:
if not repo.enabled:
continue
repo_dir = repo.custom_directory if repo.use_custom_directory else repo.directory
if Path(repo_dir) == module_path.parent:
return repo
return None
6 changes: 5 additions & 1 deletion pythonFiles/include/blender_vscode/operators/addon_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import traceback
from bpy.props import *
from ..utils import is_addon_legacy, redraw_all
from ..load_addons import is_in_any_extension_directory
from ..communication import send_dict_as_json, register_post_action


Expand Down Expand Up @@ -44,7 +45,10 @@ def reload_addon_action(data):
if is_addon_legacy(Path(dir)):
module_names.append(name)
else:
module_names.append("bl_ext.user_default." + name)
repo = is_in_any_extension_directory(Path(dir))
module = getattr(repo, "module", "user_default")
addon_name = ".".join(("bl_ext", module, name))
module_names.append(addon_name)

for name in module_names:
bpy.ops.dev.update_addon(module_name=name)
Expand Down
2 changes: 1 addition & 1 deletion pythonFiles/include/blender_vscode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import traceback


def is_addon_legacy(addon_dir: Path):
def is_addon_legacy(addon_dir: Path) -> bool:
"""Return whether an addon uses the legacy bl_info behavior, or the new blender_manifest behavior"""
if bpy.app.version < (4, 2, 0):
return True
Expand Down
1 change: 1 addition & 0 deletions pythonFiles/tests/blender_vscode/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scripts
Mateusz-Grzelinski marked this conversation as resolved.
Show resolved Hide resolved
Loading