BlenderKit: fix category display
was actually showing slugs, now shows names with links fix long descriptions issue - now has a 'more' button to read the rest online (label_multiline now has a max_lines parameter and returns True if max lenght was reached) fix avatars to match server
This commit is contained in:
parent
c84f831539
commit
c72e662491
|
@ -91,7 +91,7 @@ def get_category_name_path(categories, category):
|
|||
category_path = []
|
||||
check_categories = categories[:]
|
||||
parents = {}
|
||||
utils.pprint(categories)
|
||||
# utils.pprint(categories)
|
||||
while len(check_categories) > 0:
|
||||
ccheck = check_categories.pop()
|
||||
# print(ccheck['name'])
|
||||
|
@ -100,16 +100,19 @@ def get_category_name_path(categories, category):
|
|||
|
||||
for ch in ccheck['children']:
|
||||
# print(ch['name'])
|
||||
parents[ch['slug']] = ccheck['slug']
|
||||
parents[ch['slug']] = ccheck
|
||||
|
||||
if ch['slug'] == category:
|
||||
category_path = [ch['slug']]
|
||||
category_path = [ch['name']]
|
||||
slug = ch['slug']
|
||||
while parents.get(slug):
|
||||
slug = parents.get(slug)
|
||||
category_path.insert(0, slug)
|
||||
parent = parents.get(slug)
|
||||
slug = parent['slug']
|
||||
|
||||
category_path.insert(0, parent['name'])
|
||||
return category_path
|
||||
check_categories.append(ch)
|
||||
return category_path
|
||||
|
||||
def get_category(categories, cat_path=()):
|
||||
for category in cat_path:
|
||||
|
|
|
@ -83,6 +83,8 @@ def get_oauth_landing_url():
|
|||
def get_author_gallery_url(author_id):
|
||||
return f'{get_bkit_url()}/asset-gallery?query=author_id:{author_id}'
|
||||
|
||||
def get_asset_gallery_url(asset_id):
|
||||
return f'{get_bkit_url()}/asset-gallery-detail/{asset_id}/'
|
||||
|
||||
def default_global_dict():
|
||||
from os.path import expanduser
|
||||
|
|
|
@ -685,7 +685,37 @@ def write_gravatar(a_id, gravatar_path):
|
|||
|
||||
|
||||
def fetch_gravatar(adata):
|
||||
'''
|
||||
Gets avatars from blenderkit server
|
||||
Parameters
|
||||
----------
|
||||
adata - author data from elastic search result
|
||||
|
||||
'''
|
||||
# utils.p('fetch gravatar')
|
||||
|
||||
#fetch new avatars if available already
|
||||
if adata.get('avatar128') is not None:
|
||||
avatar_path = paths.get_temp_dir(subdir='bkit_g/') + adata['id']+ '.jpg'
|
||||
if os.path.exists(avatar_path):
|
||||
tasks_queue.add_task((write_gravatar, (adata['id'], avatar_path)))
|
||||
return;
|
||||
|
||||
url= paths.get_bkit_url() + adata['avatar128']
|
||||
r = rerequests.get(url, stream=False)
|
||||
# print(r.body)
|
||||
if r.status_code == 200:
|
||||
# print(url)
|
||||
# print(r.headers['content-disposition'])
|
||||
with open(avatar_path, 'wb') as f:
|
||||
f.write(r.content)
|
||||
tasks_queue.add_task((write_gravatar, (adata['id'], avatar_path)))
|
||||
elif r.status_code == '404':
|
||||
adata['avatar128'] = None
|
||||
utils.p('avatar for author not available.')
|
||||
return
|
||||
|
||||
#older gravatar code
|
||||
if adata.get('gravatarHash') is not None:
|
||||
gravatar_path = paths.get_temp_dir(subdir='bkit_g/') + adata['gravatarHash'] + '.jpg'
|
||||
|
||||
|
|
|
@ -1397,8 +1397,8 @@ def numeric_to_str(s):
|
|||
return s
|
||||
|
||||
|
||||
def push_op_left(layout):
|
||||
for a in range(0, 5):
|
||||
def push_op_left(layout, strength =5):
|
||||
for a in range(0, strength):
|
||||
layout.label(text='')
|
||||
|
||||
|
||||
|
@ -1486,7 +1486,13 @@ class AssetPopupCard(bpy.types.Operator, ratings_utils.RatingsProperties):
|
|||
box.scale_y = 0.4
|
||||
box.label(text='Description')
|
||||
box.separator()
|
||||
utils.label_multiline(box, self.asset_data['description'], width=width)
|
||||
link_more = utils.label_multiline(box, self.asset_data['description'], width=width, max_lines = 10)
|
||||
if link_more:
|
||||
row = box.row()
|
||||
row.scale_y = 2
|
||||
op = row.operator('wm.blenderkit_url', text='See full description', icon='URL')
|
||||
op.url = paths.get_asset_gallery_url(self.asset_data['assetBaseId'])
|
||||
op.tooltip = 'Read full description on website'
|
||||
box.separator()
|
||||
|
||||
def draw_properties(self, layout, width=250):
|
||||
|
@ -1818,15 +1824,40 @@ class AssetPopupCard(bpy.types.Operator, ratings_utils.RatingsProperties):
|
|||
|
||||
cat_path = categories.get_category_path(bcats,
|
||||
self.asset_data['category'])[1:]
|
||||
for i,c in enumerate(cat_path):
|
||||
cat_path[i] = c.capitalize()
|
||||
cat_path = ' > '.join(cat_path)
|
||||
# box.label(text=cat_path)
|
||||
|
||||
|
||||
cat_path_names = categories.get_category_name_path(bcats,
|
||||
self.asset_data['category'])[1:]
|
||||
|
||||
aname = asset_data['displayName']
|
||||
aname = aname[0].upper() + aname[1:]
|
||||
|
||||
top_drag_bar.label(text=f'{cat_path} > {aname}')
|
||||
if 1:
|
||||
name_row = top_drag_bar.row()
|
||||
# name_row = name_row.split(factor=0.5)
|
||||
# name_row = name_row.column()
|
||||
# name_row = name_row.row()
|
||||
for i, c in enumerate(cat_path):
|
||||
cat_name = cat_path_names[i]
|
||||
op = name_row.operator('view3d.blenderkit_asset_bar', text=cat_name + ' >', emboss=True)
|
||||
op.do_search = True
|
||||
op.keep_running = True
|
||||
op.tooltip = f"Browse {cat_name} category"
|
||||
op.category = c
|
||||
# name_row.label(text='>')
|
||||
|
||||
name_row.label(text=aname)
|
||||
push_op_left(name_row, strength = 3)
|
||||
# for i,c in enumerate(cat_path_names):
|
||||
# cat_path_names[i] = c.capitalize()
|
||||
# cat_path_names_string = ' > '.join(cat_path_names)
|
||||
# # box.label(text=cat_path)
|
||||
#
|
||||
#
|
||||
#
|
||||
#
|
||||
# # name_row.label(text=' ')
|
||||
# top_drag_bar.label(text=f'{cat_path_names_string} > {aname}')
|
||||
|
||||
# left side
|
||||
row = layout.row(align=True)
|
||||
|
|
|
@ -860,8 +860,22 @@ def get_fake_context(context, area_type='VIEW_3D'):
|
|||
# def is_url(text):
|
||||
|
||||
|
||||
def label_multiline(layout, text='', icon='NONE', width=-1):
|
||||
''' draw a ui label, but try to split it in multiple lines.'''
|
||||
def label_multiline(layout, text='', icon='NONE', width=-1, max_lines = 10):
|
||||
'''
|
||||
draw a ui label, but try to split it in multiple lines.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
layout
|
||||
text
|
||||
icon
|
||||
width width to split by in character count
|
||||
max_lines maximum lines to draw
|
||||
|
||||
Returns
|
||||
-------
|
||||
True if max_lines was overstepped
|
||||
'''
|
||||
if text.strip() == '':
|
||||
return
|
||||
text = text.replace('\r\n','\n')
|
||||
|
@ -870,11 +884,10 @@ def label_multiline(layout, text='', icon='NONE', width=-1):
|
|||
threshold = int(width / 5.5)
|
||||
else:
|
||||
threshold = 35
|
||||
maxlines = 8
|
||||
li = 0
|
||||
for l in lines:
|
||||
# if is_url(l):
|
||||
|
||||
li+=1
|
||||
while len(l) > threshold:
|
||||
i = l.rfind(' ', 0, threshold)
|
||||
if i < 1:
|
||||
|
@ -884,12 +897,15 @@ def label_multiline(layout, text='', icon='NONE', width=-1):
|
|||
icon = 'NONE'
|
||||
l = l[i:].lstrip()
|
||||
li += 1
|
||||
if li > maxlines:
|
||||
if li > max_lines:
|
||||
break;
|
||||
if li > maxlines:
|
||||
if li > max_lines:
|
||||
break;
|
||||
layout.label(text=l, icon=icon)
|
||||
icon = 'NONE'
|
||||
if li>max_lines:
|
||||
return True
|
||||
|
||||
|
||||
|
||||
def trace():
|
||||
|
|
Loading…
Reference in New Issue