BlenderKit: fix login after token refresh fails.

Now offers a popup to login on site, previously only reported about invalid token, which wasn't clear to many users.

(cherry picked from commit c52cfd99ff)
This commit is contained in:
Vilém Duha 2020-07-15 00:39:45 +02:00
parent c6a7bba3f0
commit a3a1815d36
6 changed files with 54 additions and 39 deletions

View File

@ -116,7 +116,7 @@ class RegisterLoginOnline(bpy.types.Operator):
message: bpy.props.StringProperty(
name="Message",
description="",
default="You were logged out from BlenderKit. Clicking OK takes you to web login. ")
default="You were logged out from BlenderKit.\n Clicking OK takes you to web login. ")
@classmethod
def poll(cls, context):
@ -124,7 +124,7 @@ class RegisterLoginOnline(bpy.types.Operator):
def draw(self, context):
layout = self.layout
utils.label_multiline(layout, text=self.message)
utils.label_multiline(layout, text=self.message, width = 300)
def execute(self, context):
preferences = bpy.context.preferences.addons['blenderkit'].preferences

View File

@ -76,6 +76,11 @@ def rerequest(method, url, **kwargs):
utils.p('reresult', response.status_code)
if response.status_code >= 400:
utils.p('reresult', response.text)
else:
tasks_queue.add_task((ui.add_report, (
'Refreshing token failed.Please login manually.', 10)))
# tasks_queue.add_task((bkit_oauth.write_tokens, ('', '', '')))
tasks_queue.add_task((bpy.ops.wm.blenderkit_login,( 'INVOKE_DEFAULT',)),fake_context = True)
return response

View File

@ -72,7 +72,7 @@ def check_errors(rdata):
if user_preferences.enable_oauth:
bkit_oauth.refresh_token_thread()
return False, rdata.get('detail')
return False, 'Missing or wrong api_key in addon preferences'
return False, 'Use login panel to connect your profile.'
return True, ''
@ -282,7 +282,7 @@ def timer_update():
search()
preferences.first_run = False
if preferences.tips_on_start:
ui.get_largest_3dview()
utils.get_largest_3dview()
ui.update_ui_size(ui.active_area, ui.active_region)
ui.add_report(text='BlenderKit Tip: ' + random.choice(rtips), timeout=12, color=colors.GREEN)
return 3.0

View File

@ -45,15 +45,16 @@ def get_queue():
return t.task_queue
class task_object:
def __init__(self, command = '', arguments = (), wait = 0, only_last = False):
def __init__(self, command = '', arguments = (), wait = 0, only_last = False, fake_context = False):
self.command = command
self.arguments = arguments
self.wait = wait
self.only_last = only_last
self.fake_context = fake_context
def add_task(task, wait = 0, only_last = False):
def add_task(task, wait = 0, only_last = False, fake_context = False):
q = get_queue()
taskob = task_object(task[0],task[1], wait = wait, only_last = only_last)
taskob = task_object(task[0],task[1], wait = wait, only_last = only_last, fake_context = fake_context)
q.put(taskob)
@ -90,7 +91,11 @@ def queue_worker():
utils.p('as a task: ')
utils.p(task.command, task.arguments)
try:
task.command(*task.arguments)
if task.fake_context:
fc = utils.get_fake_context(bpy.context)
task.command(fc,*task.arguments)
else:
task.command(*task.arguments)
except Exception as e:
utils.p('task failed:')
print(e)

View File

@ -1181,30 +1181,6 @@ def update_ui_size(area, region):
ui.rating_y = ui.bar_y - ui.bar_height
def get_largest_3dview():
maxsurf = 0
maxa = None
maxw = None
region = None
for w in bpy.context.window_manager.windows:
screen = w.screen
for a in screen.areas:
if a.type == 'VIEW_3D':
asurf = a.width * a.height
if asurf > maxsurf:
maxa = a
maxw = w
maxsurf = asurf
for r in a.regions:
if r.type == 'WINDOW':
region = r
global active_area, active_window, active_region
active_window = maxw
active_area = maxa
active_region = region
return maxw, maxa, region
class AssetBarOperator(bpy.types.Operator):
'''runs search and displays the asset bar at the same time'''
@ -1808,13 +1784,14 @@ class UndoWithContext(bpy.types.Operator):
C_dict = bpy.context.copy()
C_dict.update(region='WINDOW')
if context.area is None or context.area.type != 'VIEW_3D':
w, a, r = get_largest_3dview()
w, a, r = utils.get_largest_3dview()
override = {'window': w, 'screen': w.screen, 'area': a, 'region': r}
C_dict.update(override)
bpy.ops.ed.undo_push(C_dict, 'INVOKE_REGION_WIN', message=self.message)
return {'FINISHED'}
class RunAssetBarWithContext(bpy.types.Operator):
"""Regenerate cobweb"""
bl_idname = "object.run_assetbar_fix_context"
@ -1826,12 +1803,7 @@ class RunAssetBarWithContext(bpy.types.Operator):
# return {'RUNNING_MODAL'}
def execute(self, context):
C_dict = bpy.context.copy()
C_dict.update(region='WINDOW')
if context.area is None or context.area.type != 'VIEW_3D':
w, a, r = get_largest_3dview()
override = {'window': w, 'screen': w.screen, 'area': a, 'region': r}
C_dict.update(override)
C_dict = utils.get_fake_context(context)
bpy.ops.view3d.blenderkit_asset_bar(C_dict, 'INVOKE_REGION_WIN', keep_running=True, do_search=False)
return {'FINISHED'}

View File

@ -614,6 +614,39 @@ def guard_from_crash():
return True
def get_largest_3dview():
maxsurf = 0
maxa = None
maxw = None
region = None
for w in bpy.context.window_manager.windows:
screen = w.screen
for a in screen.areas:
if a.type == 'VIEW_3D':
asurf = a.width * a.height
if asurf > maxsurf:
maxa = a
maxw = w
maxsurf = asurf
for r in a.regions:
if r.type == 'WINDOW':
region = r
global active_area, active_window, active_region
active_window = maxw
active_area = maxa
active_region = region
return maxw, maxa, region
def get_fake_context(context):
C_dict = context.copy()
C_dict.update(region='WINDOW')
if context.area is None or context.area.type != 'VIEW_3D':
w, a, r = get_largest_3dview()
override = {'window': w, 'screen': w.screen, 'area': a, 'region': r}
C_dict.update(override)
return C_dict
def label_multiline(layout, text='', icon='NONE', width=-1):
''' draw a ui label, but try to split it in multiple lines.'''
if text.strip() == '':