[Bf-extensions-cvs] [501f660f] master: BlenderKit: save user preferences if user pastes the API key into the panel in 3d view. some users were confused by this, because they didn't save their preferences. Also some users filled in the API key area some other string and then didn't know what they did wrong.

Vilem Duha noreply at git.blender.org
Sun Apr 21 21:21:50 CEST 2019


Commit: 501f660fe857dd292a18248036124c5339019ed3
Author: Vilem Duha
Date:   Thu Apr 11 15:14:45 2019 +0200
Branches: master
https://developer.blender.org/rBA501f660fe857dd292a18248036124c5339019ed3

BlenderKit: save user preferences if user pastes the API key into the panel in 3d view.
some users were confused by this, because they didn't save their preferences.
Also some users filled in the API key area some other string and then didn't know what they did wrong.

===================================================================

M	blenderkit/__init__.py
M	blenderkit/download.py
M	blenderkit/search.py
M	blenderkit/ui_panels.py
M	blenderkit/utils.py

===================================================================

diff --git a/blenderkit/__init__.py b/blenderkit/__init__.py
index ea76bc13..22b05259 100644
--- a/blenderkit/__init__.py
+++ b/blenderkit/__init__.py
@@ -1280,6 +1280,13 @@ class BlenderKitAddonPreferences(AddonPreferences):
                                    min=0,
                                    max=20)
 
+    asset_counter:  IntProperty(name="Usage Counter",
+                                   description="Counts usages so it asks for registration only after reaching a limit",
+                                   default=0,
+                                   min=0,
+                                   max=20000)
+
+
     # allow_proximity : BoolProperty(
     #     name="allow proximity data reports",
     #     description="This sends anonymized proximity data \n \
diff --git a/blenderkit/download.py b/blenderkit/download.py
index 7fada2b9..3f340981 100644
--- a/blenderkit/download.py
+++ b/blenderkit/download.py
@@ -285,6 +285,11 @@ def append_asset(asset_data, **kwargs):  # downloaders=[], location=None,
     id = asset_data['asset_base_id']
     scene['assets rated'][id] = scene['assets rated'].get(id, False)
 
+    user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
+
+    if user_preferences.api_key == '':
+        user_preferences.asset_counter+=1
+
     if asset_data['asset_type'] == 'scene':
         scene = append_link.append_scene(file_names[0], link=False, fake_user=False)
         props = scene.blenderkit
diff --git a/blenderkit/search.py b/blenderkit/search.py
index 040ee7ea..0c674dcf 100644
--- a/blenderkit/search.py
+++ b/blenderkit/search.py
@@ -57,6 +57,9 @@ prev_time = 0
 def check_errors(rdata):
     if rdata.get('statusCode') == 401:
         if rdata.get('detail') == 'Invalid token.':
+            # reset the api key, so it can be requested again.
+            user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
+            user_preferences.api_key = ''
             return False, 'Missing or wrong api_key in addon preferences'
     return True, ''
 
diff --git a/blenderkit/ui_panels.py b/blenderkit/ui_panels.py
index 724a51a4..4aa4e25a 100644
--- a/blenderkit/ui_panels.py
+++ b/blenderkit/ui_panels.py
@@ -524,7 +524,7 @@ class VIEW3D_PT_blenderkit_unified(Panel):
 
         w = context.region.width
 
-        if user_preferences.api_key == '':
+        if len(user_preferences.api_key) < 35 and user_preferences.counter >10:
             op = layout.operator("wm.url_open", text="Register online",
                                  icon='QUESTION')
             op.url = paths.BLENDERKIT_SIGNUP_URL
diff --git a/blenderkit/utils.py b/blenderkit/utils.py
index 39c55f33..20e47db9 100644
--- a/blenderkit/utils.py
+++ b/blenderkit/utils.py
@@ -174,20 +174,27 @@ def load_prefs():
             user_preferences.api_key = prefs['API_key']
             user_preferences.global_dir = prefs['global_dir']
 
-
 def save_prefs(self, context):
     user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
     if user_preferences.api_key != '':
-        prefs = {
-            'API_key': user_preferences.api_key,
-            'global_dir': user_preferences.global_dir,
-        }
-        # user_preferences.api_key = user_preferences.api_key.strip()
-        fpath = paths.BLENDERKIT_SETTINGS_FILENAME
-        f = open(fpath, 'w')
-        with open(fpath, 'w') as s:
-            json.dump(prefs, s)
-
+        print(len(user_preferences.api_key))
+        print('length')
+        if len(user_preferences.api_key)>35:
+            prefs = {
+                'API_key': user_preferences.api_key,
+                'global_dir': user_preferences.global_dir,
+            }
+            # user_preferences.api_key = user_preferences.api_key.strip()
+            fpath = paths.BLENDERKIT_SETTINGS_FILENAME
+            f = open(fpath, 'w')
+            with open(fpath, 'w') as s:
+                json.dump(prefs, s)
+            bpy.ops.wm.save_userpref()
+        else:
+            # reset the api key in case the user writes some nonsense, e.g. a search string instead of the Key
+            user_preferences.api_key = ''
+            props = get_search_props()
+            props.report = 'Please paste a correct API Key.'
 
 def load_categories():
     categories.copy_categories()



More information about the Bf-extensions-cvs mailing list