Skip to content

Commit 96233e3

Browse files
committed
Fixed addon file directory as extension
1 parent 0d0ea9a commit 96233e3

File tree

5 files changed

+56
-21
lines changed

5 files changed

+56
-21
lines changed

__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"name": "Script To Button",
77
"author": "RivinHD",
88
"blender": (3, 6, 0),
9-
"version": (2, 3, 0),
9+
"version": (2, 3, 1),
1010
"location": "View3D",
1111
"category": "System",
1212
"doc_url": "https://github.com/RivinHD/ScriptToButton/wiki",

blender_manifest.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ schema_version = "1.0.0"
33
# Example of manifest file for a Blender extension
44
# Change the values according to your extension
55
id = "script_to_button"
6-
version = "2.3.0"
6+
version = "2.3.1"
77
name = "Script To Button"
88
tagline = "Converts scripts to buttons"
99
maintainer = "RivinHD"

functions.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from typing import TYPE_CHECKING, Union
88
import functools
99
from .import dynamic_panels as panels
10+
from . import __package__ as base_package
11+
from .wrapper import get_user_path
1012
if TYPE_CHECKING:
1113
from .preferences import STB_preferences
1214
from .properties import STB_button_properties
@@ -25,24 +27,24 @@
2527

2628

2729
def get_preferences(context: Context) -> STB_preferences:
28-
return context.preferences.addons[__package__].preferences
30+
return context.preferences.addons[base_package].preferences
31+
32+
33+
def get_storage_dir():
34+
return get_user_path(base_package, "Storage", True)
2935

3036

3137
def save_text(active_text: Text, script_name: str) -> None:
3238
text = active_text.as_string()
33-
storage_dir = os.path.join(os.path.dirname(
34-
os.path.abspath(__file__)), "Storage")
35-
if not os.path.isdir(storage_dir):
36-
os.mkdir(storage_dir)
39+
storage_dir = get_storage_dir()
3740
destination = os.path.join(storage_dir, "%s.py" % script_name)
3841
with open(destination, 'w', encoding='utf8') as outfile:
3942
outfile.write(text)
4043

4144

4245
def get_text(script_name: str) -> None:
4346
destination = os.path.join(
44-
os.path.dirname(os.path.abspath(__file__)),
45-
"Storage",
47+
get_storage_dir(),
4648
"%s.py" % script_name
4749
)
4850
if bpy.data.texts.find(script_name) == -1:
@@ -54,10 +56,7 @@ def get_text(script_name: str) -> None:
5456

5557

5658
def get_all_saved_scripts() -> list:
57-
storage_dir = os.path.join(os.path.dirname(
58-
os.path.abspath(__file__)), "Storage")
59-
if not os.path.isdir(storage_dir):
60-
os.mkdir(storage_dir)
59+
storage_dir = get_storage_dir()
6160
scripts = []
6261
for file in os.listdir(storage_dir):
6362
scripts.append(file.replace(".py", ""))
@@ -390,8 +389,7 @@ def remove_button(context: Context, delete_file: bool, delete_text: bool):
390389

391390
if delete_file:
392391
os.remove(os.path.join(
393-
os.path.dirname(__file__),
394-
"Storage",
392+
get_storage_dir(),
395393
"%s.py" % name
396394
))
397395
if delete_text:
@@ -607,8 +605,7 @@ def get_export_text(selection):
607605
if text:
608606
text = text.as_string()
609607
else:
610-
destination = "%s/Storage/%s.py" % (os.path.dirname(
611-
os.path.abspath(__file__)), selection.name)
608+
destination = os.path.join(get_storage_dir(), f"{selection.name}.py")
612609
with open(destination, 'r', encoding="utf-8") as file:
613610
text = file.read()
614611
return text
@@ -707,15 +704,14 @@ def rename(context: Context, name: str):
707704
if bpy.data.texts.find(STB_pref.selected_button) == -1:
708705
get_text(STB_pref.selected_button)
709706
text = bpy.data.texts[STB_pref.selected_button]
710-
directory = os.path.dirname(os.path.abspath(__file__))
711-
old_path = os.path.join(directory, "Storage", "%s.py" %
707+
old_path = os.path.join(get_storage_dir(), "%s.py" %
712708
STB_pref.selected_button)
713709
if name != button.name:
714710
name = check_for_duplicates(get_all_button_names(context).difference([button.name]), name)
715711
button.name = name
716712
button.selected = True
717713
text.name = name
718-
os.rename(old_path, os.path.join(directory, "Storage", "%s.py" % name))
714+
os.rename(old_path, os.path.join(get_storage_dir(), "%s.py" % name))
719715
if not STB_pref.autoload:
720716
bpy.data.texts.remove(text)
721717

preferences.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
from bpy.props import StringProperty, BoolProperty, EnumProperty, CollectionProperty, IntProperty
44
from .functions import get_preferences
55
import rna_keymap_ui
6+
from . import __package__ as base_package
67

78
keymaps = {}
89
keymap_items = []
910

1011

1112
class STB_preferences(AddonPreferences):
12-
bl_idname = __package__
13+
bl_idname = base_package
1314

1415
button_name: StringProperty(
1516
name="Name",

wrapper.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# ===============================================================================
2+
# Wrapper functions for Blender API to support multiple LTS versions of Blender.
3+
# This script should be independent of any local import to avoid circular imports.
4+
#
5+
# Supported Blender versions (Updated: 2024-11-04):
6+
# - 4.2 LTS and above
7+
# - 3.6 LTS
8+
# ===============================================================================
9+
10+
import bpy
11+
from bpy.app import version
12+
13+
import os
14+
15+
16+
def get_user_path(package: str, path: str = '', create: bool = False):
17+
"""
18+
Return a user writable directory associated with an extension.
19+
20+
Args:
21+
package (str): The __package__ of the extension.
22+
path (str, optional): Optional subdirectory. Defaults to ''.
23+
create (bool, optional): Treat the path as a directory and create it if its not existing. Defaults to False.
24+
"""
25+
fallback = os.path.dirname(__file__)
26+
try:
27+
if version >= (4, 2, 0):
28+
return bpy.utils.extension_path_user(package, path=path, create=create)
29+
else:
30+
return fallback # The Fallback path is also the extension user directory for Blender 3.6 LTS.
31+
except ValueError as err:
32+
print("ERROR STB: ValueError: %s" % str(err))
33+
if err.args[0] == "The \"package\" does not name an extension":
34+
print("--> This error might be caused as the addon is installed the first time.")
35+
print(" If this errors remains please try reinstalling the Add-on and report it to the developer.")
36+
37+
print(" Fallback to old extension directory: %s." % fallback)
38+
return fallback

0 commit comments

Comments
 (0)