From f1d959b003e28ee2226b26651dfd6b0fff13d2e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vil=C3=A9m=20Duha?= Date: Fri, 30 Apr 2021 08:23:29 +0200 Subject: BlenderKit: fix layout of the floating preview was unorganized after recent cleanup -cleaned part of the old code --- blenderkit/search.py | 189 ++++--------------------------- blenderkit/ui.py | 289 +++++++++--------------------------------------- blenderkit/ui_bgl.py | 10 +- blenderkit/ui_panels.py | 35 +++--- blenderkit/upload.py | 8 ++ 5 files changed, 102 insertions(+), 429 deletions(-) diff --git a/blenderkit/search.py b/blenderkit/search.py index 95b4c0cf..67a353ac 100644 --- a/blenderkit/search.py +++ b/blenderkit/search.py @@ -191,7 +191,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, force = False) + categories.fetch_categories_thread(api_key, force=False) first_time = True @@ -238,7 +238,7 @@ def parse_result(r): # except: # utils.p('asset with no files-size') asset_type = r['assetType'] - if len(r['files']) > 0:#TODO remove this condition so all assets are parsed. + if len(r['files']) > 0: # TODO remove this condition so all assets are parsed. get_author(r) r['available_resolutions'] = [] @@ -262,7 +262,6 @@ def parse_result(r): # small_tname = paths.extract_filename_from_url(f['fileThumbnail']) # allthumbs.append(tname) # TODO just first thumb is used now. - if f['fileType'] == 'blend': durl = f['downloadUrl'].split('?')[0] # fname = paths.extract_filename_from_url(f['filePath']) @@ -270,7 +269,7 @@ def parse_result(r): if f['fileType'].find('resolution') > -1: r['available_resolutions'].append(resolutions.resolutions[f['fileType']]) - #code for more thumbnails + # code for more thumbnails # tdict = {} # for i, t in enumerate(allthumbs): # tdict['thumbnail_%i'] = t @@ -436,8 +435,8 @@ def timer_update(): load_previews() ui_props = bpy.context.scene.blenderkitUI - if len(result_field) < ui_props.scrolloffset or not(thread[0].params.get('get_next')): - #jump back + 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 @@ -572,13 +571,13 @@ def writeblockm(tooltip, mdata, key='', pretext=None, width=40): # for longer t return tooltip - def has(mdata, prop): if mdata.get(prop) is not None and mdata[prop] is not None and mdata[prop] is not False: return True else: return False + def generate_tooltip(mdata): col_w = 40 if type(mdata['parameters']) == list: @@ -586,155 +585,10 @@ def generate_tooltip(mdata): else: mparams = mdata['parameters'] t = '' - t = writeblock(t, mdata['displayName'], width=col_w) + t = writeblock(t, mdata['displayName'], width=int(col_w * .6)) # t += '\n' - # t = writeblockm(t, mdata, key='description', pretext='', width=col_w) - # if mdata['description'] != '': - # t += '\n' - return t -def generate_tooltip_old(mdata): - col_w = 40 - if type(mdata['parameters']) == list: - mparams = utils.params_to_dict(mdata['parameters']) - else: - mparams = mdata['parameters'] - t = '' - t = writeblock(t, mdata['displayName'], width=col_w) - t += '\n' - - t = writeblockm(t, mdata, key='description', pretext='', width=col_w) - if mdata['description'] != '': - t += '\n' - - bools = (('rig', None), ('animated', None), ('manifold', 'non-manifold'), ('scene', None), ('simulation', None), - ('uv', None)) - for b in bools: - if mparams.get(b[0]): - mdata['tags'].append(b[0]) - elif b[1] != None: - mdata['tags'].append(b[1]) - - bools_data = ('adult',) - for b in bools_data: - if mdata.get(b) and mdata[b]: - mdata['tags'].append(b) - t = writeblockm(t, mparams, key='designer', pretext='Designer', width=col_w) - t = writeblockm(t, mparams, key='manufacturer', pretext='Manufacturer', width=col_w) - t = writeblockm(t, mparams, key='designCollection', pretext='Design collection', width=col_w) - - # t = writeblockm(t, mparams, key='engines', pretext='engine', width = col_w) - # t = writeblockm(t, mparams, key='model_style', pretext='style', width = col_w) - # t = writeblockm(t, mparams, key='material_style', pretext='style', width = col_w) - # t = writeblockm(t, mdata, key='tags', width = col_w) - # t = writeblockm(t, mparams, key='condition', pretext='condition', width = col_w) - # t = writeblockm(t, mparams, key='productionLevel', pretext='production level', width = col_w) - if has(mdata, 'purePbr'): - t = writeblockm(t, mparams, key='pbrType', pretext='Pbr', width=col_w) - - t = writeblockm(t, mparams, key='designYear', pretext='Design year', width=col_w) - - if has(mparams, 'dimensionX'): - t += 'Size: %s x %s x %sm\n' % (utils.fmt_length(mparams['dimensionX']), - utils.fmt_length(mparams['dimensionY']), - utils.fmt_length(mparams['dimensionZ'])) - if has(mparams, 'faceCount') and mdata['assetType'] == 'model': - t += 'Face count: %s\n' % (mparams['faceCount']) - # t += 'face count: %s, render: %s\n' % (mparams['faceCount'], mparams['faceCountRender']) - - # write files size - this doesn't reflect true file size, since files size is computed from all asset files, including resolutions. - # if mdata.get('filesSize'): - # fs = utils.files_size_to_text(mdata['filesSize']) - # t += f'files size: {fs}\n' - - # t = writeblockm(t, mparams, key='meshPolyType', pretext='mesh type', width = col_w) - # t = writeblockm(t, mparams, key='objectCount', pretext='nubmber of objects', width = col_w) - - # t = writeblockm(t, mparams, key='materials', width = col_w) - # t = writeblockm(t, mparams, key='modifiers', width = col_w) - # t = writeblockm(t, mparams, key='shaders', width = col_w) - - # if has(mparams, 'textureSizeMeters'): - # t += 'Texture size: %s m\n' % utils.fmt_length(mparams['textureSizeMeters']) - - if has(mparams, 'textureResolutionMax') and mparams['textureResolutionMax'] > 0: - if not mparams.get('textureResolutionMin'): # for HDR's - t = writeblockm(t, mparams, key='textureResolutionMax', pretext='Resolution', width=col_w) - elif mparams.get('textureResolutionMin') == mparams['textureResolutionMax']: - t = writeblockm(t, mparams, key='textureResolutionMin', pretext='Texture resolution', width=col_w) - else: - t += 'Tex resolution: %i - %i\n' % (mparams.get('textureResolutionMin'), mparams['textureResolutionMax']) - - if has(mparams, 'thumbnailScale'): - t = writeblockm(t, mparams, key='thumbnailScale', pretext='Preview scale', width=col_w) - - # t += 'uv: %s\n' % mdata['uv'] - # t += '\n' - if mdata.get('license') == 'cc_zero': - t+= 'license: CC Zero\n' - else: - t+= 'license: Royalty free\n' - # t = writeblockm(t, mdata, key='license', width=col_w) - - fs = mdata.get('files') - - if utils.profile_is_validator(): - if fs and len(fs) > 2: - resolutions = 'Resolutions:' - list.sort(fs, key=lambda f: f['fileType']) - for f in fs: - if f['fileType'].find('resolution') > -1: - resolutions += f['fileType'][11:] + ' ' - resolutions += '\n' - t += resolutions.replace('_', '.') - - if mdata['isFree']: - t += 'Free plan\n' - else: - t += 'Full plan\n' - else: - if fs: - for f in fs: - if f['fileType'].find('resolution') > -1: - t += 'Asset has lower resolutions available\n' - break; - - # generator is for both upload preview and search, this is only after search - # if mdata.get('versionNumber'): - # # t = writeblockm(t, mdata, key='versionNumber', pretext='version', width = col_w) - # a_id = mdata['author'].get('id') - # if a_id != None: - # adata = bpy.context.window_manager['bkit authors'].get(str(a_id)) - # if adata != None: - # t += generate_author_textblock(adata) - - t += '\n' - rc = mdata.get('ratingsCount') - if rc: - t+='\n' - if rc: - rcount = min(rc['quality'], rc['workingHours']) - else: - rcount = 0 - - show_rating_threshold = 5 - - if rcount < show_rating_threshold and mdata['assetType'] != 'hdr': - t += f"Only assets with enough ratings \nshow the rating value. Please rate.\n" - if rc['quality'] >= show_rating_threshold: - # t += f"{int(mdata['ratingsAverage']['quality']) * '*'}\n" - t += f"* {round(mdata['ratingsAverage']['quality'],1)}\n" - if rc['workingHours'] >= show_rating_threshold: - t += f"Hours saved: {int(mdata['ratingsAverage']['workingHours'])}\n" - if utils.profile_is_validator(): - t += f"Score: {int(mdata['score'])}\n" - - t += f"Ratings count {rc['quality']}*/{rc['workingHours']}wh value " \ - f"{(mdata['ratingsAverage']['quality'],1)}*/{(mdata['ratingsAverage']['workingHours'],1)}wh\n" - if len(t.split('\n')) < 11: - t += '\n' - t += get_random_tip(mdata) - t += '\n' + # t = writeblockm(t, mdata, key='description', pretext='', width=col_w) return t @@ -751,14 +605,10 @@ def generate_author_textblock(adata): if adata not in (None, ''): col_w = 2000 if len(adata['firstName'] + adata['lastName']) > 0: - t = 'Author: %s %s\n' % (adata['firstName'], adata['lastName']) + t = '%s %s\n' % (adata['firstName'], adata['lastName']) t += '\n' - # if adata.get('aboutMeUrl') is not None: - # t = writeblockm(t, adata, key='aboutMeUrl', pretext='', width=col_w) - # t += '\n' if adata.get('aboutMe') is not None: t = writeblockm(t, adata, key='aboutMe', pretext='', width=col_w) - t += '\n' return t @@ -900,7 +750,8 @@ def get_profile(): thread.start() return a -def query_to_url(query = {}, params = {}): + +def query_to_url(query={}, params={}): # build a new request url = paths.get_api_url() + 'search/' @@ -944,15 +795,17 @@ def query_to_url(query = {}, params = {}): urlquery = url + requeststring return urlquery + def parse_html_formated_error(text): report = text[text.find('') + 7: text.find('')] return report + class Searcher(threading.Thread): query = None - def __init__(self, query, params, orig_result, tempdir = '', headers = None, urlquery = ''): + def __init__(self, query, params, orig_result, tempdir='', headers=None, urlquery=''): super(Searcher, self).__init__() self.query = query self.params = params @@ -975,13 +828,11 @@ class Searcher(threading.Thread): query = self.query params = self.params - t = time.time() mt('search thread started') # tempdir = paths.get_temp_dir('%s_search' % query['asset_type']) # json_filepath = os.path.join(tempdir, '%s_searchresult.json' % query['asset_type']) - rdata = {} rdata['results'] = [] @@ -1030,8 +881,6 @@ class Searcher(threading.Thread): imgpath = os.path.join(self.tempdir, imgname) thumb_small_filepaths.append(imgpath) - - if d["assetType"] == 'hdr': larege_thumb_url = d['thumbnailMiddleUrlNonsquared'] @@ -1043,8 +892,6 @@ class Searcher(threading.Thread): imgpath = os.path.join(self.tempdir, imgname) thumb_full_filepaths.append(imgpath) - - # for f in d['files']: # # TODO move validation of published assets to server, too manmy checks here. # if f['fileType'] == 'thumbnail' and f['fileThumbnail'] != None and f['fileThumbnailLarge'] != None: @@ -1319,7 +1166,7 @@ def add_search_process(query, params, orig_result): if not params['get_next']: urlquery = query_to_url(query, params) - thread = Searcher(query, params, orig_result, tempdir = tempdir, headers = headers, urlquery = urlquery) + thread = Searcher(query, params, orig_result, tempdir=tempdir, headers=headers, urlquery=urlquery) thread.start() search_threads.append([thread, tempdir, query['asset_type'], {}]) # 4th field is for results @@ -1513,11 +1360,13 @@ 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""" bl_idname = "view3d.blenderkit_search" @@ -1574,12 +1423,12 @@ class SearchOperator(Operator): if self.keywords != '': sprops.search_keywords = self.keywords - search(category=self.category, get_next=self.get_next, author_id=self.author_id) # bpy.ops.view3d.blenderkit_asset_bar() return {'FINISHED'} + class UrlOperator(Operator): """""" bl_idname = "wm.blenderkit_url" @@ -1594,7 +1443,7 @@ class UrlOperator(Operator): def description(cls, context, properties): return properties.tooltip - def execute(self,context): + def execute(self, context): bpy.ops.wm.url_open(url=self.url) return {'FINISHED'} diff --git a/blenderkit/ui.py b/blenderkit/ui.py index 94542834..6351bdc7 100644 --- a/blenderkit/ui.py +++ b/blenderkit/ui.py @@ -278,50 +278,35 @@ def draw_text_block(x=0, y=0, width=40, font_size=10, line_height=15, text='', c ui_bgl.draw_text(l, x, ytext, font_size, color) -def draw_tooltip(x, y, text='', author='', img=None, gravatar=None): +def draw_tooltip(x, y, name='', author='', img=None, gravatar=None): region = bpy.context.region scale = bpy.context.preferences.view.ui_scale t = time.time() - ttipmargin = 5 - textmargin = 10 - - font_height = int(12 * scale) - line_height = int(15 * scale) - nameline_height = int(23 * scale) - - lines = text.split('\n') - alines = author.split('\n') - ncolumns = 2 - # nlines = math.ceil((len(lines) - 1) / ncolumns) - nlines = max(len(lines) - 1, len(alines)) # math.ceil((len(lines) - 1) / ncolumns) - - texth = line_height * nlines + nameline_height - if not img or max(img.size[0], img.size[1]) == 0: return; + + x += 20 + y -= 20 + #first get image size scaled isizex = int(512 * scale * img.size[0] / max(img.size[0], img.size[1])) isizey = int(512 * scale * img.size[1] / max(img.size[0], img.size[1])) - estimated_height = 2 * ttipmargin + textmargin + isizey + ttipmargin = 5 * scale + #then do recurrent re-scaling, to know where to fit the tooltip + estimated_height = 2 * ttipmargin + isizey if estimated_height > y: scaledown = y / (estimated_height) scale *= scaledown - # we need to scale these down to have correct size if the tooltip wouldn't fit. - font_height = int(12 * scale) - line_height = int(15 * scale) - nameline_height = int(23 * scale) - - lines = text.split('\n') - texth = line_height * nlines + nameline_height isizex = int(512 * scale * img.size[0] / max(img.size[0], img.size[1])) isizey = int(512 * scale * img.size[1] / max(img.size[0], img.size[1])) - name_height = int(18 * scale) + ttipmargin = 5 * scale + textmargin = 12 * scale - x += 2 * ttipmargin - y -= 2 * ttipmargin + overlay_height = 90 * scale + name_height = int(20 * scale) width = isizex + 2 * ttipmargin @@ -330,119 +315,48 @@ def draw_tooltip(x, y, text='', author='', img=None, gravatar=None): if r.type == 'UI': properties_width = r.width + # limit to area borders x = min(x + width, region.width - properties_width) - width - bgcol = bpy.context.preferences.themes[0].user_interface.wcol_tooltip.inner - bgcol1 = (bgcol[0], bgcol[1], bgcol[2], .6) + # define_colors + background_color = bpy.context.preferences.themes[0].user_interface.wcol_tooltip.inner + background_overlay = (background_color[0], background_color[1], background_color[2], .8) textcol = bpy.context.preferences.themes[0].user_interface.wcol_tooltip.text textcol = (textcol[0], textcol[1], textcol[2], 1) - textcol_mild = (textcol[0] * .8, textcol[1] * .8, textcol[2] * .8, 1) - textcol_strong = (textcol[0] * 1.3, textcol[1] * 2.3, textcol[2] * 1.3, 1) - # textcol_strong = (0.4, 1, 0.3, 1) - white = (1, 1, 1, .1) # background ui_bgl.draw_rect(x - ttipmargin, y - 2 * ttipmargin - isizey, isizex + ttipmargin * 2, 2 * ttipmargin + isizey, - bgcol) + background_color) + # main preview image ui_bgl.draw_image(x, y - isizey - ttipmargin, isizex, isizey, img, 1) + # text overlay background ui_bgl.draw_rect(x - ttipmargin, y - 2 * ttipmargin - isizey, isizex + ttipmargin * 2, - 2 * ttipmargin + texth, - bgcol1) + ttipmargin + overlay_height , + background_overlay) + # draw gravatar - gsize = 40 + author_x_text = x + isizex - textmargin + gravatar_size = overlay_height * scale - 2 * textmargin + gravatar_y = y - isizey - ttipmargin + textmargin if gravatar is not None: - # ui_bgl.draw_image(x + isizex - gsize - textmargin, y - isizey + texth - gsize - nameline_height - textmargin, - # gsize, gsize, gravatar, 1) - # ui_bgl.draw_image(x + isizex / 2 + textmargin, y - isizey + texth - gsize - nameline_height - textmargin, - # gsize, gsize, gravatar, 1) - ui_bgl.draw_image(x + isizex / 2 + textmargin, y - isizey + texth - gsize - textmargin, - gsize, gsize, gravatar, 1) - - i = 0 - column_lines = -1 # start minus one for the name - xtext = x + textmargin - fsize = name_height - tcol = textcol - - for l in lines: - ytext = y - column_lines * line_height - nameline_height - ttipmargin - textmargin - isizey + texth - if i == 0: - ytext = y - name_height + 5 - isizey + texth - textmargin - elif i == len(lines) - 1: - ytext = y - (nlines - 1) * line_height - nameline_height - ttipmargin * 2 - isizey + texth - tcol = textcol - tsize = font_height - else: - fsize = font_height + author_x_text -= gravatar_size + textmargin + ui_bgl.draw_image(x + isizex - gravatar_size - textmargin, + gravatar_y, # + textmargin, + gravatar_size, gravatar_size, gravatar, 1) - if l[:4] == 'Tip:' or l[:11] == 'Please rate': - tcol = textcol_strong - fsize = font_height + 1 + name_x = x + textmargin + name_y = y - isizey + overlay_height - textmargin - name_height + ui_bgl.draw_text(name, name_x, name_y, name_height, textcol) - i += 1 - column_lines += 1 - ui_bgl.draw_text(l, xtext, ytext, fsize, tcol) - xtext += int(isizex / ncolumns) - - column_lines = 1 - i = 0 - for l in alines: - if gravatar is not None: - if column_lines == 1: - xtext += gsize + textmargin - if column_lines == 4: - xtext -= gsize + textmargin - - ytext = y - column_lines * line_height - nameline_height - ttipmargin - textmargin - isizey + texth - if i == 0: - fsize = name_height-4 - ytext = y - name_height + 5 - isizey + texth - textmargin - elif i == len(lines) - 1: - ytext = y - (nlines - 1) * line_height - nameline_height - ttipmargin * 2 - isizey + texth - tcol = textcol - tsize = font_height - elif (i > 0 and alines[i - 1][:7] == 'Author:'): - tcol = textcol_strong - fsize = font_height + 2 - else: - fsize = font_height - tcol = textcol - - if l[:4] == 'Tip:' or l[:11] == 'Please rate': - tcol = textcol_strong - fsize = font_height + 1 - - i += 1 - column_lines += 1 - ui_bgl.draw_text(l, xtext, ytext, fsize, tcol) - - # for l in lines: - # if column_lines >= nlines: - # xtext += int(isizex / ncolumns) - # column_lines = 0 - # ytext = y - column_lines * line_height - nameline_height - ttipmargin - textmargin - isizey + texth - # if i == 0: - # ytext = y - name_height + 5 - isizey + texth - textmargin - # elif i == len(lines) - 1: - # ytext = y - (nlines - 1) * line_height - nameline_height - ttipmargin * 2 - isizey + texth - # tcol = textcol - # tsize = font_height - # else: - # if l[:4] == 'Tip:': - # tcol = textcol_strong - # fsize = font_height - # i += 1 - # column_lines += 1 - # ui_bgl.draw_text(l, xtext, ytext, fsize, tcol) - - t = time.time() + author_text_size = int(name_height * .7) + ui_bgl.draw_text(author, author_x_text, gravatar_y, author_text_size, textcol, ralign=True) def draw_tooltip_with_author(asset_data, x, y): @@ -456,111 +370,14 @@ def draw_tooltip_with_author(asset_data, x, y): if a is not None and a != '': if a.get('gravatarImg') is not None: gimg = utils.get_hidden_image(a['gravatarImg'], a['gravatarHash']) - atip = a['tooltip'] - - tooltip = f"{asset_data['displayName']}\n\n" \ - f"Left click to drag to append/link.\nRight click for more." - - # scene = bpy.context.scene - # ui_props = scene.blenderkitUI - author_s = '' - if not gimg: - author_s = 'Author: ' - draw_tooltip(x, y, text=asset_data['displayName']+'\n\n', author=f"{author_s}{a['firstName']} {a['lastName']}", img=img, + # author_s = '' + # if not gimg: + # author_s = 'Author: ' + draw_tooltip(x, y, name=asset_data['displayName'], author=f"by {a['firstName']} {a['lastName']}", img=img, gravatar=gimg) -def draw_tooltip_old(x, y, text='', author='', img=None): - region = bpy.context.region - scale = bpy.context.preferences.view.ui_scale - t = time.time() - - ttipmargin = 10 - - font_height = int(12 * scale) - line_height = int(15 * scale) - nameline_height = int(23 * scale) - - lines = text.split('\n') - ncolumns = 2 - nlines = math.ceil((len(lines) - 1) / ncolumns) - texth = line_height * nlines + nameline_height - - isizex = int(512 * scale * img.size[0] / max(img.size[0], img.size[1])) - isizey = int(512 * scale * img.size[1] / max(img.size[0], img.size[1])) - - estimated_height = texth + 3 * ttipmargin + isizey - - if estimated_height > y: - scaledown = y / (estimated_height) - scale *= scaledown - # we need to scale these down to have correct size if the tooltip wouldn't fit. - font_height = int(12 * scale) - line_height = int(15 * scale) - nameline_height = int(23 * scale) - - lines = text.split('\n') - ncolumns = 2 - nlines = math.ceil((len(lines) - 1) / ncolumns) - texth = line_height * nlines + nameline_height - - isizex = int(512 * scale * img.size[0] / max(img.size[0], img.size[1])) - isizey = int(512 * scale * img.size[1] / max(img.size[0], img.size[1])) - - name_height = int(18 * scale) - - x += 2 * ttipmargin - y -= 2 * ttipmargin - - width = isizex + 2 * ttipmargin - - properties_width = 0 - for r in bpy.context.area.regions: - if r.type == 'UI': - properties_width = r.width - - x = min(x + width, region.width - properties_width) - width - - bgcol = bpy.context.preferences.themes[0].user_interface.wcol_tooltip.inner - textcol = bpy.context.preferences.themes[0].user_interface.wcol_tooltip.text - textcol = (textcol[0], textcol[1], textcol[2], 1) - textcol1 = (textcol[0] * .8, textcol[1] * .8, textcol[2] * .8, 1) - white = (1, 1, 1, .1) - - ui_bgl.draw_rect(x - ttipmargin, - y - texth - 2 * ttipmargin - isizey, - isizex + ttipmargin * 2, - texth + 3 * ttipmargin + isizey, - bgcol) - - i = 0 - column_lines = -1 # start minus one for the name - xtext = x - fsize = name_height - tcol = textcol - for l in lines: - if column_lines >= nlines: - xtext += int(isizex / ncolumns) - column_lines = 0 - ytext = y - column_lines * line_height - nameline_height - ttipmargin - if i == 0: - ytext = y - name_height + 5 - elif i == len(lines) - 1: - ytext = y - (nlines - 1) * line_height - nameline_height - ttipmargin - tcol = textcol - tsize = font_height - else: - if l[:5] == 'tags:': - tcol = textcol1 - fsize = font_height - i += 1 - column_lines += 1 - ui_bgl.draw_text(l, xtext, ytext, fsize, tcol) - t = time.time() - ui_bgl.draw_image(x, y - texth - isizey - ttipmargin, isizex, isizey, img, 1) - - def draw_callback_2d(self, context): if not utils.guard_from_crash(): return @@ -602,14 +419,6 @@ def draw_downloader(x, y, percent=0, img=None, text=''): # ui_bgl.draw_text(asset_data['filesSize']) if text: ui_bgl.draw_text(text, x, y - 15, 12, colors.TEXT) - # asset_data and asset_data.get('filesSize'): - # fs = asset_data['filesSize'] - # fsmb = fs // (1024 * 1024) - # fskb = fs % 1024 - # if fsmb == 0: - # t += 'files size: %iKB\n' % fskb - # else: - # t += 'files size: %iMB %iKB\n' % (fsmb, fskb) def draw_progress(x, y, text='', percent=None, color=colors.GREEN): @@ -665,10 +474,10 @@ def draw_callback_2d_progress(self, context): for process in bg_blender.bg_processes: tcom = process[1] - n='' + n = '' if tcom.name is not None: - n = tcom.name +': ' - draw_progress(x, y - index * 30, '%s' % n+tcom.lasttext, + n = tcom.name + ': ' + draw_progress(x, y - index * 30, '%s' % n + tcom.lasttext, tcom.progress) index += 1 global reports @@ -697,7 +506,7 @@ def draw_callback_2d_upload_preview(self, context): img = utils.get_hidden_image(ui_props.thumbnail_image, 'upload_preview') - draw_tooltip(ui_props.bar_x, ui_props.bar_y, text=ui_props.tooltip, img=img) + draw_tooltip(ui_props.bar_x, ui_props.bar_y, name=ui_props.tooltip, img=img) def is_upload_old(asset_data): @@ -1433,7 +1242,8 @@ class AssetBarOperator(bpy.types.Operator): or ui_props.asset_type == 'SCENE' or ui_props.asset_type == 'HDR': export_data, upload_data = upload.get_upload_data(context=context, asset_type=ui_props.asset_type) if upload_data: - ui_props.tooltip = search.generate_tooltip(upload_data) + # print(upload_data) + ui_props.tooltip = upload_data['displayName']#search.generate_tooltip(upload_data) return {'PASS_THROUGH'} @@ -1587,7 +1397,7 @@ class AssetBarOperator(bpy.types.Operator): my = event.mouse_y - r.y if event.value == 'PRESS' and mouse_in_asset_bar(mx, my): - context.window.cursor_warp(event.mouse_x-400, event.mouse_y-20); + context.window.cursor_warp(event.mouse_x - 400, event.mouse_y - 20); bpy.ops.wm.blenderkit_asset_popup('INVOKE_DEFAULT') context.window.cursor_warp(event.mouse_x, event.mouse_y); @@ -1784,8 +1594,8 @@ class AssetBarOperator(bpy.types.Operator): asset_index=asset_search_index, # replace_resolution=True, invoke_resolution=True, - max_resolution = asset_data.get('max_resolution', 0) - ) + max_resolution=asset_data.get('max_resolution', 0) + ) else: bpy.ops.scene.blenderkit_download( # asset_type=ui_props.asset_type, asset_index=asset_search_index, @@ -1893,7 +1703,7 @@ class AssetBarOperator(bpy.types.Operator): context.window_manager.modal_handler_add(self) ui_props.assetbar_on = True - #in an exceptional case these were accessed before drag start. + # in an exceptional case these were accessed before drag start. self.drag_start_x = 0 self.drag_start_y = 0 @@ -2190,7 +2000,8 @@ class RunAssetBarWithContext(bpy.types.Operator): do_search=self.do_search) else: - bpy.ops.view3d.blenderkit_asset_bar(C_dict, 'INVOKE_REGION_WIN', keep_running=self.keep_running, do_search=self.do_search) + bpy.ops.view3d.blenderkit_asset_bar(C_dict, 'INVOKE_REGION_WIN', keep_running=self.keep_running, + do_search=self.do_search) return {'FINISHED'} diff --git a/blenderkit/ui_bgl.py b/blenderkit/ui_bgl.py index fabda329..79ecffd9 100644 --- a/blenderkit/ui_bgl.py +++ b/blenderkit/ui_bgl.py @@ -120,10 +120,14 @@ def draw_image(x, y, width, height, image, transparency, crop=(0, 0, 1, 1)): bgl.glDisable(bgl.GL_TEXTURE_2D) -def draw_text(text, x, y, size, color=(1, 1, 1, 0.5)): - font_id = 0 +def draw_text(text, x, y, size, color=(1, 1, 1, 0.5), ralign = False): + font_id = 1 # bgl.glColor4f(*color) blf.color(font_id, color[0], color[1], color[2], color[3]) - blf.position(font_id, x, y, 0) blf.size(font_id, size, 72) + if ralign: + width,height = blf.dimensions(font_id, text) + x-=width + blf.position(font_id, x, y, 0) + blf.draw(font_id, text) diff --git a/blenderkit/ui_panels.py b/blenderkit/ui_panels.py index 3ac9366a..09231e6f 100644 --- a/blenderkit/ui_panels.py +++ b/blenderkit/ui_panels.py @@ -1377,6 +1377,7 @@ class OBJECT_MT_blenderkit_asset_menu(bpy.types.Menu): asset_data = sr[ui_props.active_index] draw_asset_context_menu(self.layout, context, asset_data, from_panel=False) + def numeric_to_str(s): if s: s = str(round(s)) @@ -1384,6 +1385,7 @@ def numeric_to_str(s): s = '-' return s + class AssetPopupCard(bpy.types.Operator): """Generate Cycles thumbnail for model assets""" bl_idname = "wm.blenderkit_asset_popup" @@ -1558,7 +1560,8 @@ class AssetPopupCard(bpy.types.Operator): return self.draw_property(layout, pretext, parameter) - def draw_tooltip(self, layout): + def draw_properties(self, layout): + if type(self.asset_data['parameters']) == list: mparams = utils.params_to_dict(self.asset_data['parameters']) else: @@ -1568,14 +1571,14 @@ class AssetPopupCard(bpy.types.Operator): if len(self.asset_data['description']) > 0: box = layout.box() box.scale_y = 0.8 - box.label(text='Description:') + box.label(text='Description') utils.label_multiline(box, self.asset_data['description'], width=200) pcoll = icons.icon_collections["main"] box = layout.box() box.scale_y = 0.8 - + box.label(text='Properties') if self.asset_data.get('license') == 'cc_zero': t = 'CC Zero' icon = pcoll['cc0'] @@ -1623,7 +1626,7 @@ class AssetPopupCard(bpy.types.Operator): tooltip=verification_status_tooltips[self.asset_data['verificationStatus']] ) - #resolution/s + # resolution/s # fs = self.asset_data['files'] # # if fs and len(fs) > 2: @@ -1636,11 +1639,11 @@ class AssetPopupCard(bpy.types.Operator): # self.draw_property(box, 'Resolutions:', resolutions) resolution = utils.get_param(self.asset_data, 'textureResolutionMax') if resolution is not None: - ress = f"{int(round(resolution/1024,0))}K" + ress = f"{int(round(resolution / 1024, 0))}K" self.draw_property(box, 'Resolution', ress) self.draw_asset_parameter(box, key='designer', pretext='Designer') - self.draw_asset_parameter(box, key='manufacturer', pretext='Manufacturer')#TODO make them clickable! + self.draw_asset_parameter(box, key='manufacturer', pretext='Manufacturer') # TODO make them clickable! self.draw_asset_parameter(box, key='designCollection', pretext='Collection') self.draw_asset_parameter(box, key='designVariant', pretext='Variant') self.draw_asset_parameter(box, key='designYear', pretext='Design year') @@ -1655,11 +1658,11 @@ class AssetPopupCard(bpy.types.Operator): if utils.get_param(self.asset_data, 'dimensionX'): t = '%s×%s×%s m' % (utils.fmt_length(mparams['dimensionX']), - utils.fmt_length(mparams['dimensionY']), - utils.fmt_length(mparams['dimensionZ'])) + utils.fmt_length(mparams['dimensionY']), + utils.fmt_length(mparams['dimensionZ'])) self.draw_property(box, 'Size:', t) - #Free/Full plan or private Access + # Free/Full plan or private Access if self.asset_data['isPrivate']: t = 'Private' self.draw_property(box, 'Access:', t, icon='LOCKED') @@ -1672,9 +1675,6 @@ class AssetPopupCard(bpy.types.Operator): icon = pcoll['full'] self.draw_property(box, 'Access:', t, icon_value=icon.icon_id) - - - def draw_author(self, layout, width=330): image_split = 0.25 text_width = width @@ -1682,11 +1682,10 @@ class AssetPopupCard(bpy.types.Operator): a = authors.get(self.asset_data['author']['id']) if a is not None: # or a is '' or (a.get('gravatarHash') is not None and a.get('gravatarImg') is None): - row = layout.row() author_box = row.box() author_box.scale_y = 0.6 # get text lines closer to each other - author_box.label(text=' ') # just one extra line to give spacing + author_box.label(text='Author') # just one extra line to give spacing if hasattr(self, 'gimg'): author_left = author_box.split(factor=0.25) @@ -1701,7 +1700,8 @@ class AssetPopupCard(bpy.types.Operator): col = row.column() utils.label_multiline(col, text=a['tooltip'], width=text_width) - if upload.can_edit_asset(asset_data=self.asset_data) and a.get('aboutMe') is not None and len( + #check if author didn't fill any data about himself and prompt him if that's the case + if upload.user_is_owner(asset_data=self.asset_data) and a.get('aboutMe') is not None and len( a.get('aboutMe', '')) == 0: row = col.row() row.enabled = False @@ -1769,7 +1769,7 @@ class AssetPopupCard(bpy.types.Operator): row.label(text=str(q), icon='SOLO_ON') row.label(text=str(c), icon_value=pcoll['dumbbell'].icon_id) - if rcount<= show_rating_threshold: + if rcount <= show_rating_threshold: box_thumbnail.alert = True box_thumbnail.label(text=f"") @@ -1783,7 +1783,7 @@ class AssetPopupCard(bpy.types.Operator): # left - tooltip & params row = box.row() split_left_left = row.split(factor=0.7) - self.draw_tooltip(split_left_left) + self.draw_properties(split_left_left) # right - menu col1 = split_left_left.split() @@ -1815,6 +1815,7 @@ class AssetPopupCard(bpy.types.Operator): self.draw_menu_desc_author(context, split_right) ratings_box = layout.box() + ratings_box.scale_y = 0.7 ratings_box.label(text='Rate asset quality:') ratings.draw_ratings_menu(self, context, ratings_box) tip_box = layout.box() diff --git a/blenderkit/upload.py b/blenderkit/upload.py index b1d20f94..9fd54187 100644 --- a/blenderkit/upload.py +++ b/blenderkit/upload.py @@ -567,6 +567,14 @@ def update_free_full(self, context): " based on our fair share system. " \ "Part of subscription is sent to artists based on usage by paying users.") +def user_is_owner(asset_data=None): + '''Checks if the current logged in user is owner of the asset''' + profile = bpy.context.window_manager.get('bkit profile') + if profile is None: + return False + if int(asset_data['author']['id']) == int(profile['user']['id']): + return True + return False def can_edit_asset(active_index=-1, asset_data=None): if active_index < 0 and not asset_data: -- cgit v1.2.3