GPencil Tools: Canvas rotate improvement
[blender-addons.git] / blenderkit / ratings.py
blobf6a221c4d11a46bd0637b3f2d94e8c860a3a6073
1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
19 from blenderkit import paths, utils, rerequests, tasks_queue, ratings_utils, icons
21 import bpy
22 import requests, threading
23 import logging
25 bk_logger = logging.getLogger('blenderkit')
27 from bpy.props import (
28 IntProperty,
29 FloatProperty,
30 StringProperty,
31 EnumProperty,
32 BoolProperty,
33 PointerProperty,
35 from bpy.types import (
36 Operator,
37 Panel,
41 def pretty_print_POST(req):
42 """
43 pretty print a request
44 """
45 print('{}\n{}\n{}\n\n{}'.format(
46 '-----------START-----------',
47 req.method + ' ' + req.url,
48 '\n'.join('{}: {}'.format(k, v) for k, v in req.headers.items()),
49 req.body,
53 def upload_review_thread(url, reviews, headers):
54 r = rerequests.put(url, data=reviews, verify=True, headers=headers)
56 # except requests.exceptions.RequestException as e:
57 # print('reviews upload failed: %s' % str(e))
60 def upload_rating(asset):
61 user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
62 api_key = user_preferences.api_key
63 headers = utils.get_headers(api_key)
65 bkit_ratings = asset.bkit_ratings
66 # print('rating asset', asset_data['name'], asset_data['assetBaseId'])
67 url = paths.get_api_url() + 'assets/' + asset['asset_data']['id'] + '/rating/'
69 ratings = [
73 if bkit_ratings.rating_quality > 0.1:
74 ratings = (('quality', bkit_ratings.rating_quality),)
75 tasks_queue.add_task((ratings_utils.send_rating_to_thread_quality, (url, ratings, headers)), wait=2.5,
76 only_last=True)
77 if bkit_ratings.rating_work_hours > 0.1:
78 ratings = (('working_hours', round(bkit_ratings.rating_work_hours, 1)),)
79 tasks_queue.add_task((ratings_utils.send_rating_to_thread_work_hours, (url, ratings, headers)), wait=2.5,
80 only_last=True)
82 thread = threading.Thread(target=ratings_utils.upload_rating_thread, args=(url, ratings, headers))
83 thread.start()
85 url = paths.get_api_url() + 'assets/' + asset['asset_data']['id'] + '/review'
87 reviews = {
88 'reviewText': bkit_ratings.rating_compliments,
89 'reviewTextProblems': bkit_ratings.rating_problems,
91 if not (bkit_ratings.rating_compliments == '' and bkit_ratings.rating_compliments == ''):
92 thread = threading.Thread(target=upload_review_thread, args=(url, reviews, headers))
93 thread.start()
95 # the info that the user rated an item is stored in the scene
96 s = bpy.context.scene
97 s['assets rated'] = s.get('assets rated', {})
98 if bkit_ratings.rating_quality > 0.1 and bkit_ratings.rating_work_hours > 0.1:
99 s['assets rated'][asset['asset_data']['assetBaseId']] = True
102 def get_assets_for_rating():
104 gets assets from scene that could/should be rated by the user.
105 TODO this is only a draft.
108 assets = []
109 for ob in bpy.context.scene.objects:
110 if ob.get('asset_data'):
111 assets.append(ob)
112 for m in bpy.data.materials:
113 if m.get('asset_data'):
114 assets.append(m)
115 for b in bpy.data.brushes:
116 if b.get('asset_data'):
117 assets.append(b)
118 return assets
121 asset_types = (
122 ('MODEL', 'Model', 'set of objects'),
123 ('SCENE', 'Scene', 'scene'),
124 ('HDR', 'HDR', 'hdr'),
125 ('MATERIAL', 'Material', 'any .blend Material'),
126 ('TEXTURE', 'Texture', 'a texture, or texture set'),
127 ('BRUSH', 'Brush', 'brush, can be any type of blender brush'),
128 ('ADDON', 'Addon', 'addnon'),
132 # TODO drop this operator, not needed anymore.
133 class UploadRatingOperator(bpy.types.Operator):
134 """Upload rating to the web db"""
135 bl_idname = "object.blenderkit_rating_upload"
136 bl_label = "Send Rating"
137 bl_options = {'REGISTER', 'UNDO', 'INTERNAL'}
139 # type of upload - model, material, textures, e.t.c.
140 # asset_type: EnumProperty(
141 # name="Type",
142 # items=asset_types,
143 # description="Type of asset",
144 # default="MODEL",
147 # @classmethod
148 # def poll(cls, context):
149 # return bpy.context.active_object != None and bpy.context.active_object.get('asset_id') is not None
150 def draw(self, context):
151 layout = self.layout
152 layout.label(text='Rating sent to server. Thanks for rating!')
154 def execute(self, context):
155 return {'FINISHED'}
157 def invoke(self, context, event):
158 wm = context.window_manager
159 asset = utils.get_active_asset()
160 upload_rating(asset)
161 return wm.invoke_props_dialog(self)
164 def draw_ratings_menu(self, context, layout):
165 pcoll = icons.icon_collections["main"]
167 profile_name = ''
168 profile = bpy.context.window_manager.get('bkit profile')
169 if profile and len(profile['user']['firstName']) > 0:
170 profile_name = ' ' + profile['user']['firstName']
172 col = layout.column()
173 # layout.template_icon_view(bkit_ratings, property, show_labels=False, scale=6.0, scale_popup=5.0)
174 row = col.row()
175 row.label(text='Quality:', icon='SOLO_ON')
176 row = col.row()
177 row.label(text='Please help the community by rating quality:')
179 row = col.row()
180 row.prop(self, 'rating_quality_ui', expand=True, icon_only=True, emboss=False)
181 if self.rating_quality > 0:
182 # row = col.row()
184 row.label(text=f' Thanks{profile_name}!', icon='FUND')
185 # row.label(text=str(self.rating_quality))
186 col.separator()
187 col.separator()
189 row = col.row()
190 row.label(text='Complexity:', icon_value=pcoll['dumbbell'].icon_id)
191 row = col.row()
192 row.label(text=f"How many hours did this {self.asset_type} save you?")
194 if utils.profile_is_validator():
195 row = col.row()
196 row.prop(self, 'rating_work_hours')
198 if self.asset_type in ('model', 'scene'):
199 row = col.row()
201 row.prop(self, 'rating_work_hours_ui', expand=True, icon_only=False, emboss=True)
202 if float(self.rating_work_hours_ui) > 100:
203 utils.label_multiline(col,
204 text=f"\nThat's huge! please be sure to give such rating only to godly {self.asset_type}s.\n",
205 width=500)
206 elif float(self.rating_work_hours_ui) > 18:
207 col.separator()
209 utils.label_multiline(col,
210 text=f"\nThat's a lot! please be sure to give such rating only to amazing {self.asset_type}s.\n",
211 width=500)
214 elif self.asset_type == 'hdr':
215 row = col.row()
216 row.prop(self, 'rating_work_hours_ui_1_10', expand=True, icon_only=False, emboss=True)
217 else:
218 row = col.row()
219 row.prop(self, 'rating_work_hours_ui_1_5', expand=True, icon_only=False, emboss=True)
221 if self.rating_work_hours > 0:
222 row = col.row()
223 row.label(text=f'Thanks{profile_name}, you are amazing!', icon='FUND')
226 class FastRateMenu(Operator, ratings_utils.RatingsProperties):
227 """Rating of the assets , also directly from the asset bar - without need to download assets"""
228 bl_idname = "wm.blenderkit_menu_rating_upload"
229 bl_label = "Ratings"
230 bl_options = {'REGISTER', 'UNDO', 'INTERNAL'}
232 @classmethod
233 def poll(cls, context):
234 scene = bpy.context.scene
235 ui_props = scene.blenderkitUI
236 return True;
238 def draw(self, context):
239 layout = self.layout
240 draw_ratings_menu(self, context, layout)
242 def execute(self, context):
243 scene = bpy.context.scene
244 ui_props = scene.blenderkitUI
245 #get asset id
246 if ui_props.active_index > -1:
247 sr = bpy.context.window_manager['search results']
248 asset_data = dict(sr[ui_props.active_index])
249 self.asset_id = asset_data['id']
250 self.asset_type = asset_data['assetType']
252 if self.asset_id == '':
253 return {'CANCELLED'}
255 wm = context.window_manager
257 self.prefill_ratings()
259 if self.asset_type in ('model', 'scene'):
260 # spawn a wider one for validators for the enum buttons
261 return wm.invoke_popup(self, width=500)
262 else:
263 return wm.invoke_popup(self)
266 def rating_menu_draw(self, context):
267 layout = self.layout
269 ui_props = context.scene.blenderkitUI
270 sr = bpy.context.window_manager['search results orig']
272 asset_search_index = ui_props.active_index
273 if asset_search_index > -1:
274 asset_data = dict(sr['results'][asset_search_index])
276 col = layout.column()
277 layout.label(text='Admin rating Tools:')
278 col.operator_context = 'INVOKE_DEFAULT'
280 op = col.operator('wm.blenderkit_menu_rating_upload', text='Rate')
281 op.asset_id = asset_data['id']
282 op.asset_name = asset_data['name']
283 op.asset_type = asset_data['assetType']
286 def register_ratings():
287 bpy.utils.register_class(UploadRatingOperator)
288 bpy.utils.register_class(FastRateMenu)
289 # bpy.types.OBJECT_MT_blenderkit_asset_menu.append(rating_menu_draw)
292 def unregister_ratings():
293 pass;
294 # bpy.utils.unregister_class(StarRatingOperator)
295 bpy.utils.unregister_class(UploadRatingOperator)
296 bpy.utils.unregister_class(FastRateMenu)