[Bf-extensions-cvs] [b0027289] master: BlenderKit: Updated tooltip layout and finishing profile and OAuth

Vilem Duha noreply at git.blender.org
Sun May 19 16:02:55 CEST 2019


Commit: b0027289420fd8eaf7c2686e6a64ffe8b2e4419e
Author: Vilem Duha
Date:   Fri May 17 21:25:33 2019 +0200
Branches: master
https://developer.blender.org/rBAb0027289420fd8eaf7c2686e6a64ffe8b2e4419e

BlenderKit: Updated tooltip layout and finishing profile and OAuth

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

M	blenderkit/categories.py
M	blenderkit/oauth.py
M	blenderkit/search.py
M	blenderkit/tasks_queue.py
M	blenderkit/ui.py
M	blenderkit/ui_panels.py
M	blenderkit/utils.py

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

diff --git a/blenderkit/categories.py b/blenderkit/categories.py
index 794b15b1..c2b37dd1 100644
--- a/blenderkit/categories.py
+++ b/blenderkit/categories.py
@@ -1,7 +1,8 @@
 import requests
 import json
 import os
-from blenderkit import paths,utils
+import bpy
+from blenderkit import paths, utils, tasks_queue
 import shutil
 import threading
 
@@ -18,6 +19,7 @@ def fix_category_counts(categories):
 
 
 def filter_category(category):
+    ''' filter categories with no assets, so they aren't shown in search panel'''
     if category['assetCount'] < 1:
         return True
     else:
@@ -57,6 +59,23 @@ def copy_categories():
             print("couldn't copy categories file")
 
 
+def load_categories():
+    copy_categories()
+    tempdir = paths.get_temp_dir()
+    categories_filepath = os.path.join(tempdir, 'categories.json')
+
+    wm = bpy.context.window_manager
+    with open(categories_filepath, 'r') as catfile:
+        wm['bkit_categories'] = json.load(catfile)
+
+    wm['active_category'] = {
+        'MODEL': ['model'],
+        'SCENE': ['scene'],
+        'MATERIAL': ['material'],
+        'BRUSH': ['brush'],
+    }
+
+
 def fetch_categories(API_key):
     url = paths.get_api_url() + 'categories/'
 
@@ -73,8 +92,10 @@ def fetch_categories(API_key):
         # filter_categories(categories) #TODO this should filter categories for search, but not for upload. by now off.
         with open(categories_filepath, 'w') as s:
             json.dump(categories, s, indent=4)
-    except:
-        # print('category fetching failed')
+        tasks_queue.add_task((load_categories, ()))
+    except Exception as e:
+        utils.p('category fetching failed')
+        utils.p(e)
         if not os.path.exists(categories_filepath):
             source_path = paths.get_addon_file(subpath='data' + os.sep + 'categories.json')
             shutil.copy(source_path, categories_filepath)
diff --git a/blenderkit/oauth.py b/blenderkit/oauth.py
index 2e0f76eb..c5d1932a 100644
--- a/blenderkit/oauth.py
+++ b/blenderkit/oauth.py
@@ -26,7 +26,7 @@ from urllib.parse import parse_qs, urlparse
 import requests
 import threading
 import blenderkit
-from blenderkit import tasks_queue, utils, paths, search
+from blenderkit import tasks_queue, utils, paths, search, categories
 
 CLIENT_ID = "IdFRwa3SGA8eMpzhRVFMg5Ts8sPK93xBjif93x0F"
 PORTS = [62485, 1234]
@@ -127,6 +127,7 @@ def write_tokens(auth_token, refresh_token):
     props = utils.get_search_props()
     props.report = 'Login success!'
     search.get_profile()
+    categories.fetch_categories_thread(auth_token)
 
 
 class RegisterLoginOnline(bpy.types.Operator):
diff --git a/blenderkit/search.py b/blenderkit/search.py
index b0ea46f9..44bc1005 100644
--- a/blenderkit/search.py
+++ b/blenderkit/search.py
@@ -62,9 +62,7 @@ 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 = ''
+            user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
             if user_preferences.api_key != '':
                 oauth.refresh_token_thread()
                 return False, "You've been logged out. Logging in...."
@@ -77,6 +75,12 @@ thumb_sml_download_threads = {}
 thumb_full_download_threads = {}
 reports = ''
 
+def refresh_token_timer():
+    ''' this timer gets run every 20 hours. It refreshes tokens and categories.'''
+    print( 'refresh timer')
+    fetch_server_data()
+    categories.load_categories()
+    return 72000
 
 @persistent
 def scene_load(context):
@@ -84,7 +88,9 @@ def scene_load(context):
     fetch_server_data()
     # following doesn't necessarilly happen if version isn't checked yet or similar, first run.
     # wm['bkit_update'] = version_checker.compare_versions(blenderkit)
-    utils.load_categories()
+    categories.load_categories()
+    if not bpy.app.timers.is_registered(refresh_token_timer):
+        bpy.app.timers.register(refresh_token_timer, persistent=True, first_interval = 72000)
 
 
 def fetch_server_data():
@@ -471,7 +477,7 @@ def generate_author_textblock(adata):
     t = ''
     if adata not in (None, ''):
         t += 'author: %s %s\n' % (adata['firstName'], adata['lastName'])
-        t = writeblockm(t, adata, key='aboutMe', pretext='')
+        t = writeblockm(t, adata, key='aboutMe', pretext='about me')
         t += '\n'
         t = writeblockm(t, adata, key='aboutMeUrl', pretext='')
     return t
@@ -524,10 +530,10 @@ class ThumbDownloader(threading.Thread):
 
 
 def write_author(a_id, adata):
-    utils.p('writing author back')
+    # utils.p('writing author back')
     authors = bpy.context.window_manager['bkit authors']
     if authors.get(a_id) in (None, ''):
-        adata['tooltip'] = generate_author_textblock
+        adata['tooltip'] = generate_author_textblock(adata)
         authors[a_id] = adata
 
 
@@ -563,13 +569,12 @@ def get_author(r):
 
 def write_profile(adata):
     utils.p('writing profile')
-    utils.p(adata.keys())
-    print(adata)
     user = adata['user']
     # we have to convert to MB here, numbers too big for python int type
-    user['sumAssetFilesSize'] /= (1024 * 1024)
-    user['sumPrivateAssetFilesSize'] /= (1024 * 1024)
-    user['remainingPrivateQuota'] /= (1024 * 1024)
+    if user.get('sumAssetFileSize') is not None:
+        user['sumAssetFilesSize'] /= (1024 * 1024)
+        user['sumPrivateAssetFilesSize'] /= (1024 * 1024)
+        user['remainingPrivateQuota'] /= (1024 * 1024)
 
     bpy.context.window_manager['bkit profile'] = adata
 
@@ -1089,7 +1094,7 @@ def register_search():
 
     # bpy.app.timers.register(timer_update, persistent = True)
 
-    utils.load_categories()
+    categories.load_categories()
 
 
 def unregister_search():
diff --git a/blenderkit/tasks_queue.py b/blenderkit/tasks_queue.py
index b86b29da..d347ed46 100644
--- a/blenderkit/tasks_queue.py
+++ b/blenderkit/tasks_queue.py
@@ -30,7 +30,7 @@ def queue_worker():
     q = get_queue()
     while not q.empty():
         utils.p('as a task:   ')
-        print('window manager', bpy.context.window_manager)
+        # print('window manager', bpy.context.window_manager)
         task = q.get()
         utils.p(task)
         try:
diff --git a/blenderkit/ui.py b/blenderkit/ui.py
index e26b24ab..8a3a6aa4 100644
--- a/blenderkit/ui.py
+++ b/blenderkit/ui.py
@@ -285,7 +285,104 @@ def draw_ratings_bgl():
                          font_size)
 
 
-def draw_tooltip(x, y, text, img):
+def draw_tooltip(x, y, text='', author='', img=None):
+    region = bpy.context.region
+    scale = bpy.context.preferences.view.ui_scale
+    t = time.time()
+
+    ttipmargin = 10
+
+    font_height = int(12 * scale)
+    line_height = int(15 * scale)
+    nameline_height = int(23 * scale)
+
+    lines = text.split('\n')
+    ncolumns = 2
+    nlines = math.ceil((len(lines) - 1) / ncolumns)
+    texth = line_height * nlines + nameline_height
+
+    isizex = int(512 * scale * img.size[0] / max(img.size[0], img.size[1]))
+    isizey = int(512 * scale * img.size[1] / max(img.size[0], img.size[1]))
+
+    estimated_height = 3 * ttipmargin + isizey
+
+    if estimated_height > y:
+        scaledown = y / (estimated_height)
+        scale *= scaledown
+        # we need to scale these down to have correct size if the tooltip wouldn't fit.
+        font_height = int(12 * scale)
+        line_height = int(15 * scale)
+        nameline_height = int(23 * scale)
+
+        lines = text.split('\n')
+        ncolumns = 2
+        nlines = math.ceil((len(lines) - 1) / ncolumns)
+        texth = line_height * nlines + nameline_height
+
+        isizex = int(512 * scale * img.size[0] / max(img.size[0], img.size[1]))
+        isizey = int(512 * scale * img.size[1] / max(img.size[0], img.size[1]))
+
+    name_height = int(18 * scale)
+
+    x += 2 * ttipmargin
+    y -= 2 * ttipmargin
+
+    width = isizex + 2 * ttipmargin
+
+    properties_width = 0
+    for r in bpy.context.area.regions:
+        if r.type == 'UI':
+            properties_width = r.width
+
+    x = min(x + width, region.width - properties_width) - width
+
+    bgcol = bpy.context.preferences.themes[0].user_interface.wcol_tooltip.inner
+    bgcol1 = (bgcol[0], bgcol[1], bgcol[2], .6)
+    textcol = bpy.context.preferences.themes[0].user_interface.wcol_tooltip.text
+    textcol = (textcol[0], textcol[1], textcol[2], 1)
+    textcol_mild = (textcol[0] * .8, textcol[1] * .8, textcol[2] * .8, 1)
+    textcol_strong = (textcol[0] * 1.3, textcol[1] * 1.3, textcol[2] * 1.3, 1)
+    white = (1, 1, 1, .1)
+
+    ui_bgl.draw_rect(x - ttipmargin,
+                     y - 2 * ttipmargin - isizey,
+                     isizex + ttipmargin * 2,
+                     2 * ttipmargin + isizey,
+                     bgcol)
+    ui_bgl.draw_image(x, y - isizey - ttipmargin, isizex, isizey, img, 1)
+
+    ui_bgl.draw_rect(x - ttipmargin,
+                     y - 2 * ttipmargin - isizey,
+                     isizex + ttipmargin * 2,
+                     2 * ttipmargin + texth,
+                     bgcol1)
+    i = 0
+    column_break = -1  # start minus one for the name
+    xtext = x + ttipmargin
+    fsize = name_height
+    tcol = textcol
+    for l in lines:
+        if column_break >= nlines:
+            xtext += int(isizex / ncolumns)
+            column_break = 0
+        ytext = y - column_break * line_height - nameline_height - ttipmargin * 2 - isizey + texth
+        if i == 0:
+            ytext = y - name_height + 5 - isizey + texth - ttipmargin
+        elif i == len(lines) - 1:
+            ytext = y - (nlines - 1) * line_height - nameline_height - ttipmargin * 2 - isizey + texth
+            tcol = textcol
+            tsize = font_height
+        else:
+            if l[:4] == 'Tip:':
+                tcol = textcol_strong
+            fsize = font_height
+        i += 1
+        column_break += 1
+        ui_bgl.draw_text(l, xtext, ytext, fsize, tcol)
+    t = time.time()
+
+
+def draw_tooltip_old(x, y, text='', author='', img=None):
     region = 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list