BlenderKit: fixes for search

queries with diacritics or too long queries are now stripped of diacritics or cropped correctly
Scroll jumps back to beginning for new searches.
This commit is contained in:
Vilém Duha 2021-04-08 13:06:06 +02:00
parent 4e1ab4a386
commit 85d7154958
2 changed files with 20 additions and 11 deletions

View File

@ -47,6 +47,7 @@ import bpy
import copy
import json
import math
import unicodedata
import logging
@ -417,7 +418,6 @@ def timer_update():
return .2
rdata = thread[0].result
result_field = []
ok, error = check_errors(rdata)
if ok:
@ -435,7 +435,8 @@ def timer_update():
load_previews()
ui_props = bpy.context.scene.blenderkitUI
if len(result_field) < ui_props.scrolloffset:
if len(result_field) < ui_props.scrolloffset or not(thread[0].params.get('get_next')):
#jump back
ui_props.scrolloffset = 0
props.is_searching = False
props.search_error = False
@ -1434,6 +1435,18 @@ def search(category='', get_next=False, author_id=''):
props = scene.blenderkit_brush
query = build_query_brush()
# crop long searches
if query.get('query'):
if len(query['query']) > 50:
print('strip it now strip it good')
print(query['query'])
query['query'] = strip_accents(query['query'])
print(query['query'])
if len(query['query']) > 150:
idx = query['query'].find(' ', 142)
query['query'] = query['query'][:idx]
# it's possible get_net was requested more than once.
if props.is_searching and get_next == True:
return;
@ -1464,8 +1477,6 @@ def search(category='', get_next=False, author_id=''):
'free_first': props.free_only
}
# if free_only:
# query['keywords'] += '+is_free:true'
orig_results = bpy.context.window_manager.get(f'bkit {ui_props.asset_type.lower()} search orig', {})
if orig_results != {}:
# ensure it's a copy in dict for what we are passing to thread:
@ -1516,6 +1527,10 @@ def search_update(self, context):
search()
# accented_string is of type 'unicode'
def strip_accents(s):
return ''.join(c for c in unicodedata.normalize('NFD', s)
if unicodedata.category(c) != 'Mn')
class SearchOperator(Operator):
"""Tooltip"""
@ -1565,10 +1580,7 @@ class SearchOperator(Operator):
sprops.search_keywords = ''
if self.keywords != '':
sprops.search_keywords = self.keywords
#crop long searches
if len(self.keywords) > 150:
idx = self.keywords.find(' ', 142)
self.keywords = self.keywords[:idx]
search(category=self.category, get_next=self.get_next, author_id=self.author_id)
# bpy.ops.view3d.blenderkit_asset_bar()

View File

@ -1580,9 +1580,6 @@ def draw_panel_categories(self, context):
layout = self.layout
# row = layout.row()
# row.prop(ui_props, 'asset_type', expand=True, icon_only=True)
layout.separator()
layout.label(text='Categories')
wm = bpy.context.window_manager
if wm.get('bkit_categories') == None:
return