BlenderKit: Updated tooltip layout and finishing profile and OAuth

This commit is contained in:
Vilem Duha 2019-05-17 21:25:33 +02:00
parent ab57ec6a76
commit b002728942
7 changed files with 155 additions and 46 deletions

View File

@ -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)

View File

@ -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):

View File

@ -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():

View File

@ -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:

View File

@ -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 = bpy.context.region
scale = bpy.context.preferences.view.ui_scale
t = time.time()
@ -438,11 +535,12 @@ def draw_callback_2d_progress(self, context):
img = utils.get_hidden_image(tpath, 'rating_preview')
loc = view3d_utils.location_3d_to_region_2d(bpy.context.region, bpy.context.space_data.region_3d,
d['location'])
if asset_data['asset_type'] == 'model':
# models now draw with star trek mode, no need to draw percent for the image.
draw_downloader(loc[0], loc[1], percent=tcom.progress, img=img)
else:
draw_downloader(loc[0], loc[1], percent=tcom.progress, img=img)
if loc is not None:
if asset_data['asset_type'] == 'model':
# models now draw with star trek mode, no need to draw percent for the image.
draw_downloader(loc[0], loc[1], percent=tcom.progress, img=img)
else:
draw_downloader(loc[0], loc[1], percent=tcom.progress, img=img)
else:
@ -469,7 +567,7 @@ def draw_callback_2d_upload_preview(self, context):
img = utils.get_hidden_image(ui_props.thumbnail_image, 'upload_preview')
draw_tooltip(ui_props.bar_x, ui_props.bar_y, ui_props.tooltip, img)
draw_tooltip(ui_props.bar_x, ui_props.bar_y, text=ui_props.tooltip, img=img)
def draw_callback_2d_search(self, context):
@ -627,7 +725,7 @@ def draw_callback_2d_search(self, context):
iname = utils.previmg_name(ui_props.active_index)
img = bpy.data.images.get(iname)
draw_tooltip(ui_props.mouse_x, ui_props.mouse_y, ui_props.tooltip, img)
draw_tooltip(ui_props.mouse_x, ui_props.mouse_y, text=ui_props.tooltip, img=img)
if ui_props.dragging and (
ui_props.draw_drag_image or ui_props.draw_snapped_bounds) and ui_props.active_index > -1:
@ -1312,13 +1410,15 @@ class AssetBarOperator(bpy.types.Operator):
else:
return {'RUNNING_MODAL'}
if event.type == 'A' and ui_props.active_index != -3:
if event.type == 'W' and ui_props.active_index != -3:
sr = bpy.context.scene['search results']
asset_data = sr[ui_props.active_index]
a = bpy.context.window_manager['bkit authors'].get(asset_data['author_id'])
if a is not None:
utils.p('author:', a)
if a.get('aboutMeUrl') is not None:
bpy.ops.wm.url_open(url=a['aboutMeUrl'])
return {'RUNNING_MODAL'}
if event.type == 'X' and ui_props.active_index != -3:
sr = bpy.context.scene['search results']
asset_data = sr[ui_props.active_index]

View File

@ -573,7 +573,7 @@ class VIEW3D_PT_blenderkit_unified(Panel):
layout.operator("wm.blenderkit_login_cancel", text="Cancel", icon='CANCEL')
return
if len(user_preferences.api_key) < 20 and user_preferences.asset_counter > 5:
if len(user_preferences.api_key) < 20 and user_preferences.asset_counter > 20:
layout.operator("wm.blenderkit_login", text="Login/ Sign up",
icon='URL')
# layout.label(text='Paste your API Key:')

View File

@ -201,24 +201,6 @@ def save_prefs(self, context):
bpy.ops.wm.save_userpref()
def load_categories():
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 get_hidden_image(tpath, bdata_name, force_reload=False):
hidden_name = '.%s' % bdata_name
img = bpy.data.images.get(hidden_name)