Skip to content

Commit

Permalink
fix template
Browse files Browse the repository at this point in the history
  • Loading branch information
BradyAJohnston committed Sep 13, 2023
1 parent 779f414 commit 66eff1a
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 117 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test:
version := $(shell grep version pyproject.toml | grep -o -E "\b[0-9]+\.[0-9]+\.[0-9]+\b")

template:
zip -r MolecularNodes/assets/template/Molecular_Nodes.zip MolecularNodes/assets/template/Molecular_Nodes -x *blend1
cd MolecularNodes/assets/template && zip -r MolecularNodes.zip MolecularNodes -x *blend1

release:
git clean -dfX
Expand Down
7 changes: 2 additions & 5 deletions MolecularNodes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


bl_info = {
"name" : "MolecularNodes",
"author" : "Brady Johnston",
Expand All @@ -28,16 +27,14 @@
from . import auto_load
from .ui import MN_add_node_menu
import bpy
import pathlib
import os
from . import pref
from . import utils

auto_load.init()

def register():
auto_load.register()
bpy.types.NODE_MT_add.append(MN_add_node_menu)
pref.template_install()
utils.template_install()

def unregister():
bpy.types.NODE_MT_add.remove(MN_add_node_menu)
Expand Down
Binary file modified MolecularNodes/assets/MN_data_file.blend
Binary file not shown.
11 changes: 11 additions & 0 deletions MolecularNodes/assets/blender_assets.cats.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This is an Asset Catalog Definition file for Blender.
#
# Empty lines and lines starting with `#` will be ignored.
# The first non-ignored line should be the version indicator.
# Other lines are of the format "UUID:catalog/path/for/assets:simple catalog name"

VERSION 1

11e51ada-a84c-4c87-baee-eaa0385c99c1:Molecular Nodes:Molecular Nodes
427147b8-bbce-4652-aca5-f21b1224c1ca:Molecular Nodes/Color:Molecular Nodes-Color
4d52a503-3b55-43c2-b9ec-ff4fcbeda65b:Molecular Nodes/Style:Molecular Nodes-Style
Binary file not shown.
2 changes: 1 addition & 1 deletion MolecularNodes/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
description = 'Location to cache PDB files',
options = {'TEXTEDIT_UPDATE'},
default = str(Path('~', '.MolecularNodes').expanduser()),
subtype = 'NONE'
subtype = 'FILE_PATH'
)
bpy.types.Scene.MN_import_center = bpy.props.BoolProperty(
name = "MN_import_centre",
Expand Down
110 changes: 0 additions & 110 deletions MolecularNodes/pref.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import bpy
import os
import traceback
import zipfile
import pathlib
from . import pkg
from bpy.types import AddonPreferences
from bpy.app.translations import pgettext_tip as tip_

install_instructions = "https://bradyajohnston.github.io/MolecularNodes/installation.html#installing-biotite-mdanalysis"
ADDON_DIR = pathlib.Path(__file__).resolve().parent
Expand Down Expand Up @@ -71,109 +67,3 @@ def draw(self, context):
box.operator(
"wm.url_open", text = "Installation Instructions", icon = 'HELP'
).url = install_instructions

def _module_filesystem_remove(path_base, module_name):
# taken from the bpy.ops.preferences.app_template_install() operator source code
# Remove all Python modules with `module_name` in `base_path`.
# The `module_name` is expected to be a result from `_zipfile_root_namelist`.
import os
import shutil
module_name = os.path.splitext(module_name)[0]
for f in os.listdir(path_base):
f_base = os.path.splitext(f)[0]
if f_base == module_name:
f_full = os.path.join(path_base, f)
if os.path.isdir(f_full):
shutil.rmtree(f_full)
else:
os.remove(f_full)

def _zipfile_root_namelist(file_to_extract):
# taken from the bpy.ops.preferences.app_template_install() operator source code
# Return a list of root paths from zipfile.ZipFile.namelist.
import os
root_paths = []
for f in file_to_extract.namelist():
# Python's `zipfile` API always adds a separate at the end of directories.
# use `os.path.normpath` instead of `f.removesuffix(os.sep)`
# since paths could be stored as `./paths/./`.
#
# Note that `..` prefixed paths can exist in ZIP files but they don't write to parent directory when extracting.
# Nor do they pass the `os.sep not in f` test, this is important,
# otherwise `shutil.rmtree` below could made to remove directories outside the installation directory.
f = os.path.normpath(f)
if os.sep not in f:
root_paths.append(f)
return root_paths

def template_install():
template = os.path.join(os.path.abspath(ADDON_DIR), 'assets', 'template', 'Molecular_Nodes.zip')
_install_template(template)
bpy.utils.refresh_script_paths()

def template_uninstall():
import shutil
for folder in bpy.utils.app_template_paths():
path = os.path.join(os.path.abspath(folder), 'Molecular_Nodes')
if os.path.exists(path):
shutil.rmtree(path)
bpy.utils.refresh_script_paths()

def _install_template(filepath, overwrite = True):
# taken from the bpy.ops.preferences.app_template_install() operator source code

path_app_templates = bpy.utils.user_resource(
'SCRIPTS',
path=os.path.join("startup", "bl_app_templates_user"),
create=True,
)

if not os.path.isdir(path_app_templates):
try:
os.makedirs(path_app_templates, exist_ok=True)
except:
traceback.print_exc()

app_templates_old = set(os.listdir(path_app_templates))

# check to see if the file is in compressed format (.zip)
if zipfile.is_zipfile(filepath):
try:
file_to_extract = zipfile.ZipFile(filepath, 'r')
except:
traceback.print_exc()
return {'CANCELLED'}

file_to_extract_root = _zipfile_root_namelist(file_to_extract)
if overwrite:
for f in file_to_extract_root:
_module_filesystem_remove(path_app_templates, f)
else:
for f in file_to_extract_root:
path_dest = os.path.join(path_app_templates, os.path.basename(f))
if os.path.exists(path_dest):
# self.report({'WARNING'}, tip_("File already installed to %r\n") % path_dest)
return {'CANCELLED'}

try: # extract the file to "bl_app_templates_user"
file_to_extract.extractall(path_app_templates)
except:
traceback.print_exc()
return {'CANCELLED'}

else:
# Only support installing zipfiles
print('no zipfile')
return {'CANCELLED'}

app_templates_new = set(os.listdir(path_app_templates)) - app_templates_old

# in case a new module path was created to install this addon.
bpy.utils.refresh_script_paths()

# print message
msg = (
tip_("Template Installed (%s) from %r into %r") %
(", ".join(sorted(app_templates_new)), filepath, path_app_templates)
)
print(msg)
112 changes: 112 additions & 0 deletions MolecularNodes/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import bpy
import traceback
import os
import zipfile
from .pref import ADDON_DIR
from bpy.app.translations import pgettext_tip as tip_

def _module_filesystem_remove(path_base, module_name):
# taken from the bpy.ops.preferences.app_template_install() operator source code
# Remove all Python modules with `module_name` in `base_path`.
# The `module_name` is expected to be a result from `_zipfile_root_namelist`.
import os
import shutil
module_name = os.path.splitext(module_name)[0]
for f in os.listdir(path_base):
f_base = os.path.splitext(f)[0]
if f_base == module_name:
f_full = os.path.join(path_base, f)
if os.path.isdir(f_full):
shutil.rmtree(f_full)
else:
os.remove(f_full)

def _zipfile_root_namelist(file_to_extract):
# taken from the bpy.ops.preferences.app_template_install() operator source code
# Return a list of root paths from zipfile.ZipFile.namelist.
import os
root_paths = []
for f in file_to_extract.namelist():
# Python's `zipfile` API always adds a separate at the end of directories.
# use `os.path.normpath` instead of `f.removesuffix(os.sep)`
# since paths could be stored as `./paths/./`.
#
# Note that `..` prefixed paths can exist in ZIP files but they don't write to parent directory when extracting.
# Nor do they pass the `os.sep not in f` test, this is important,
# otherwise `shutil.rmtree` below could made to remove directories outside the installation directory.
f = os.path.normpath(f)
if os.sep not in f:
root_paths.append(f)
return root_paths

def template_install():
template = os.path.join(os.path.abspath(ADDON_DIR), 'assets', 'template', 'MolecularNodes.zip')
_install_template(template)
bpy.utils.refresh_script_paths()

def template_uninstall():
import shutil
for folder in bpy.utils.app_template_paths():
path = os.path.join(os.path.abspath(folder), 'MolecularNodes')
if os.path.exists(path):
shutil.rmtree(path)
bpy.utils.refresh_script_paths()

def _install_template(filepath, overwrite = True):
# taken from the bpy.ops.preferences.app_template_install() operator source code

path_app_templates = bpy.utils.user_resource(
'SCRIPTS',
path=os.path.join("startup", "bl_app_templates_user"),
create=True,
)

if not os.path.isdir(path_app_templates):
try:
os.makedirs(path_app_templates, exist_ok=True)
except:
traceback.print_exc()

app_templates_old = set(os.listdir(path_app_templates))

# check to see if the file is in compressed format (.zip)
if zipfile.is_zipfile(filepath):
try:
file_to_extract = zipfile.ZipFile(filepath, 'r')
except:
traceback.print_exc()
return {'CANCELLED'}

file_to_extract_root = _zipfile_root_namelist(file_to_extract)
if overwrite:
for f in file_to_extract_root:
_module_filesystem_remove(path_app_templates, f)
else:
for f in file_to_extract_root:
path_dest = os.path.join(path_app_templates, os.path.basename(f))
if os.path.exists(path_dest):
# self.report({'WARNING'}, tip_("File already installed to %r\n") % path_dest)
return {'CANCELLED'}

try: # extract the file to "bl_app_templates_user"
file_to_extract.extractall(path_app_templates)
except:
traceback.print_exc()
return {'CANCELLED'}

else:
# Only support installing zipfiles
print('no zipfile')
return {'CANCELLED'}

app_templates_new = set(os.listdir(path_app_templates)) - app_templates_old

# in case a new module path was created to install this addon.
bpy.utils.refresh_script_paths()

# print message
msg = (
tip_("Template Installed (%s) from %r into %r") %
(", ".join(sorted(app_templates_new)), filepath, path_app_templates)
)
print(msg)

0 comments on commit 66eff1a

Please sign in to comment.