[Bf-extensions-cvs] [79a0f961] master: BlenderKit: initial support for notifications

Vilem Duha noreply at git.blender.org
Fri Oct 22 08:32:44 CEST 2021


Commit: 79a0f96101e3e098f76ce22a88b320f6c831b7b5
Author: Vilem Duha
Date:   Wed Oct 20 07:53:20 2021 +0200
Branches: master
https://developer.blender.org/rBA79a0f96101e3e098f76ce22a88b320f6c831b7b5

BlenderKit: initial support for notifications

slight optimisation of the new asset bar

===================================================================

M	blenderkit/asset_bar_op.py
M	blenderkit/bkit_oauth.py
M	blenderkit/bl_ui_widgets/bl_ui_button.py
M	blenderkit/bl_ui_widgets/bl_ui_image.py
M	blenderkit/comments_utils.py
M	blenderkit/icons.py
M	blenderkit/search.py
A	blenderkit/thumbnails/bell.png
M	blenderkit/ui_panels.py

===================================================================

diff --git a/blenderkit/asset_bar_op.py b/blenderkit/asset_bar_op.py
index 5011ffa2..2a2ff4e8 100644
--- a/blenderkit/asset_bar_op.py
+++ b/blenderkit/asset_bar_op.py
@@ -15,7 +15,7 @@ import random
 import math
 
 import blenderkit
-from blenderkit import ui, paths, utils, search
+from blenderkit import ui, paths, utils, search, comments_utils
 
 from bpy.props import (
     IntProperty,
@@ -55,8 +55,7 @@ def get_area_height(self):
 
 BL_UI_Widget.get_area_height = get_area_height
 
-
-def asset_bar_modal(self, context, event):
+def modal_inside(self,context,event):
     ui_props = bpy.context.window_manager.blenderkitUI
     if ui_props.turn_off:
         ui_props.turn_off = False
@@ -110,6 +109,9 @@ def asset_bar_modal(self, context, event):
 
     return {"PASS_THROUGH"}
 
+def asset_bar_modal(self, context, event):
+    return modal_inside(self,context,event)
+
 
 def asset_bar_invoke(self, context, event):
     if not self.on_invoke(context, event):
@@ -285,6 +287,11 @@ class BlenderKitAssetBarOperator(BL_UI_OT_draw_operator):
         for w in self.tooltip_widgets:
             w.visible = True
 
+    def show_notifications(self, widget):
+        bpy.ops.wm.show_notifications()
+        if comments_utils.check_notifications_read():
+            widget.visible = False
+
     def check_new_search_results(self, context):
         sr = bpy.context.window_manager.get('search results')
         if not hasattr(self, 'search_results_count'):
@@ -404,15 +411,13 @@ class BlenderKitAssetBarOperator(BL_UI_OT_draw_operator):
 
         self.button_close.set_location(self.bar_width - self.other_button_size, 0)
         self.button_scroll_up.set_location(self.bar_width, 0)
-        self.panel.set_location(self.panel.x, self.panel.y)
         self.panel.width = self.bar_width
         self.panel.height = self.bar_height
 
+        self.panel.set_location(self.panel.x, self.panel.y)
+
         # to hide arrows accordingly
         self.scroll_update()
-        # self.init_tooltip()
-        # self.hide_tooltip()
-        # self.scroll_update()
 
     def asset_button_init(self, asset_x, asset_y, button_idx):
         ui_scale = bpy.context.preferences.view.ui_scale
@@ -460,6 +465,9 @@ class BlenderKitAssetBarOperator(BL_UI_OT_draw_operator):
         progress_bar.bg_color = (0.0, 1.0, 0.0, 0.3)
         new_button.progress_bar = progress_bar
         self.progress_bars.append(progress_bar)
+
+
+
         # if result['downloaded'] > 0:
         #     ui_bgl.draw_rect(x, y, int(ui_props.thumb_size * result['downloaded'] / 100.0), 2, green)
 
@@ -496,14 +504,17 @@ class BlenderKitAssetBarOperator(BL_UI_OT_draw_operator):
                 self.asset_buttons.append(new_button)
                 button_idx += 1
 
-        self.button_close = BL_UI_Button(self.bar_width - self.other_button_size, -0, self.other_button_size,
+        self.button_close = BL_UI_Button(self.bar_width - self.other_button_size, -self.other_button_size, self.other_button_size,
                                          self.other_button_size)
         self.button_close.bg_color = button_bg_color
         self.button_close.hover_bg_color = button_hover_color
-        self.button_close.text = "X"
+        self.button_close.text = ""
+        img_fp = paths.get_addon_thumbnail_path('vs_rejected.png')
+        self.button_close.set_image(img_fp)
         self.button_close.set_mouse_down(self.cancel_press)
 
         self.widgets_panel.append(self.button_close)
+
         scroll_width = 30
         self.button_scroll_down = BL_UI_Button(-scroll_width, 0, scroll_width, self.bar_height)
         self.button_scroll_down.bg_color = button_bg_color
@@ -529,6 +540,19 @@ class BlenderKitAssetBarOperator(BL_UI_OT_draw_operator):
 
         self.widgets_panel.append(self.button_scroll_up)
 
+        #notifications
+        if not comments_utils.check_notifications_read():
+
+            self.button_notifications = BL_UI_Button(self.bar_width - self.other_button_size, self.bar_height, self.other_button_size,
+                                             self.other_button_size)
+            self.button_notifications.bg_color = button_bg_color
+            self.button_notifications.hover_bg_color = button_hover_color
+            self.button_notifications.text = ""
+            img_fp = paths.get_addon_thumbnail_path('bell.png')
+            self.button_notifications.set_image(img_fp)
+            self.button_notifications.set_mouse_down(self.show_notifications)
+            self.widgets_panel.append(self.button_notifications)
+
         self.update_images()
 
     def position_and_hide_buttons(self):
diff --git a/blenderkit/bkit_oauth.py b/blenderkit/bkit_oauth.py
index e080845a..fa00e616 100644
--- a/blenderkit/bkit_oauth.py
+++ b/blenderkit/bkit_oauth.py
@@ -91,6 +91,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, force = False)
 
 
diff --git a/blenderkit/bl_ui_widgets/bl_ui_button.py b/blenderkit/bl_ui_widgets/bl_ui_button.py
index 3f7fe082..d8772e68 100644
--- a/blenderkit/bl_ui_widgets/bl_ui_button.py
+++ b/blenderkit/bl_ui_widgets/bl_ui_button.py
@@ -68,8 +68,9 @@ class BL_UI_Button(BL_UI_Widget):
 
     def set_image(self, rel_filepath):
         try:
-            self.__image = bpy.data.images.load(rel_filepath, check_existing=True)
-            self.__image.gl_load()
+            if self.__image is None or self.__image.filepath != rel_filepath:
+                self.__image = bpy.data.images.load(rel_filepath, check_existing=True)
+                self.__image.gl_load()
         except:
             pass
 
diff --git a/blenderkit/bl_ui_widgets/bl_ui_image.py b/blenderkit/bl_ui_widgets/bl_ui_image.py
index 7d69ad13..a11c52c5 100644
--- a/blenderkit/bl_ui_widgets/bl_ui_image.py
+++ b/blenderkit/bl_ui_widgets/bl_ui_image.py
@@ -21,8 +21,9 @@ class BL_UI_Image(BL_UI_Widget):
 
     def set_image(self, rel_filepath):
         try:
-            self.__image = bpy.data.images.load(rel_filepath, check_existing=True)
-            self.__image.gl_load()
+            if self.__image is None or self.__image.filepath != rel_filepath:
+                self.__image = bpy.data.images.load(rel_filepath, check_existing=True)
+                self.__image.gl_load()
         except:
             pass
 
diff --git a/blenderkit/comments_utils.py b/blenderkit/comments_utils.py
index 121beeb1..f91e151f 100644
--- a/blenderkit/comments_utils.py
+++ b/blenderkit/comments_utils.py
@@ -27,8 +27,10 @@ import logging
 bk_logger = logging.getLogger('blenderkit')
 
 
-def upload_comment_thread(url, comment='', headers=None):
+def upload_comment_thread(url, comment='', api_key=None):
     ''' Upload rating thread function / disconnected from blender data.'''
+    headers = utils.get_headers(api_key)
+
     bk_logger.debug('upload comment ' + comment)
 
     # rating_url = url + rating_name + '/'
@@ -55,8 +57,10 @@ def upload_comment_thread(url, comment='', headers=None):
     #     print('ratings upload failed: %s' % str(e))
 
 
-def upload_comment_flag_thread(url, comment_id='', flag='like', headers=None):
+def upload_comment_flag_thread(url, comment_id='', flag='like', api_key=None):
     ''' Upload rating thread function / disconnected from blender data.'''
+    headers = utils.get_headers(api_key)
+
     bk_logger.debug('upload comment flag' + str(comment_id))
 
     # rating_url = url + rating_name + '/'
@@ -74,10 +78,10 @@ def upload_comment_flag_thread(url, comment_id='', flag='like', headers=None):
     #     print('ratings upload failed: %s' % str(e))
 
 
-def send_comment_to_thread(url, comment, headers):
+def send_comment_to_thread(url, comment, api_key):
     '''Sens rating into thread rating, main purpose is for tasks_queue.
     One function per property to avoid lost data due to stashing.'''
-    thread = threading.Thread(target=upload_comment_thread, args=(url, comment, headers))
+    thread = threading.Thread(target=upload_comment_thread, args=(url, comment, api_key))
     thread.start()
 
 
@@ -97,9 +101,9 @@ def get_comments_local(asset_id):
     return None
 
 
-def get_comments(asset_id, headers):
+def get_comments(asset_id, api_key):
     '''
-    Retrieve ratings from BlenderKit server. Can be run from a thread
+    Retrieve comments  from BlenderKit server. Can be run from a thread
     Parameters
     ----------
     asset_id
@@ -109,6 +113,8 @@ def get_comments(asset_id, headers):
     -------
     ratings - dict of type:value ratings
     '''
+    headers = utils.get_headers(api_key)
+
     url = paths.get_api_url() + 'comments/assets-uuidasset/' + asset_id + '/'
     params = {}
     r = rerequests.get(url, params=params, verify=True, headers=headers)
@@ -125,3 +131,63 @@ def get_comments(asset_id, headers):
         #     # store empty ratings too, so that server isn't checked repeatedly
         #     tasks_queue.add_task((store_rating_local_empty,(asset_id,)))
         # return ratings
+
+def store_notifications_local(notifications):
+    bpy.context.window_manager['bkit notifications'] = notifications
+
+def check_notifications_read():
+    '''checks if all notifications were already read, and removes them if so'''
+    all_read = True
+    notifications = bpy.context.window_manager.get('bkit notifications')
+    if notifications is None:
+        return True
+    for n in notifications:
+        if n['unread'] == 1:
+            all_read = False
+            return False
+    bpy.context.window_manager['bkit notifications'] = None
+    return True
+
+def get_notifications(api_key):
+    '''
+    Retrieve ratings from BlenderKit server. Can be run from a thread
+    Parameters
+    ----------
+    asset_id
+    headers
+
+    Returns
+    -------
+    ratings - dict of type:value ratings
+    '''
+    headers = utils.get_headers(api_key)
+
+    url = paths.get_api_url() + 'notifications/unread/'
+    params = {}
+    r = rerequests.get(url, params=params, verify=True, headers=headers)
+    if r is None:
+        return
+    if r.status_code == 200:
+        rj = r.json()
+        print(rj)
+        # store notifications -

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list