diff --git a/blenderkit/bkit_oauth.py b/blenderkit/bkit_oauth.py index 7e7946a27984ea43efe2d21485063a8d79914ac7..efcc1cc7dd1e8437b84c601b26e60913eb30f3e2 100644 --- a/blenderkit/bkit_oauth.py +++ b/blenderkit/bkit_oauth.py @@ -87,7 +87,7 @@ def write_tokens(auth_token, refresh_token, oauth_response): props.report = '' ui.add_report('BlenderKit Re-Login success') search.get_profile() - categories.fetch_categories_thread(auth_token) + categories.fetch_categories_thread(auth_token, force = False) class RegisterLoginOnline(bpy.types.Operator): diff --git a/blenderkit/categories.py b/blenderkit/categories.py index 8408f16f2d03c4b9b02965674e241b64a54d4642..2da830cb8a33e3edb6c281591e4ad0f082727c58 100644 --- a/blenderkit/categories.py +++ b/blenderkit/categories.py @@ -28,6 +28,7 @@ import time import shutil import threading import logging + bk_logger = logging.getLogger('blenderkit') @@ -95,6 +96,7 @@ def get_category(categories, cat_path=()): return (c) break; + # def get_upload_asset_type(self): # typemapper = { # bpy.types.Object.blenderkit: 'model', @@ -106,15 +108,16 @@ def get_category(categories, cat_path=()): # asset_type = typemapper[type(self)] # return asset_type -def update_category_enums(self,context): +def update_category_enums(self, context): '''Fixes if lower level is empty - sets it to None, because enum value can be higher.''' - enums = get_subcategory_enums(self,context) + enums = get_subcategory_enums(self, context) if enums[0][0] == 'NONE' and self.subcategory != 'NONE': self.subcategory = 'NONE' -def update_subcategory_enums(self,context): + +def update_subcategory_enums(self, context): '''Fixes if lower level is empty - sets it to None, because enum value can be higher.''' - enums = get_subcategory1_enums(self,context) + enums = get_subcategory1_enums(self, context) if enums[0][0] == 'NONE' and self.subcategory1 != 'NONE': self.subcategory1 = 'NONE' @@ -132,10 +135,11 @@ def get_category_enums(self, context): items.append(('NONE', '', 'no categories on this level defined')) return items + def get_subcategory_enums(self, context): wm = bpy.context.window_manager props = bpy.context.scene.blenderkitUI - asset_type = props.asset_type.lower() + asset_type = props.asset_type.lower() items = [] if self.category != '': asset_categories = get_category(wm['bkit_categories'], cat_path=(asset_type, self.category,)) @@ -146,13 +150,14 @@ def get_subcategory_enums(self, context): # print('subcategory', items) return items + def get_subcategory1_enums(self, context): wm = bpy.context.window_manager props = bpy.context.scene.blenderkitUI - asset_type = props.asset_type.lower() + asset_type = props.asset_type.lower() items = [] if self.category != '' and self.subcategory != '': - asset_categories = get_category(wm['bkit_categories'], cat_path=(asset_type, self.category, self.subcategory, )) + asset_categories = get_category(wm['bkit_categories'], cat_path=(asset_type, self.category, self.subcategory,)) if asset_categories: for c in asset_categories['children']: items.append((c['slug'], c['name'], c['description'])) @@ -160,6 +165,7 @@ def get_subcategory1_enums(self, context): items.append(('NONE', '', 'no categories on this level defined')) return items + def copy_categories(): # this creates the categories system on only tempdir = paths.get_temp_dir() @@ -193,11 +199,12 @@ def load_categories(): except: print('categories failed to read') + # catfetch_counter = 0 -def fetch_categories(API_key, force = False): +def fetch_categories(API_key, force=False): url = paths.get_api_url() + 'categories/' headers = utils.get_headers(API_key) @@ -216,14 +223,14 @@ def fetch_categories(API_key, force = False): try: # read categories only once per day maximum, or when forced to do so. if catfile_age > 86400 or force: - bk_logger.debug('requesting categories') + bk_logger.debug('requesting categories from server') r = rerequests.get(url, headers=headers) rdata = r.json() categories = rdata['results'] fix_category_counts(categories) # filter_categories(categories) #TODO this should filter categories for search, but not for upload. by now off. - with open(categories_filepath, 'w', encoding = 'utf-8') as s: - json.dump(categories, s, ensure_ascii=False, indent=4) + with open(categories_filepath, 'w', encoding='utf-8') as s: + json.dump(categories, s, ensure_ascii=False, indent=4) tasks_queue.add_task((load_categories, ())) except Exception as e: bk_logger.debug('category fetching failed') @@ -233,6 +240,6 @@ def fetch_categories(API_key, force = False): shutil.copy(source_path, categories_filepath) -def fetch_categories_thread(API_key, force = False): +def fetch_categories_thread(API_key, force=False): cat_thread = threading.Thread(target=fetch_categories, args=([API_key, force]), daemon=True) cat_thread.start() diff --git a/blenderkit/search.py b/blenderkit/search.py index e362a928df79579db5ca44730eecb7e59a67918d..d37b0045572f6d57721c5f2e3505694866cecdcd 100644 --- a/blenderkit/search.py +++ b/blenderkit/search.py @@ -153,14 +153,12 @@ def purge_search_results(): 'search results', 'search results orig', ] - print('purge search') asset_types = ['model', 'material', 'scene', 'hdr', 'brush'] for at in asset_types: sr_props.append('bkit {at} search') sr_props.append('bkit {at} search orig') for sr_prop in sr_props: if s.get(sr_prop): - print(sr_prop) del (s[sr_prop]) @@ -192,7 +190,7 @@ def fetch_server_data(): if api_key != '' and bpy.context.window_manager.get('bkit profile') == None: get_profile() if bpy.context.window_manager.get('bkit_categories') is None: - categories.fetch_categories_thread(api_key) + categories.fetch_categories_thread(api_key, force = False) first_time = True @@ -1388,6 +1386,7 @@ def search(category='', get_next=False, author_id=''): props = scene.blenderkit_brush query = build_query_brush() + # it's possible get_net was requested more than once. if props.is_searching and get_next == True: return; diff --git a/blenderkit/ui.py b/blenderkit/ui.py index 844d87c5bae7901f8b73b6c54bc975dd5aa484d0..6d797f7336d178d9377a278f8ac57780d330f504 100644 --- a/blenderkit/ui.py +++ b/blenderkit/ui.py @@ -1705,8 +1705,9 @@ class AssetBarOperator(bpy.types.Operator): def invoke(self, context, event): # FIRST START SEARCH ui_props = context.scene.blenderkitUI + sr = bpy.context.window_manager.get('search results') - if self.do_search: + if self.do_search or sr is None: # we erase search keywords for cateogry search now, since these combinations usually return nothing now. # when the db gets bigger, this can be deleted. if self.category != '': @@ -1714,6 +1715,9 @@ class AssetBarOperator(bpy.types.Operator): sprops.search_keywords = '' search.search(category=self.category) + if sr is None: + bpy.context.window_manager['search results'] = [] + if ui_props.assetbar_on: # we don't want to run the assetbar many times, that's why it has a switch on/off behaviour, # unless being called with 'keep_running' prop. @@ -1734,9 +1738,7 @@ class AssetBarOperator(bpy.types.Operator): ui_props.assetbar_on = True ui_props.turn_off = False - sr = bpy.context.window_manager.get('search results') - if sr is None: - bpy.context.window_manager['search results'] = [] + if context.area.type != 'VIEW_3D': self.report({'WARNING'}, "View3D not found, cannot run operator") diff --git a/blenderkit/utils.py b/blenderkit/utils.py index adf5bd03f3a119c55d114f5fe52850c1cf40426f..30f29ab00f8f5395d1ca8ad635f56ad64735e276 100644 --- a/blenderkit/utils.py +++ b/blenderkit/utils.py @@ -825,3 +825,6 @@ def label_multiline(layout, text='', icon='NONE', width=-1): break; layout.label(text=l, icon=icon) icon = 'NONE' + +def trace(): + traceback.print_stack() \ No newline at end of file