[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [4081] contrib/py/scripts/addons/ online_mat_lib/__init__.py: Add-on no longer attempts to access bpy. context at startup.

Peter Cassetta peter at fingertipsoft.com
Thu Dec 20 05:06:02 CET 2012


Revision: 4081
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=4081
Author:   petercassetta
Date:     2012-12-20 04:05:55 +0000 (Thu, 20 Dec 2012)
Log Message:
-----------
Add-on no longer attempts to access bpy.context at startup.

Fixed add-on so it will not try to access bpy.context when being imported, which, as Campbell stated in an email to the bf-python list, will now result in an attribute error.
As well, it re-checks where the add-on's install location whenever a material is applied, saved, etc., in case the user changes any addon paths while Blender is running.

Modified Paths:
--------------
    contrib/py/scripts/addons/online_mat_lib/__init__.py

Modified: contrib/py/scripts/addons/online_mat_lib/__init__.py
===================================================================
--- contrib/py/scripts/addons/online_mat_lib/__init__.py	2012-12-20 02:18:18 UTC (rev 4080)
+++ contrib/py/scripts/addons/online_mat_lib/__init__.py	2012-12-20 04:05:55 UTC (rev 4081)
@@ -58,19 +58,26 @@
 mat_lib_host = ""
 mat_lib_location = ""
 mat_lib_cached_files = -1
-if os.path.exists(os.path.join(bpy.context.user_preferences.filepaths.script_directory, "addons", "online_mat_lib", "material-library")):
-    mat_lib_folder = os.path.join(bpy.context.user_preferences.filepaths.script_directory, "addons", "online_mat_lib", "material-library")
-elif os.path.exists(os.path.join(bpy.utils.script_path_user(), "addons", "online_mat_lib", "material-library")):
-    mat_lib_folder = os.path.join(bpy.utils.script_path_user(), "addons", "online_mat_lib", "material-library")
-elif os.path.exists(os.path.join(bpy.utils.script_paths()[0], "addons", "online_mat_lib", "material-library")):
-    mat_lib_folder = os.path.join(bpy.utils.script_paths()[0], "addons", "online_mat_lib", "material-library")
-elif os.path.exists(os.path.join(bpy.utils.script_paths()[0], "addons_contrib", "online_mat_lib", "material-library")):
-    mat_lib_folder = os.path.join(bpy.utils.script_paths()[0], "addons_contrib", "online_mat_lib", "material-library")
-else:
-    print("ONLINE MATERIAL LIBRARY -- MAJOR PROBLEM:"\
-    "COULD NOT LOCATE ADD-ON INSTALLATION PATH.")
-    mat_lib_folder = "error"
 
+mat_lib_folder = ""
+
+def findLibrary():
+    global mat_lib_folder
+    if os.path.exists(os.path.join(str(bpy.utils.script_path_pref()), "addons", "online_mat_lib", "material-library")):
+        mat_lib_folder = os.path.join(str(bpy.utils.script_path_pref()), "addons", "online_mat_lib", "material-library")
+    elif os.path.exists(os.path.join(bpy.utils.script_path_user(), "addons", "online_mat_lib", "material-library")):
+        mat_lib_folder = os.path.join(bpy.utils.script_path_user(), "addons", "online_mat_lib", "material-library")
+    elif os.path.exists(os.path.join(bpy.utils.script_paths()[0], "addons", "online_mat_lib", "material-library")):
+        mat_lib_folder = os.path.join(bpy.utils.script_paths()[0], "addons", "online_mat_lib", "material-library")
+    elif os.path.exists(os.path.join(bpy.utils.script_paths()[0], "addons_contrib", "online_mat_lib", "material-library")):
+        mat_lib_folder = os.path.join(bpy.utils.script_paths()[0], "addons_contrib", "online_mat_lib", "material-library")
+    else:
+        print("ONLINE MATERIAL LIBRARY -- MAJOR PROBLEM:"\
+        "COULD NOT LOCATE ADD-ON INSTALLATION PATH.")
+        mat_lib_folder = "error"
+
+findLibrary()
+
 mat_lib_contents = "Please refresh."
 mat_lib_category_filenames = []
 mat_lib_category_types = []
@@ -906,6 +913,8 @@
         global mat_lib_location
         global working_mode
         
+        findLibrary()
+        
         if self.mode == "online":
             mat_lib_host = context.scene.mat_lib_library[:context.scene.mat_lib_library.index("/")]
             mat_lib_location = context.scene.mat_lib_library[(context.scene.mat_lib_library.index(mat_lib_host) + len(mat_lib_host)):]
@@ -1211,6 +1220,8 @@
         global current_material_number
                 
         global category_type
+        
+        findLibrary()
     
         i = 0
         while i < len(category_enum_items):
@@ -1328,6 +1339,8 @@
         global current_material_cached
         global current_material_previewed
         
+        findLibrary()
+        
         if current_material_number == self.material:
             if current_material_previewed:
                 current_material_previewed = True
@@ -1391,8 +1404,8 @@
     
     def execute(self, context):
         global mat_lib_cached_files
+        findLibrary()
         
-        
         if library == "bundled":
             self.report({'ERROR'}, "The bundled library is local only and contains no cached online data.")
             return {'CANCELLED'}
@@ -1466,6 +1479,8 @@
         global library
         global current_material_previewed
         
+        findLibrary()
+        
         #Check for a cached preview
         if library == "bundled":
             image_path = mat_lib_folder + os.sep + "bundled" + os.sep + "cycles" + os.sep  + category_filename + os.sep + self.filename + ".jpg"
@@ -1542,6 +1557,8 @@
         global node_message
         global current_material_cached
         
+        findLibrary()
+        
         if not bpy.context.active_object:
             self.report({'ERROR'}, "No object selected!")
         if self.open_location == "" and self.text_block == "":
@@ -1724,6 +1741,8 @@
         global current_material_cached
         global osl_scripts
         
+        findLibrary()
+        
         mat_name = ""
         material_file_contents = ""
         if not bpy.context.active_object:
@@ -1900,6 +1919,8 @@
         global material_file_contents
         global current_material_cached
         
+        findLibrary()
+        
         if working_mode == "online":
             connection = http.client.HTTPConnection(mat_lib_host)
             connection.request("GET", mat_lib_location + "cycles/" + category_filename + "/" + self.filename + ".bcm")
@@ -2039,6 +2060,8 @@
         global save_filename
         global current_material_cached
         
+        findLibrary()
+        
         if library == "composite" and os.path.exists(mat_lib_folder + os.sep + mat_lib_host + os.sep + "cycles" + os.sep + category_filename + os.sep + save_filename):
             bcm_file = open(mat_lib_folder + os.sep + mat_lib_host + os.sep + "cycles" + os.sep + category_filename + os.sep + self.filename, mode="r+b")
             response = bcm_file.read()



More information about the Bf-extensions-cvs mailing list