Skip to content
Snippets Groups Projects
search.py 40.3 KiB
Newer Older
  • Learn to ignore specific revisions
  • Vilem Duha's avatar
    Vilem Duha committed
        prev_time = time.time()
    
    Vilem Duha's avatar
    Vilem Duha committed
        utils.p(text, alltime, since_last)
    
    Vilem Duha's avatar
    Vilem Duha committed
    
    
    def add_search_process(query, params):
        global search_threads
    
        while (len(search_threads) > 0):
            old_thread = search_threads.pop(0)
            old_thread[0].stop()
            # TODO CARE HERE FOR ALSO KILLING THE THREADS...AT LEAST NOW SEARCH DONE FIRST WON'T REWRITE AN OLDER ONE
    
        tempdir = paths.get_temp_dir('%s_search' % query['asset_type'])
        thread = Searcher(query, params)
        thread.start()
    
        search_threads.append([thread, tempdir, query['asset_type']])
    
        mt('thread started')
    
    
    
    def search(category='', get_next=False, author_id=''):
    
    Vilem Duha's avatar
    Vilem Duha committed
        ''' initialize searching'''
        global search_start_time
    
        user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
    
    
    Vilem Duha's avatar
    Vilem Duha committed
        search_start_time = time.time()
        mt('start')
        scene = bpy.context.scene
        uiprops = scene.blenderkitUI
    
        if uiprops.asset_type == 'MODEL':
            if not hasattr(scene, 'blenderkit'):
                return;
            props = scene.blenderkit_models
            query = build_query_model()
    
        if uiprops.asset_type == 'SCENE':
            if not hasattr(scene, 'blenderkit_scene'):
                return;
            props = scene.blenderkit_scene
            query = build_query_scene()
    
        if uiprops.asset_type == 'MATERIAL':
            if not hasattr(scene, 'blenderkit_mat'):
                return;
            props = scene.blenderkit_mat
            query = build_query_material()
    
        if uiprops.asset_type == 'TEXTURE':
            if not hasattr(scene, 'blenderkit_tex'):
                return;
            # props = scene.blenderkit_tex
            # query = build_query_texture()
    
        if uiprops.asset_type == 'BRUSH':
            if not hasattr(scene, 'blenderkit_brush'):
                return;
            props = scene.blenderkit_brush
            query = build_query_brush()
    
        if props.is_searching and get_next == True:
            return;
    
    Vilem Duha's avatar
    Vilem Duha committed
        if category != '':
    
            query['category_subtree'] = category
    
        if author_id != '':
    
            query['author_id'] = author_id
    
    
    Vilem Duha's avatar
    Vilem Duha committed
        # utils.p('searching')
    
    Vilem Duha's avatar
    Vilem Duha committed
        props.is_searching = True
    
        params = {
            'scene_uuid': bpy.context.scene.get('uuid', None),
            'addon_version': version_checker.get_addon_version(),
    
            'api_key': user_preferences.api_key,
    
    Vilem Duha's avatar
    Vilem Duha committed
            'get_next': get_next
        }
    
    
        # if free_only:
        #     query['keywords'] += '+is_free:true'
    
    Vilem Duha's avatar
    Vilem Duha committed
    
        add_search_process(query, params)
    
        tasks_queue.add_task((ui.add_report, ('BlenderKit searching....', 2)))
    
    Vilem Duha's avatar
    Vilem Duha committed
        props.report = 'BlenderKit searching....'
    
    
    def search_update(self, context):
    
    Vilem Duha's avatar
    Vilem Duha committed
        utils.p('search updater')
    
    Vilem Duha's avatar
    Vilem Duha committed
        if self.search_keywords != '':
            search()
    
    
    class SearchOperator(Operator):
        """Tooltip"""
        bl_idname = "view3d.blenderkit_search"
        bl_label = "BlenderKit asset search"
    
        own: BoolProperty(name="own assets only",
                          description="Find all own assets",
                          default=False)
        category: StringProperty(
            name="category",
            description="search only subtree of this category",
            default="")
    
    
        author_id: StringProperty(
            name="Author ID",
            description="Author ID - search only assets by this author",
            default="")
    
    
    Vilem Duha's avatar
    Vilem Duha committed
        get_next: BoolProperty(name="next page",
                               description="get next page from previous search",
                               default=False)
    
        @classmethod
        def poll(cls, context):
            return True
    
        def execute(self, context):
    
            search(own=self.own, category=self.category, get_next=self.get_next, author_id=self.author_id)
    
    Vilem Duha's avatar
    Vilem Duha committed
            bpy.ops.view3d.blenderkit_asset_bar()
    
            return {'FINISHED'}
    
    
    classes = [
        SearchOperator
    ]
    
    
    def register_search():
        bpy.app.handlers.load_post.append(scene_load)
    
        for c in classes:
            bpy.utils.register_class(c)
    
        # bpy.app.timers.register(timer_update, persistent = True)
    
    
    Vilem Duha's avatar
    Vilem Duha committed
    
    
    def unregister_search():
        bpy.app.handlers.load_post.remove(scene_load)
    
        for c in classes:
            bpy.utils.unregister_class(c)
    
        # bpy.app.timers.unregister(timer_update)
    
    
    '''
    
    Vilem Duha's avatar
    Vilem Duha committed
    build query
    START THREAD
    
    send query (bg already)
    get result - metadata, small thumbnails, big thumbnails paths (now generate this?)
    write metadata, possibly to
    
    Vilem Duha's avatar
    Vilem Duha committed
    download small thumbnails first
    start big thumbnails download. these don't have to be there on updates, if they aren't the Image in image editor doesn't get updated.
    parse metadata, save it in json in the temp dir which gets read on each update of the search.
    END THREAD
    when download is triggered, get also this metadata from json. E
    pass this single - item metadata in the download functions, threads.
    '''