77from typing import TYPE_CHECKING , Union
88import functools
99from .import dynamic_panels as panels
10+ from . import __package__ as base_package
11+ from .wrapper import get_user_path
1012if TYPE_CHECKING :
1113 from .preferences import STB_preferences
1214 from .properties import STB_button_properties
2527
2628
2729def 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
3137def 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
4245def 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
5658def 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
0 commit comments