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 problems when loading and saving config file #217

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ There are two possible solutions:


## Updating to new versions ##
If you are upgrading from a previous version of blender2ogre and having problems, you may want to delete your old .pickle config file from
[$BLENDER_DIR](https://docs.blender.org/manual/en/latest/advanced/blender_directory_layout.html)`/config/scripts/blender2ogre.pickle` and restart blender.
If you are upgrading from a previous version of blender2ogre and having problems, you may want to delete your old .json config file from
[$BLENDER_DIR](https://docs.blender.org/manual/en/latest/advanced/blender_directory_layout.html)`/config/scripts/io_ogre.json` and restart blender.

## Video Tutorials
* [General Usage](http://www.youtube.com/watch?feature=player_embedded&v=3EpwEsB0_kk)
Expand Down
29 changes: 12 additions & 17 deletions io_ogre/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,19 @@
# https://blender.stackexchange.com/questions/28504/blender-ignores-changes-to-python-scripts/28505
# When bpy is already in local, we know this is not the initial import...
if "bpy" in locals():
# ...so we need to reload our submodule(s) using importlib
import importlib
if "config" in locals():
importlib.reload(config)
if "properties" in locals():
importlib.reload(properties)
if "ui" in locals():
importlib.reload(ui)

# This is only relevant on first run, on later reloads those modules
# are already in locals() and those statements do not do anything.
from . import config
from . import properties
from . import ui
#print("Reloading modules: config, mesh_preview, properties, xml, ui, util")
importlib.reload(config)
importlib.reload(mesh_preview)
importlib.reload(properties)
importlib.reload(xml)
importlib.reload(ui)
importlib.reload(util)

import bpy
import os, sys, logging
from pprint import pprint
import bpy
from . import config, properties, ui

# Import the plugin directory and setup the plugin
class Blender2OgreAddonPreferences(bpy.types.AddonPreferences):
Expand Down Expand Up @@ -107,7 +102,7 @@ def register():
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='[%(levelname)5s] %(message)s', datefmt='%H:%M:%S')

logging.info('Starting io_ogre %s', bl_info["version"])

# The UI modules define auto_register functions that
# return classes that should be loaded by the plugin
for clazz in ui.auto_register(True):
Expand All @@ -120,10 +115,10 @@ def register():

def unregister():
logging.info('Unloading io_ogre %s', bl_info["version"])

# Save the config
config.save_config()

# Unregister classes
for clazz in ui.auto_register(False):
bpy.utils.unregister_class(clazz)
Expand Down
1 change: 0 additions & 1 deletion io_ogre/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@
from .ogre.scene import dot_scene

# import various functions that can be used to export objects

51 changes: 33 additions & 18 deletions io_ogre/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import bpy, os, sys, logging, pickle, mathutils
import bpy, os, sys, logging, mathutils, json
from pprint import pprint
from bpy.props import *

Expand Down Expand Up @@ -28,9 +28,11 @@
]

CONFIG_PATH = bpy.utils.user_resource('CONFIG', path='scripts', create=True)
CONFIG_FILENAME = 'io_ogre.pickle'
CONFIG_FILENAME = 'io_ogre.json'
CONFIG_FILEPATH = os.path.join(CONFIG_PATH, CONFIG_FILENAME)

CONFIG = {}

_CONFIG_DEFAULTS_ALL = {
# General
'SWAP_AXIS' : 'xz-y',
Expand Down Expand Up @@ -74,7 +76,7 @@
'ARRAY' : True,
'EXTREMITY_POINTS' : 0,
'GENERATE_EDGE_LISTS' : False,
'GENERATE_TANGENTS' : "0",
'GENERATE_TANGENTS' : '0',
'OPTIMISE_ANIMATIONS' : True,
'INTERFACE_TOGGLE': False,
'OPTIMISE_VERTEX_BUFFERS' : True,
Expand Down Expand Up @@ -102,7 +104,8 @@
'IMPORT_ANIMATIONS' : True,
'ROUND_FRAMES' : True,
'USE_SELECTED_SKELETON' : True,
'IMPORT_SHAPEKEYS' : True
'IMPORT_SHAPEKEYS' : True,
'IMPORT_LOGGING' : False
}

_CONFIG_TAGS_ = 'OGRETOOLS_XML_CONVERTER OGRETOOLS_MESH_UPGRADER MESH_PREVIEWER IMAGE_MAGICK_CONVERT USER_MATERIALS SHADER_PROGRAMS'.split()
Expand Down Expand Up @@ -148,15 +151,22 @@
## PUBLIC API continues

def load_config():
global CONFIG
logger.info('Loading config: %s' % CONFIG_FILEPATH)
config_dict = {}

# Check if the config file exists and load it
if os.path.isfile( CONFIG_FILEPATH ):
try:
with open( CONFIG_FILEPATH, 'rb' ) as f:
config_dict = pickle.load( f )
except:
logger.error('Can not read config from %s' % CONFIG_FILEPATH)
with open( os.path.join(CONFIG_FILEPATH), 'r' ) as f:
config_dict = json.load(f)
except EOFError:
logger.error('Config file: %s is empty' % CONFIG_FILEPATH)
except Exception as e:
logger.error('Can not read config from: %s' % CONFIG_FILEPATH)
logger.error('Exception: %s' % e)
else:
logger.error('Config file: %s does not exist' % CONFIG_FILEPATH)

# Load default values from _CONFIG_DEFAULTS_ALL if they don't exist after loading config from file
for tag in _CONFIG_DEFAULTS_ALL:
Expand Down Expand Up @@ -196,9 +206,6 @@ def load_config():

return config_dict

# Global CONFIG dictionary
CONFIG = load_config()

def get(name, default=None):
global CONFIG
if name in CONFIG:
Expand All @@ -207,26 +214,34 @@ def get(name, default=None):
logger.error("Config option %s does not exist!" % name)
return default

# Global CONFIG dictionary
CONFIG = load_config()

def update(**kwargs):
for k,v in kwargs.items():
if k not in _CONFIG_DEFAULTS_ALL:
logger.warn("Trying to set CONFIG['%s'] = %s, but it is not a known config setting" % (k,v))
CONFIG[k] = v
global CONFIG
for key,value in kwargs.items():
if key not in _CONFIG_DEFAULTS_ALL:
logger.warn("Trying to set CONFIG['%s'] = %s, but it is not a known config setting" % (key, value))
#print("update() :: key: %s, value: %s" % (key, value))
CONFIG[key] = value
save_config()

def save_config():
global CONFIG
logger.info('Saving config to: %s' % CONFIG_FILEPATH)
#for key in CONFIG: print( '%s = %s' %(key, CONFIG[key]) )
if os.path.isdir( CONFIG_PATH ):
try:
with open( CONFIG_FILEPATH, 'wb' ) as f:
pickle.dump( CONFIG, f, -1 )
except:
with open( os.path.join(CONFIG_FILEPATH), 'w' ) as f:
f.write(json.dumps(CONFIG, indent=4))
except Exception as e:
logger.error('Can not write to %s' % CONFIG_FILEPATH)
logger.error('Exception: %s' % e)
else:
logger.error('Config directory %s does not exist' % CONFIG_PATH)

def update_from_addon_preference(context):
global CONFIG
addon_preferences = context.preferences.addons["io_ogre"].preferences

for key in _CONFIG_TAGS_:
Expand Down
20 changes: 4 additions & 16 deletions io_ogre/ogre/material.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@

# When bpy is already in local, we know this is not the initial import...
if "bpy" in locals():
# ...so we need to reload our submodule(s) using importlib
import importlib
if "config" in locals():
importlib.reload(config)
if "util" in locals():
importlib.reload(util)
if "shader" in locals():
importlib.reload(shader)
if "report" in locals():
importlib.reload(report)
if "program" in locals():
importlib.reload(program)

# This is only relevant on first run, on later reloads those modules
# are already in locals() and those statements do not do anything.
import logging, os, shutil, tempfile, math
#print("Reloading modules: shader")
importlib.reload(shader)

import os, shutil, tempfile, math, logging
from .. import config
from .. import shader
from .. import util
Expand Down
16 changes: 0 additions & 16 deletions io_ogre/ogre/materialv2json.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@

# When bpy is already in local, we know this is not the initial import...
if "bpy" in locals():
# ...so we need to reload our submodule(s) using importlib
import importlib
if "config" in locals():
importlib.reload(config)
if "util" in locals():
importlib.reload(util)
if "report" in locals():
importlib.reload(report)
if "material" in locals():
importlib.reload(material)

# This is only relevant on first run, on later reloads those modules
# are already in locals() and those statements do not do anything.
import logging, os, shutil, tempfile, json
from .. import config
from .. import util
Expand Down
21 changes: 2 additions & 19 deletions io_ogre/ogre/mesh.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import os, time, sys, logging
import bmesh
import mathutils
import math

# When bpy is already in local, we know this is not the initial import...
if "bpy" in locals():
# ...so we need to reload our submodule(s) using importlib
import importlib
if "report" in locals():
importlib.reload(report)
if "material" in locals():
importlib.reload(material)
if "util" in locals():
importlib.reload(util)
if "xml" in locals():
importlib.reload(xml)
if "skeleton.Skeleton" in locals():
importlib.reload(skeleton.Skeleton)
import bmesh, mathutils, math

from ..report import Report
from ..util import *
from ..xml import *
from .. import util
from .. import util, config
from .material import *
from .skeleton import Skeleton

Expand Down
16 changes: 0 additions & 16 deletions io_ogre/ogre/node_anim.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@

# When bpy is already in local, we know this is not the initial import...
if "bpy" in locals():
# ...so we need to reload our submodule(s) using importlib
import importlib
if "config" in locals():
importlib.reload(config)
if "report" in locals():
importlib.reload(report)
if "xml" in locals():
importlib.reload(xml)
if "util" in locals():
importlib.reload(util)

# This is only relevant on first run, on later reloads those modules
# are already in locals() and those statements do not do anything.
import bpy, mathutils, logging, time
from .. import config
from ..report import Report
Expand Down
10 changes: 4 additions & 6 deletions io_ogre/ogre/ogre_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,11 @@
"""

# When bpy is already in local, we know this is not the initial import...
if "bpy" in locals():
# ...so we need to reload our submodule(s) using importlib
import importlib
if "material_parser" in locals():
importlib.reload(material_parser.MaterialParser)
#if "bpy" in locals():
# import importlib
# print("RELOADING: material_parser.MaterialParser")
# importlib.reload(material_parser)

#from Blender import *
import bpy
from xml.dom import minidom
from mathutils import Vector, Matrix
Expand Down
10 changes: 0 additions & 10 deletions io_ogre/ogre/program.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@

# When bpy is already in local, we know this is not the initial import...
if "bpy" in locals():
# ...so we need to reload our submodule(s) using importlib
import importlib
if "config" in locals():
importlib.reload(config)

# This is only relevant on first run, on later reloads those modules
# are already in locals() and those statements do not do anything.
import os, logging
from .. import config

Expand Down
42 changes: 10 additions & 32 deletions io_ogre/ogre/scene.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,17 @@

# When bpy is already in local, we know this is not the initial import...
if "bpy" in locals():
# ...so we need to reload our submodule(s) using importlib
import importlib
if "material" in locals():
importlib.reload(material)
if "materialv2json" in locals():
importlib.reload(materialv2json)
if "node_anim" in locals():
importlib.reload(node_anim)
if "mesh" in locals():
importlib.reload(mesh)
if "skeleton" in locals():
importlib.reload(skeleton)
if "config" in locals():
importlib.reload(config)
if "util" in locals():
importlib.reload(util)
if "report" in locals():
importlib.reload(report)
if "xml" in locals():
importlib.reload(xml)

# This is only relevant on first run, on later reloads those modules
# are already in locals() and those statements do not do anything.
import bpy, mathutils, os, getpass, math
#print("Reloading modules: material, materialv2json, node_anim, mesh, skeleton")
importlib.reload(material)
importlib.reload(materialv2json)
importlib.reload(node_anim)
importlib.reload(mesh)
importlib.reload(skeleton)

import bpy, mathutils, os, getpass, math, logging
from os.path import join
from . import material
from . import materialv2json
from . import node_anim
from . import mesh
from . import skeleton
from .. import bl_info
from .. import config
from .. import util
from . import material, materialv2json, node_anim, mesh, skeleton
from .. import bl_info, config, util
from ..report import Report
from ..util import *
from ..xml import *
Expand Down
16 changes: 0 additions & 16 deletions io_ogre/ogre/skeleton.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@

# When bpy is already in local, we know this is not the initial import...
if "bpy" in locals():
# ...so we need to reload our submodule(s) using importlib
import importlib
if "config" in locals():
importlib.reload(config)
if "report" in locals():
importlib.reload(report)
if "xml" in locals():
importlib.reload(xml)
if "util" in locals():
importlib.reload(util)

# This is only relevant on first run, on later reloads those modules
# are already in locals() and those statements do not do anything.
import bpy, mathutils, logging, time, sys
from .. import config
from ..report import Report
Expand Down
Loading
Loading