Skip to content
Snippets Groups Projects
ui.py 58.8 KiB
Newer Older
  • Learn to ignore specific revisions
  • Vilem Duha's avatar
    Vilem Duha committed
    1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
                mx = event.mouse_x
                my = event.mouse_y
    
                ui_props.draw_tooltip = True
    
                # only generate tooltip once in a while
                if (
                        event.type == 'LEFTMOUSE' or event.type == 'RIGHTMOUSE') and event.value == 'RELEASE' or event.type == 'ENTER' or ui_props.tooltip == '':
                    ao = bpy.context.active_object
                    if ui_props.asset_type == 'MODEL' and ao != None \
                            or ui_props.asset_type == 'MATERIAL' and ao != None and ao.active_material != None \
                            or ui_props.asset_type == 'BRUSH':
                        export_data, upload_data, eval_path_computing, eval_path_state, eval_path, props = upload.get_upload_data(
                            self,
                            context,
                            ui_props.asset_type)
                        ui_props.tooltip = search.generate_tooltip(upload_data)
    
                return {'PASS_THROUGH'}
    
            # TODO add one more condition here to take less performance.
            r = self.region
            s = bpy.context.scene
            sr = s.get('search results')
    
            # If there aren't any results, we need no interaction(yet)
            if sr is None:
                return {'PASS_THROUGH'}
            if len(sr) - ui_props.scrolloffset < (ui_props.wcount * ui_props.hcount) + 10:
                self.search_more()
            if event.type == 'WHEELUPMOUSE' or event.type == 'WHEELDOWNMOUSE' or event.type == 'TRACKPADPAN':
                # scrolling
                mx = event.mouse_region_x
                my = event.mouse_region_y
    
                if ui_props.dragging and not mouse_in_asset_bar(mx, my):  # and my < r.height - ui_props.bar_height \
                    # and mx > 0 and mx < r.width and my > 0:
                    sprops = bpy.context.scene.blenderkit_models
                    if event.type == 'WHEELUPMOUSE':
                        sprops.offset_rotation_amount += sprops.offset_rotation_step
                    elif event.type == 'WHEELDOWNMOUSE':
                        sprops.offset_rotation_amount -= sprops.offset_rotation_step
    
                    #### TODO - this snapping code below is 3x in this file.... refactor it.
                    ui_props.has_hit, ui_props.snapped_location, ui_props.snapped_normal, ui_props.snapped_rotation, face_index, object, matrix = mouse_raycast(
                        context, mx, my)
    
                    # MODELS can be dragged on scene floor
                    if not ui_props.has_hit and ui_props.asset_type == 'MODEL':
                        ui_props.has_hit, ui_props.snapped_location, ui_props.snapped_normal, ui_props.snapped_rotation, face_index, object, matrix = floor_raycast(
                            context,
                            mx, my)
    
                    return {'RUNNING_MODAL'}
    
                if not mouse_in_asset_bar(mx, my):
                    return {'PASS_THROUGH'}
    
                # note - TRACKPADPAN is unsupported in blender by now.
                # if event.type == 'TRACKPADPAN' :
                #     print(dir(event))
                #     print(event.value, event.oskey, event.)
                if (event.type == 'WHEELDOWNMOUSE') and len(sr) - ui_props.scrolloffset > ui_props.wcount:
                    ui_props.scrolloffset += 1
    
                if event.type == 'WHEELUPMOUSE' and ui_props.scrolloffset > 0:
                    ui_props.scrolloffset -= 1
                return {'RUNNING_MODAL'}
            if event.type == 'MOUSEMOVE':  # Apply
    
                r = self.region
                mx = event.mouse_region_x
                my = event.mouse_region_y
    
                ui_props.mouse_x = mx
                ui_props.mouse_y = my
    
                if ui_props.dragging_rating or ui_props.rating_menu_on:
                    res = interact_rating(r, mx, my, event)
                    if res == True:
                        return {'RUNNING_MODAL'}
    
                if ui_props.drag_init:
                    ui_props.drag_length += 1
                    if ui_props.drag_length > 0:
                        ui_props.dragging = True
                        ui_props.drag_init = False
    
                if not (ui_props.dragging and mouse_in_region(r, mx, my)) and not mouse_in_asset_bar(mx, my):
                    ui_props.active_index = -3
                    ui_props.draw_tooltip = False
                    bpy.context.window.cursor_set("DEFAULT")
                    return {'PASS_THROUGH'}
    
                sr = bpy.context.scene['search results']
    
                if not ui_props.dragging:
                    bpy.context.window.cursor_set("DEFAULT")
    
                    if sr != None and ui_props.wcount * ui_props.hcount > len(sr) and ui_props.scrolloffset > 0:
                        ui_props.scrolloffset = 0
    
                    asset_search_index = get_asset_under_mouse(mx, my)
                    ui_props.active_index = asset_search_index
                    if asset_search_index > -1:
    
                        asset_data = sr[asset_search_index]
                        ui_props.draw_tooltip = True
    
                        ui_props.tooltip = asset_data['tooltip']
                    else:
                        ui_props.draw_tooltip = False
    
                    if mx > ui_props.bar_x + ui_props.bar_width - 50 and len(sr) - ui_props.scrolloffset > (
                            ui_props.wcount * ui_props.hcount):
                        ui_props.active_index = -1
                        return {'RUNNING_MODAL'}
                    if mx < ui_props.bar_x + 50 and ui_props.scrolloffset > 0:
                        ui_props.active_index = -2
                        return {'RUNNING_MODAL'}
    
                else:
                    result = False
                    if ui_props.dragging and not mouse_in_asset_bar(mx, my) and mouse_in_region(r, mx, my):
                        ui_props.has_hit, ui_props.snapped_location, ui_props.snapped_normal, ui_props.snapped_rotation, face_index, object, matrix = mouse_raycast(
                            context, mx, my)
                        # MODELS can be dragged on scene floor
                        if not ui_props.has_hit and ui_props.asset_type == 'MODEL':
                            ui_props.has_hit, ui_props.snapped_location, ui_props.snapped_normal, ui_props.snapped_rotation, face_index, object, matrix = floor_raycast(
                                context,
                                mx, my)
                    if ui_props.has_hit and ui_props.asset_type == 'MODEL':
                        # this condition is here to fix a bug for a scene submitted by a user, so this situation shouldn't
                        # happen anymore, but there might exists scenes which have this problem for some reason.
                        if ui_props.active_index<len(sr) and ui_props.active_index>-1:
                            ui_props.draw_snapped_bounds = True
                            active_mod = sr[ui_props.active_index]
                            ui_props.snapped_bbox_min = Vector(active_mod['bbox_min'])
                            ui_props.snapped_bbox_max = Vector(active_mod['bbox_max'])
    
                    else:
                        ui_props.draw_snapped_bounds = False
                        ui_props.draw_drag_image = True
                return {'RUNNING_MODAL'}
    
            if event.type == 'LEFTMOUSE':
    
                r = self.region
                mx = event.mouse_x - r.x
                my = event.mouse_y - r.y
    
                ui_props = context.scene.blenderkitUI
                if event.value == 'PRESS' and ui_props.active_index > -1:
                    if ui_props.asset_type == 'MODEL' or ui_props.asset_type == 'MATERIAL':
                        ui_props.drag_init = True
                        bpy.context.window.cursor_set("NONE")
                        ui_props.draw_tooltip = False
                        ui_props.drag_length = 0
    
                if ui_props.rating_on:
                    res = interact_rating(r, mx, my, event)
                    if res:
                        return {'RUNNING_MODAL'}
    
                if not ui_props.dragging and not mouse_in_asset_bar(mx, my):
                    return {'PASS_THROUGH'}
    
                # this can happen by switching result asset types - length of search result changes
                if ui_props.scrolloffset > 0 and (ui_props.wcount * ui_props.hcount) > len(sr) - ui_props.scrolloffset:
                    ui_props.scrolloffset = len(sr) - (ui_props.wcount * ui_props.hcount)
    
                if event.value == 'RELEASE':  # Confirm
                    ui_props.drag_init = False
    
                    # scroll by a whole page
                    if mx > ui_props.bar_x + ui_props.bar_width - 50 and len(
                            sr) - ui_props.scrolloffset > ui_props.wcount * ui_props.hcount:
                        ui_props.scrolloffset = min(
                            ui_props.scrolloffset + (ui_props.wcount * ui_props.hcount),
                            len(sr) - ui_props.wcount * ui_props.hcount)
                        return {'RUNNING_MODAL'}
                    if mx < ui_props.bar_x + 50 and ui_props.scrolloffset > 0:
                        ui_props.scrolloffset = max(0, ui_props.scrolloffset - ui_props.wcount * ui_props.hcount)
                        return {'RUNNING_MODAL'}
    
                    # Drag-drop interaction
                    if ui_props.dragging and mouse_in_region(r, mx, my):
                        asset_search_index = ui_props.active_index
                        # raycast here
                        ui_props.active_index = -3
    
                        if ui_props.asset_type == 'MODEL':
    
                            ui_props.has_hit, ui_props.snapped_location, ui_props.snapped_normal, ui_props.snapped_rotation, face_index, object, matrix = mouse_raycast(
                                context, mx, my)
    
                            # MODELS can be dragged on scene floor
                            if not ui_props.has_hit and ui_props.asset_type == 'MODEL':
                                ui_props.has_hit, ui_props.snapped_location, ui_props.snapped_normal, ui_props.snapped_rotation, face_index, object, matrix = floor_raycast(
                                    context,
                                    mx, my)
    
                            if not ui_props.has_hit:
                                return {'RUNNING_MODAL'}
    
                            target_object = ''
                            if object is not None:
                                target_object = object.name
                            target_slot = ''
    
                        if ui_props.asset_type == 'MATERIAL':
                            ui_props.has_hit, ui_props.snapped_location, ui_props.snapped_normal, ui_props.snapped_rotation, face_index, object, matrix = mouse_raycast(
                                context, mx, my)
    
                            if not ui_props.has_hit:
                                # this is last attempt to get object under mouse - for curves and other objects than mesh.
                                ui_props.dragging = False
                                sel = utils.selection_get()
                                bpy.ops.view3d.select(location=(event.mouse_region_x, event.mouse_region_y))
                                sel1 = utils.selection_get()
                                if sel[0] != sel1[0] and sel1[0].type != 'MESH':
                                    object = sel1[0]
                                    target_slot = sel1[0].active_material_index
                                    ui_props.has_hit = True
                                utils.selection_set(sel)
    
                            if not ui_props.has_hit:
                                return {'RUNNING_MODAL'}
    
                            else:
                                # first, test if object can have material applied.
                                if object is not None and not object.is_library_indirect:
                                    target_object = object.name
                                    # create final mesh to extract correct material slot
                                    temp_mesh = object.to_mesh(depsgraph=bpy.context.depsgraph, apply_modifiers=True,
                                                               calc_undeformed=False)
                                    target_slot = temp_mesh.polygons[face_index].material_index
                                else:
                                    self.report({'WARNING'}, "Invalid or library object as input:")
                                    target_object = ''
                                    target_slot = ''
    
                    # Click interaction
                    else:
                        asset_search_index = get_asset_under_mouse(mx, my)
    
                        if ui_props.asset_type in ('MATERIAL',
                                                   'MODEL'):  # this was meant for particles, commenting for now or ui_props.asset_type == 'MODEL':
                            ao = bpy.context.active_object
                            if ao != None and not ao.is_library_indirect:
                                target_object = bpy.context.active_object.name
                                target_slot = bpy.context.active_object.active_material_index
                            else:
                                target_object = ''
                                target_slot = ''
                    # FIRST START SEARCH
    
                    if asset_search_index == -3:
                        return {'RUNNING_MODAL'}
                    if asset_search_index > -3:
                        if ui_props.asset_type == 'MATERIAL':
                            if target_object != '':
                                # position is for downloader:
                                loc = ui_props.snapped_location
                                rotation = (0, 0, 0)
    
                                asset_data = sr[asset_search_index]
                                utils.automap(target_object, target_slot=target_slot,
                                              tex_size=asset_data.get('texture_size_meters', 1.0))
                                bpy.ops.scene.blenderkit_download(True,
                                                                  asset_type=ui_props.asset_type,
                                                                  asset_index=asset_search_index,
                                                                  model_location=loc,
                                                                  model_rotation=rotation,
                                                                  target_object=target_object,
                                                                  material_target_slot=target_slot)
    
    
                        elif ui_props.asset_type == 'MODEL':
                            if ui_props.has_hit and ui_props.dragging:
                                loc = ui_props.snapped_location
                                rotation = ui_props.snapped_rotation
                            else:
                                loc = s.cursor.location
                                rotation = s.cursor.rotation_euler
    
                            bpy.ops.scene.blenderkit_download(True,
                                                              asset_type=ui_props.asset_type,
                                                              asset_index=asset_search_index,
                                                              model_location=loc,
                                                              model_rotation=rotation,
                                                              target_object=target_object)
    
                        else:
                            bpy.ops.scene.blenderkit_download(asset_type=ui_props.asset_type,
                                                              asset_index=asset_search_index)
    
                        ui_props.dragging = False
                        return {'RUNNING_MODAL'}
                else:
                    return {'RUNNING_MODAL'}
    
            if event.type == 'X' and ui_props.active_index != -3:
                sr = bpy.context.scene['search results']
                asset_data = sr[ui_props.active_index]
                print(asset_data['name'])
                print('delete')
                paths.delete_asset_debug(asset_data)
                asset_data['downloaded'] = 0
                return {'RUNNING_MODAL'}
            return {'PASS_THROUGH'}
    
        def invoke(self, context, event):
            # FIRST START SEARCH
            ui_props = context.scene.blenderkitUI
    
            if self.do_search:
                # we erase search keywords for cateogry search now, since these combinations usually return nothing now.
                # when the db gets bigger, this can be deleted.
                if self.category != '':
                    sprops = utils.get_search_props()
                    sprops.search_keywords = ''
                search.search(category=self.category, free_only=self.free_only)
    
            if ui_props.assetbar_on:
                # we don't want to run the assetbar many times, that's why it has a switch on/off behaviour,
                # unless being called with 'keep_running' prop.
                if not self.keep_running:
                    # this sends message to the originally running operator, so it quits, and then it ends this one too.
                    # If it initiated a search, the search will finish in a thread. The switch off procedure is run
                    # by the 'original' operator, since if we get here, it means
                    # same operator is allready running.
                    ui_props.turn_off = True
                    # if there was an error, we need to turn off these props so we can restart after 2 clicks
                    ui_props.assetbar_on = False
    
                else:
                    pass
                return {'FINISHED'}
    
            ui_props.dragging = False #only for cases where assetbar ended with an error.
            ui_props.assetbar_on = True
            ui_props.turn_off = False
    
            sr = bpy.context.scene.get('search results')
            if sr is None:
                bpy.context.scene['search results'] = []
    
            if context.area.type == 'VIEW_3D':
                # the arguments we pass the the callback
                args = (self, context)
                self.area = context.area
                self.scene = bpy.context.scene
    
                for r in self.area.regions:
                    if r.type == 'WINDOW':
                        self.region = r
    
                update_ui_size(self.area, self.region)
    
                self._handle_2d = bpy.types.SpaceView3D.draw_handler_add(draw_callback_2d, args, 'WINDOW', 'POST_PIXEL')
                self._handle_3d = bpy.types.SpaceView3D.draw_handler_add(draw_callback_3d, args, 'WINDOW', 'POST_VIEW')
    
                context.window_manager.modal_handler_add(self)
                ui_props.assetbar_on = True
                return {'RUNNING_MODAL'}
            else:
    
                self.report({'WARNING'}, "View3D not found, cannot run operator")
                return {'CANCELLED'}
    
        def execute(self, context):
            return {'RUNNING_MODAL'}
    
    
    classess = (
        AssetBarOperator,
    
    )
    
    # store keymaps here to access after registration
    addon_keymaps = []
    
    
    def register_ui():
        for c in classess:
            bpy.utils.register_class(c)
    
        args = (None, bpy.context)
        bpy.types.SpaceView3D.draw_handler_add(draw_callback_2d_progress, args, 'WINDOW', 'POST_PIXEL')
        bpy.types.SpaceView3D.draw_handler_add(draw_callback_3d_progress, args, 'WINDOW', 'POST_VIEW')
    
        wm = bpy.context.window_manager
    
        # spaces solved by registering shortcut to Window. Couldn't register object mode before somehow.
        if not wm.keyconfigs.addon:
            return
        km = wm.keyconfigs.addon.keymaps.new(name="Window", space_type='EMPTY')
        kmi = km.keymap_items.new(AssetBarOperator.bl_idname, 'SEMI_COLON', 'PRESS', ctrl=False, shift=False)
        kmi.properties.keep_running = False
        kmi.properties.do_search = False
    
        addon_keymaps.append(km)
    
    
    def unregister_ui():
        for c in classess:
            bpy.utils.unregister_class(c)
    
        args = (None, bpy.context)
    
        try:
            bpy.types.SpaceView3D.draw_handler_remove(draw_callback_2d_progress, args, 'WINDOW', 'POST_PIXEL')
            bpy.types.SpaceView3D.draw_handler_remove(draw_callback_3d_progress, args, 'WINDOW', 'POST_VIEW')
        except:
            print('handlers allready removed')
        wm = bpy.context.window_manager
        if not wm.keyconfigs.addon:
            return
    
        for km in addon_keymaps:
            wm.keyconfigs.addon.keymaps.remove(km)
        del addon_keymaps[:]