BlenderKit: fist steps to comments integration
This commit is contained in:
parent
50e38d250d
commit
884f776180
|
@ -50,6 +50,7 @@ if "bpy" in locals():
|
|||
paths = reload(paths)
|
||||
ratings = reload(ratings)
|
||||
ratings_utils = reload(ratings_utils)
|
||||
comments_utils = reload(comments_utils)
|
||||
resolutions = reload(resolutions)
|
||||
search = reload(search)
|
||||
tasks_queue = reload(tasks_queue)
|
||||
|
@ -86,6 +87,7 @@ else:
|
|||
from blenderkit import paths
|
||||
from blenderkit import ratings
|
||||
from blenderkit import ratings_utils
|
||||
from blenderkit import comments_utils
|
||||
from blenderkit import resolutions
|
||||
from blenderkit import search
|
||||
from blenderkit import tasks_queue
|
||||
|
|
|
@ -20,22 +20,82 @@
|
|||
import bpy
|
||||
from blenderkit import utils, paths, tasks_queue, rerequests
|
||||
|
||||
import threading
|
||||
import requests
|
||||
import logging
|
||||
|
||||
bk_logger = logging.getLogger('blenderkit')
|
||||
|
||||
|
||||
from blenderkit import rerequests
|
||||
user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
|
||||
api_key = user_preferences.api_key
|
||||
headers = utils.get_headers(api_key)
|
||||
r = rerequests.get(f"{api_url}/comments/assets-uuidasset/{asset_data['assetBaseId']}/", headers = headers)
|
||||
r= r.json()
|
||||
print(r)
|
||||
def upload_comment_thread(url, comment='', headers=None):
|
||||
''' Upload rating thread function / disconnected from blender data.'''
|
||||
bk_logger.debug('upload comment ' + comment)
|
||||
|
||||
# rating_url = url + rating_name + '/'
|
||||
data = {
|
||||
"content_type": "",
|
||||
"object_pk": "",
|
||||
"timestamp": "",
|
||||
"security_hash": "",
|
||||
"honeypot": "",
|
||||
"name": "",
|
||||
"email": "",
|
||||
"url": "",
|
||||
"comment": comment,
|
||||
"followup": False,
|
||||
"reply_to": None
|
||||
}
|
||||
|
||||
# try:
|
||||
r = rerequests.put(url, data=data, verify=True, headers=headers)
|
||||
print(r)
|
||||
print(dir(r))
|
||||
print(r.text)
|
||||
# except requests.exceptions.RequestException as e:
|
||||
# print('ratings upload failed: %s' % str(e))
|
||||
|
||||
|
||||
def store_comments_local(asset_id, type='quality', value=0):
|
||||
def upload_comment_flag_thread(url, comment_id='', flag='like', headers=None):
|
||||
''' Upload rating thread function / disconnected from blender data.'''
|
||||
bk_logger.debug('upload comment flag' + str(comment_id))
|
||||
|
||||
# rating_url = url + rating_name + '/'
|
||||
data = {
|
||||
"comment": comment_id,
|
||||
"flag": flag,
|
||||
}
|
||||
|
||||
# try:
|
||||
r = rerequests.post(url, data=data, verify=True, headers=headers)
|
||||
print(r)
|
||||
print(dir(r))
|
||||
print(r.text)
|
||||
# except requests.exceptions.RequestException as e:
|
||||
# print('ratings upload failed: %s' % str(e))
|
||||
|
||||
|
||||
def send_comment_to_thread(url, comment, headers):
|
||||
'''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.start()
|
||||
|
||||
|
||||
def store_comments_local(asset_id, comments):
|
||||
context = bpy.context
|
||||
ar = context.window_manager['asset comments']
|
||||
ar[asset_id] = ar.get(asset_id, {})
|
||||
ar[asset_id][type] = value
|
||||
ac = context.window_manager.get('asset comments', {})
|
||||
ac[asset_id] = comments
|
||||
context.window_manager['asset comments'] = ac
|
||||
|
||||
|
||||
def get_comments_local(asset_id):
|
||||
context = bpy.context
|
||||
context.window_manager['asset comments'] = context.window_manager.get('asset comments', {})
|
||||
comments = context.window_manager['asset comments'].get(asset_id)
|
||||
if comments:
|
||||
return comments
|
||||
return None
|
||||
|
||||
|
||||
def get_comments(asset_id, headers):
|
||||
'''
|
||||
|
@ -59,7 +119,7 @@ def get_comments(asset_id, headers):
|
|||
comments = []
|
||||
# store comments - send them to task queue
|
||||
|
||||
tasks_queue.add_task((store_comments_local,(asset_id, rj['results'])))
|
||||
tasks_queue.add_task((store_comments_local, (asset_id, rj['results'])))
|
||||
|
||||
# if len(rj['results'])==0:
|
||||
# # store empty ratings too, so that server isn't checked repeatedly
|
||||
|
|
|
@ -78,7 +78,7 @@ def store_rating_local(asset_id, type='quality', value=0):
|
|||
context = bpy.context
|
||||
ar = context.window_manager['asset ratings']
|
||||
ar[asset_id] = ar.get(asset_id, {})
|
||||
ar[asset_id][type] = value
|
||||
ar[asset_id]['type'] = value
|
||||
|
||||
|
||||
def get_rating(asset_id, headers):
|
||||
|
|
|
@ -171,11 +171,13 @@ def unpack_asset(data):
|
|||
#mark asset browser asset
|
||||
data_block = None
|
||||
if asset_data['assetType'] == 'model':
|
||||
|
||||
for c in bpy.data.collections:
|
||||
if c.get('asset_data') is not None:
|
||||
c.asset_mark()
|
||||
data_block = c
|
||||
for ob in bpy.data.objects:
|
||||
if ob.parent is None and ob in bpy.context.visible_objects:
|
||||
ob.asset_mark()
|
||||
# for c in bpy.data.collections:
|
||||
# if c.get('asset_data') is not None:
|
||||
# c.asset_mark()
|
||||
# data_block = c
|
||||
elif asset_data['assetType'] == 'material':
|
||||
for m in bpy.data.materials:
|
||||
m.asset_mark()
|
||||
|
@ -197,9 +199,10 @@ def unpack_asset(data):
|
|||
# if this isn't here, blender crashes when saving file.
|
||||
bpy.context.preferences.filepaths.file_preview_type = 'NONE'
|
||||
|
||||
bpy.ops.wm.save_mainfile(compress=False)
|
||||
bpy.ops.wm.save_as_mainfile(filepath = bpy.data.filepath, compress=False)
|
||||
# now try to delete the .blend1 file
|
||||
try:
|
||||
|
||||
os.remove(bpy.data.filepath + '1')
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
|
|
@ -17,7 +17,8 @@
|
|||
# ##### END GPL LICENSE BLOCK #####
|
||||
|
||||
|
||||
from blenderkit import paths, ratings, ratings_utils, utils, download, categories, icons, search, resolutions, ui, \
|
||||
from blenderkit import paths, comments_utils, ratings, ratings_utils, utils, download, categories, icons, search, \
|
||||
resolutions, ui, \
|
||||
tasks_queue, \
|
||||
autothumb, upload
|
||||
|
||||
|
@ -870,6 +871,7 @@ class VIEW3D_PT_blenderkit_advanced_material_search(Panel):
|
|||
row.prop(props, "search_file_size_max", text='Max')
|
||||
layout.prop(props, "quality_limit", slider=True)
|
||||
|
||||
|
||||
class VIEW3D_PT_blenderkit_advanced_HDR_search(Panel):
|
||||
bl_category = "BlenderKit"
|
||||
bl_idname = "VIEW3D_PT_blenderkit_advanced_HDR_search"
|
||||
|
@ -895,7 +897,6 @@ class VIEW3D_PT_blenderkit_advanced_HDR_search(Panel):
|
|||
layout.prop(props, "true_hdr")
|
||||
|
||||
|
||||
|
||||
class VIEW3D_PT_blenderkit_categories(Panel):
|
||||
bl_category = "BlenderKit"
|
||||
bl_idname = "VIEW3D_PT_blenderkit_categories"
|
||||
|
@ -1199,7 +1200,7 @@ class BlenderKitWelcomeOperator(bpy.types.Operator):
|
|||
props = bpy.context.window_manager.blenderkit_models
|
||||
score_limit = 1000
|
||||
|
||||
props.search_keywords = ''#random_search[1]
|
||||
props.search_keywords = '' # random_search[1]
|
||||
props.search_keywords += f'+is_free:true+score_gte:{score_limit}+order:-created' # random_search[1]
|
||||
# search.search()
|
||||
return {'FINISHED'}
|
||||
|
@ -1938,6 +1939,37 @@ class AssetPopupCard(bpy.types.Operator, ratings_utils.RatingsProperties):
|
|||
push_op_left(name_row, strength=3)
|
||||
op = name_row.operator('view3d.close_popup_button', text='', icon='CANCEL')
|
||||
|
||||
def draw_comment(self, context, layout, comment, width=330):
|
||||
box = layout.box()
|
||||
box.emboss = 'NORMAL'
|
||||
is_moderator = comment['userModerator']
|
||||
if is_moderator:
|
||||
role_text = f" - moderator"
|
||||
else:
|
||||
role_text = ""
|
||||
box.label(text=f"{comment['submitDate']} - {comment['userName']}{role_text}")
|
||||
utils.label_multiline(box, text=comment['comment'], width = width)
|
||||
removal = False
|
||||
likes = 0
|
||||
dislikes = 0
|
||||
for l in comment['flags']:
|
||||
if l['flag'] == 'like':
|
||||
likes +=1
|
||||
if l['flag'] == 'dislike':
|
||||
dislikes +=1
|
||||
if l['flag'] == 'removal':
|
||||
removal = True
|
||||
row = box.row()
|
||||
|
||||
row.label(text = str(likes), icon = 'TRIA_UP')
|
||||
row.label(text = str(dislikes), icon = 'TRIA_DOWN')
|
||||
if removal:
|
||||
row.label(text = '', icon = 'ERROR')
|
||||
|
||||
|
||||
#box.label(text=str(comment['flags']))
|
||||
|
||||
|
||||
def draw(self, context):
|
||||
layout = self.layout
|
||||
# top draggable bar with name of the asset
|
||||
|
@ -1963,6 +1995,12 @@ class AssetPopupCard(bpy.types.Operator, ratings_utils.RatingsProperties):
|
|||
|
||||
tip_box = layout.box()
|
||||
tip_box.label(text=self.tip)
|
||||
# comments
|
||||
if utils.profile_is_validator() and bpy.app.debug_value == 2:
|
||||
if self.comments is not None:
|
||||
for comment in self.comments:
|
||||
self.draw_comment(context, layout, comment)
|
||||
|
||||
|
||||
def execute(self, context):
|
||||
wm = context.window_manager
|
||||
|
@ -1993,6 +2031,17 @@ class AssetPopupCard(bpy.types.Operator, ratings_utils.RatingsProperties):
|
|||
# pre-fill ratings
|
||||
self.prefill_ratings()
|
||||
|
||||
# get comments
|
||||
if utils.profile_is_validator() and bpy.app.debug_value == 2:
|
||||
user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
|
||||
api_key = user_preferences.api_key
|
||||
headers = utils.get_headers(api_key)
|
||||
comments = comments_utils.get_comments_local(asset_data['assetBaseId'])
|
||||
if comments is None:
|
||||
comments_utils.get_comments(asset_data['assetBaseId'], headers)
|
||||
comments = bpy.context.window_manager.get('asset comments', {})
|
||||
self.comments = comments.get(asset_data['assetBaseId'], [])
|
||||
|
||||
return wm.invoke_popup(self, width=self.width)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue