Skip to content

Commit

Permalink
Added type annotations and use of Path
Browse files Browse the repository at this point in the history
Replaced some instances of os.path with the Path object from pathlib for
readability. Also added type annotations to MCprepEnv as we're moving
towards that. This also means MCprep will not work at all in Blender
2.7x due to the use of new syntax.

This will be the first in a long process of modernizing MCprep's code
with 2.8 style code. Blender 2.7x users may not be happy, but it's for
the better. If anything, 6 years worth of 2.7x support was a mistake (in
my opinion), as it limited what we could do and opened MCprep to even
more bugs (like in Blender 2.93 with make_annotations, which ironically
now is deprecated).
  • Loading branch information
StandingPadAnimations committed Mar 17, 2023
1 parent 643fefd commit a34b01a
Showing 1 changed file with 16 additions and 30 deletions.
46 changes: 16 additions & 30 deletions MCprep_addon/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# ##### END GPL LICENSE BLOCK #####

import os

from pathlib import Path
import bpy

# check if custom preview icons available
Expand All @@ -33,36 +33,27 @@

class MCprepEnv:
def __init__(self, dev_build=False, verbose=False):
self.dev_build = dev_build
self.verbose = verbose
self.very_verbose = dev_build
self.dev_build: bool = dev_build
self.verbose: bool = verbose
self.very_verbose: bool = dev_build

self.data = None
self.json_data = None
self.json_path = os.path.join(
os.path.dirname(__file__),
"MCprep_resources",
"mcprep_data.json")

self.json_path_update = os.path.join(
os.path.dirname(__file__),
"MCprep_resources",
"mcprep_data_update.json")
self.json_path: Path = Path(os.path.dirname(__file__), "MCprep_resources", "mcprep_data.json")
self.json_path_update: Path = Path(os.path.dirname(__file__), "MCprep_resources", "mcprep_data_update.json")

# if new update file found from install, replace old one with new
if os.path.isfile(self.json_path_update):
if os.path.isfile(self.json_path) is True:
os.remove(self.json_path)
os.rename(self.json_path_update, self.json_path)
if self.json_path_update.exists():
self.json_path_update.replace(self.json_path)

# lazy load json, ie only load it when needed (util function defined)

# -----------------------------------------------
# For preview icons
# -----------------------------------------------

self.use_icons = True
self.preview_collections = {}
self.use_icons: bool = True
self.preview_collections: dict = {}

# -----------------------------------------------
# For initializing the custom icons
Expand All @@ -76,11 +67,11 @@ def __init__(self, dev_build=False, verbose=False):
# To ensure shift-A starts drawing sub menus after pressing load all spawns
# as without this, if any one of the spawners loads nothing (invalid folder,
# no blend files etc), then it would continue to ask to reload spanwers.
self.loaded_all_spawners = False
self.loaded_all_spawners: bool = False

self.skin_list = [] # each is: [ basename, path ]
self.rig_categories = [] # simple list of directory names
self.entity_list = []
self.skin_list: list = [] # each is: [ basename, path ]
self.rig_categories: list = [] # simple list of directory names
self.entity_list: list = []

# -----------------------------------------------
# Matieral sync cahce, to avoid repeat lib reads
Expand All @@ -97,16 +88,12 @@ def __init__(self, dev_build=False, verbose=False):


def icons_init(self):
# start with custom icons
# put into a try statement in case older blender version!
global preview_collections

collection_sets = [
"main", "skins", "mobs", "entities", "blocks", "items", "effects", "materials"]

try:
for iconset in collection_sets:
preview_collections[iconset] = bpy.utils.previews.new()
self.preview_collections[iconset] = bpy.utils.previews.new()

script_path = bpy.path.abspath(os.path.dirname(__file__))
icons_dir = os.path.join(script_path, 'icons')
Expand Down Expand Up @@ -156,7 +143,6 @@ def log(self, statement, vv_only=False):

# ! Deprecated as of MCprep 3.4.2
def init():

# -----------------------------------------------
# Verbose, use as env.verbose
# Used to print out extra information, set false with distribution
Expand Down Expand Up @@ -349,7 +335,7 @@ def unregister():
try:
bpy.utils.previews.remove(pcoll)
except:
log('Issue clearing preview set ' + str(pcoll))
env.log('Issue clearing preview set ' + str(pcoll))
env.preview_collections.clear()

env.json_data = None # actively clearing out json data for next open
Expand Down

0 comments on commit a34b01a

Please sign in to comment.