Skip to content
Snippets Groups Projects
ui.py 91 KiB
Newer Older
  • Learn to ignore specific revisions
  • Vilem Duha's avatar
    Vilem Duha committed
                ui.bar_x = r.width * reg_multiplier + ui.margin + ui.bar_x_offset * ui_scale
            elif r.type == 'UI':
                ui.bar_end = r.width * reg_multiplier + 100 * ui_scale
    
        ui.bar_width = region.width - ui.bar_x - ui.bar_end
        ui.wcount = math.floor(
            (ui.bar_width - 2 * ui.drawoffset) / (ui.thumb_size + ui.margin))
    
    
        search_results = bpy.context.window_manager.get('search results')
    
        if search_results != None and ui.wcount > 0:
    
    Vilem Duha's avatar
    Vilem Duha committed
            ui.hcount = min(user_preferences.max_assetbar_rows, math.ceil(len(search_results) / ui.wcount))
        else:
            ui.hcount = 1
        ui.bar_height = (ui.thumb_size + ui.margin) * ui.hcount + ui.margin
        ui.bar_y = region.height - ui.bar_y_offset * ui_scale
        if ui.down_up == 'UPLOAD':
    
            ui.reports_y = ui.bar_y - 600
    
    Vilem Duha's avatar
    Vilem Duha committed
            ui.reports_x = ui.bar_x
        else:
    
            ui.reports_y = ui.bar_y - ui.bar_height - 100
    
    Vilem Duha's avatar
    Vilem Duha committed
            ui.reports_x = ui.bar_x
    
        ui.rating_x = ui.bar_x
        ui.rating_y = ui.bar_y - ui.bar_height
    
    
    class ParticlesDropDialog(bpy.types.Operator):
        """Tooltip"""
        bl_idname = "object.blenderkit_particles_drop"
        bl_label = "BlenderKit particle plants object drop"
        bl_options = {'REGISTER', 'INTERNAL'}
    
        asset_search_index: IntProperty(name="Asset index",
                                        description="Index of the asset in asset bar",
                                        default=0,
                                        )
    
        model_location: FloatVectorProperty(name="Location",
                                            default=(0, 0, 0))
    
        model_rotation: FloatVectorProperty(name="Rotation",
                                            default=(0, 0, 0),
                                            subtype='QUATERNION')
    
        target_object: StringProperty(
            name="Target object",
            description="The object to which the particles will get applied",
            default="", options={'SKIP_SAVE'})
    
        @classmethod
        def poll(cls, context):
            return True
    
        def draw(self, context):
            layout = self.layout
            message = 'This asset is a particle setup. BlenderKit can apply particles to the active/drag-drop object.' \
                      'The number of particles is caluclated automatically, but if there are 2 many particles,' \
                      ' BlenderKit can do the following steps to make sure Blender continues to run:' \
                      '\n1.Switch to bounding box view of the particles.' \
                      '\n2.Turn down number of particles that are shown in the view.' \
                      '\n3.Hide the particle system completely from the 3D view.' \
                      "as a result of this, it's possible you'll see the particle setup only in render view or " \
                      "rendered images. You should still be careful and test particle systems on smaller objects first."
            utils.label_multiline(layout, text=message, width=400)
    
        def execute(self, context):
            bpy.ops.scene.blenderkit_download(True,
                                              # asset_type=ui_props.asset_type,
                                              asset_index=self.asset_search_index,
                                              model_location=self.model_rotation,
                                              model_rotation=self.model_rotation,
                                              target_object=self.target_object)
            return {'FINISHED'}
    
        def invoke(self, context, event):
            wm = context.window_manager
            return wm.invoke_props_dialog(self, width=400)
    
    
    # class MaterialDropDialog(bpy.types.Operator):
    #     """Tooltip"""
    #     bl_idname = "object.blenderkit_material_drop"
    #     bl_label = "BlenderKit material drop on linked objects"
    #     bl_options = {'REGISTER', 'INTERNAL'}
    #
    #     asset_search_index: IntProperty(name="Asset index",
    #                                     description="Index of the asset in asset bar",
    #                                     default=0,
    #                                     )
    #
    #     model_location: FloatVectorProperty(name="Location",
    #                                         default=(0, 0, 0))
    #
    #     model_rotation: FloatVectorProperty(name="Rotation",
    #                                         default=(0, 0, 0),
    #                                         subtype='QUATERNION')
    #
    #     target_object: StringProperty(
    #         name="Target object",
    #         description="The object to which the particles will get applied",
    #         default="", options={'SKIP_SAVE'})
    #
    #     target_material_slot: IntProperty(name="Target material slot",
    #                                     description="Index of the material on the object to be changed",
    #                                     default=0,
    #                                     )
    #
    #     @classmethod
    #     def poll(cls, context):
    #         return True
    #
    #     def draw(self, context):
    #         layout = self.layout
    #         message = "This asset is linked to the scene from an external file and cannot have material appended." \
    #                   " Do you want to bring it into Blender Scene?"
    #         utils.label_multiline(layout, text=message, width=400)
    #
    #     def execute(self, context):
    #         for c in bpy.data.collections:
    #             for o in c.objects:
    #                 if o.name != self.target_object:
    #                     continue;
    #                 for empty in bpy.context.visible_objects:
    #                     if not(empty.instance_type == 'COLLECTION' and empty.instance_collection == c):
    #                         continue;
    #                     utils.activate(empty)
    #                     break;
    #         bpy.ops.object.blenderkit_bring_to_scene()
    #         bpy.ops.scene.blenderkit_download(True,
    #                                           # asset_type=ui_props.asset_type,
    #                                           asset_index=self.asset_search_index,
    #                                           model_location=self.model_rotation,
    #                                           model_rotation=self.model_rotation,
    #                                           target_object=self.target_object,
    #                                           material_target_slot = self.target_slot)
    #         return {'FINISHED'}
    #
    #     def invoke(self, context, event):
    #         wm = context.window_manager
    #         return wm.invoke_props_dialog(self, width=400)
    
    Vilem Duha's avatar
    Vilem Duha committed
    class AssetBarOperator(bpy.types.Operator):
        '''runs search and displays the asset bar at the same time'''
        bl_idname = "view3d.blenderkit_asset_bar"
        bl_label = "BlenderKit Asset Bar UI"
    
    Vilém Duha's avatar
    Vilém Duha committed
        bl_options = {'REGISTER', 'UNDO', 'INTERNAL'}
    
    Vilem Duha's avatar
    Vilem Duha committed
    
        do_search: BoolProperty(name="Run Search", description='', default=True, options={'SKIP_SAVE'})
        keep_running: BoolProperty(name="Keep Running", description='', default=True, options={'SKIP_SAVE'})
    
        free_only: BoolProperty(name="Free first", description='', default=False, options={'SKIP_SAVE'})
    
    Vilem Duha's avatar
    Vilem Duha committed
    
        category: StringProperty(
            name="Category",
            description="search only subtree of this category",
            default="", options={'SKIP_SAVE'})
    
    
        tooltip: bpy.props.StringProperty(default='runs search and displays the asset bar at the same time')
    
    
        @classmethod
        def description(cls, context, properties):
            return properties.tooltip
    
    
    Vilem Duha's avatar
    Vilem Duha committed
        def search_more(self):
    
            sro = bpy.context.window_manager.get('search results orig')
    
    Vilem Duha's avatar
    Vilem Duha committed
            if sro is not None and sro.get('next') is not None:
    
    Vilem Duha's avatar
    Vilem Duha committed
                search.search(get_next=True)
    
        def exit_modal(self):
            try:
                bpy.types.SpaceView3D.draw_handler_remove(self._handle_2d, 'WINDOW')
                bpy.types.SpaceView3D.draw_handler_remove(self._handle_3d, 'WINDOW')
            except:
                pass;
            ui_props = bpy.context.scene.blenderkitUI
    
            ui_props.dragging = False
            ui_props.tooltip = ''
            ui_props.active_index = -3
            ui_props.draw_drag_image = False
            ui_props.draw_snapped_bounds = False
            ui_props.has_hit = False
            ui_props.assetbar_on = False
    
        def modal(self, context, event):
    
    Vilem Duha's avatar
    Vilem Duha committed
            # This is for case of closing the area or changing type:
            ui_props = context.scene.blenderkitUI
            user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
    
            areas = []
    
    
    Vilém Duha's avatar
    Vilém Duha committed
            # timers testing - seems timers might be causing crashes. testing it this way now.
    
            if not user_preferences.use_timers:
    
                search.search_timer()
                download.download_timer()
    
    Vilém Duha's avatar
    Vilém Duha committed
                tasks_queue.queue_worker()
                bg_blender.bg_update()
    
    Vilem Duha's avatar
    Vilem Duha committed
            if bpy.context.scene != self.scene:
                self.exit_modal()
                return {'CANCELLED'}
    
    
            for w in context.window_manager.windows:
                areas.extend(w.screen.areas)
    
            if self.area not in areas or self.area.type != 'VIEW_3D' or self.has_quad_views != (
                    len(self.area.spaces[0].region_quadviews) > 0):
                # print('search areas')   bpy.context.area.spaces[0].region_quadviews
    
    Vilem Duha's avatar
    Vilem Duha committed
                # stopping here model by now - because of:
                #   switching layouts or maximizing area now fails to assign new area throwing the bug
                #   internal error: modal gizmo-map handler has invalid area
                self.exit_modal()
                return {'CANCELLED'}
    
                newarea = None
                for a in context.window.screen.areas:
                    if a.type == 'VIEW_3D':
                        self.area = a
                        for r in a.regions:
                            if r.type == 'WINDOW':
                                self.region = r
                        newarea = a
    
    Vilem Duha's avatar
    Vilem Duha committed
                        # context.area = a
    
                # we check again and quit if things weren't fixed this way.
                if newarea == None:
                    self.exit_modal()
                    return {'CANCELLED'}
    
            update_ui_size(self.area, self.region)
    
            # this was here to check if sculpt stroke is running, but obviously that didn't help,
            #  since the RELEASE event is cought by operator and thus there is no way to detect a stroke has ended...
            if bpy.context.mode in ('SCULPT', 'PAINT_TEXTURE'):
                if event.type == 'MOUSEMOVE':  # ASSUME THAT SCULPT OPERATOR ACTUALLY STEALS THESE EVENTS,
                    # SO WHEN THERE ARE SOME WE CAN APPEND BRUSH...
                    bpy.context.window_manager['appendable'] = True
                if event.type == 'LEFTMOUSE':
                    if event.value == 'PRESS':
                        bpy.context.window_manager['appendable'] = False
    
            self.area.tag_redraw()
            s = context.scene
    
            if ui_props.turn_off:
                ui_props.turn_off = False
                self.exit_modal()
                ui_props.draw_tooltip = False
                return {'CANCELLED'}
    
    
            if context.region != self.region:
                # print(time.time(), 'pass through because of region')
                # print(context.region.type, self.region.type)
                return {'PASS_THROUGH'}
    
    
    Vilem Duha's avatar
    Vilem Duha committed
            if ui_props.down_up == 'UPLOAD':
    
                ui_props.mouse_x = 0
                ui_props.mouse_y = self.region.height
    
                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' and utils.get_active_brush() is not None \
                            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:
    
                            # print(upload_data)
    
                            ui_props.tooltip = upload_data['displayName']  # search.generate_tooltip(upload_data)
    
    Vilem Duha's avatar
    Vilem Duha committed
    
                return {'PASS_THROUGH'}
    
            # TODO add one more condition here to take less performance.
            r = self.region
            s = bpy.context.scene
    
            sr = bpy.context.window_manager.get('search results')
            search_results_orig = bpy.context.window_manager.get('search results orig')
    
    Vilem Duha's avatar
    Vilem Duha committed
            # 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.hcount):
                    if ui_props.hcount > 1:
                        ui_props.scrolloffset += ui_props.wcount
                    else:
                        ui_props.scrolloffset += 1
                    if len(sr) - ui_props.scrolloffset < (ui_props.wcount * ui_props.hcount):
                        ui_props.scrolloffset = len(sr) - (ui_props.wcount * ui_props.hcount)
    
    Vilem Duha's avatar
    Vilem Duha committed
    
                if event.type == 'WHEELUPMOUSE' and ui_props.scrolloffset > 0:
    
                    if ui_props.hcount > 1:
                        ui_props.scrolloffset -= ui_props.wcount
                    else:
                        ui_props.scrolloffset -= 1
                    if ui_props.scrolloffset < 0:
                        ui_props.scrolloffset = 0
    
    
    Vilem Duha's avatar
    Vilem Duha committed
                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'}
    
    Vilem Duha's avatar
    Vilem Duha committed
    
                if ui_props.drag_init:
    
                    ui_props.drag_length = abs(self.drag_start_x - mx) + abs(self.drag_start_y - my)
                    if ui_props.drag_length > 5:
    
    Vilem Duha's avatar
    Vilem Duha committed
                        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.dragging = False
                    ui_props.has_hit = False
    
    Vilem Duha's avatar
    Vilem Duha committed
                    ui_props.active_index = -3
    
                    ui_props.draw_drag_image = False
                    ui_props.draw_snapped_bounds = False
    
    Vilem Duha's avatar
    Vilem Duha committed
                    ui_props.draw_tooltip = False
                    bpy.context.window.cursor_set("DEFAULT")
                    return {'PASS_THROUGH'}
    
    
                sr = bpy.context.window_manager['search results']
    
    Vilem Duha's avatar
    Vilem Duha committed
    
                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']
    
                        # bpy.ops.wm.call_menu(name='OBJECT_MT_blenderkit_asset_menu')
    
    Vilém Duha's avatar
    Vilém Duha committed
    
    
    Vilem Duha's avatar
    Vilem Duha committed
                    else:
                        ui_props.draw_tooltip = False
    
    
                    if mx > ui_props.bar_x + ui_props.bar_width - 50 and search_results_orig[
                        'count'] - ui_props.scrolloffset > (
                            ui_props.wcount * ui_props.hcount) + 1:
    
    Vilem Duha's avatar
    Vilem Duha committed
                        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 mouse_in_region(r, mx, my):
    
    Vilem Duha's avatar
    Vilem Duha committed
                        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:
    
    Vilem Duha's avatar
    Vilem Duha committed
                            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 == 'RIGHTMOUSE':
                mx = event.mouse_x - r.x
                my = event.mouse_y - r.y
    
    
                if event.value == 'PRESS' and mouse_in_asset_bar(mx, my) and ui_props.active_index > -1:
    
                    # context.window.cursor_warp(event.mouse_x - 300, event.mouse_y - 10);
    
    
                    bpy.ops.wm.blenderkit_asset_popup('INVOKE_DEFAULT')
    
                    # context.window.cursor_warp(event.mouse_x, event.mouse_y);
    
    
                    # bpy.ops.wm.call_menu(name='OBJECT_MT_blenderkit_asset_menu')
    
                    return {'RUNNING_MODAL'}
    
    
    Vilem Duha's avatar
    Vilem Duha committed
            if event.type == 'LEFTMOUSE':
    
                r = self.region
    
                mx = event.mouse_region_x
                my = event.mouse_region_y
    
    Vilem Duha's avatar
    Vilem Duha committed
    
                ui_props = context.scene.blenderkitUI
                if event.value == 'PRESS' and ui_props.active_index > -1:
    
                    # start dragging models and materials
    
    Vilem Duha's avatar
    Vilem Duha committed
                    if ui_props.asset_type == 'MODEL' or ui_props.asset_type == 'MATERIAL':
    
                        # check if asset is locked and let the user know in that case
                        asset_search_index = ui_props.active_index
                        asset_data = sr[asset_search_index]
    
                        if not asset_data.get('canDownload'):
    
                            message = "Let's support asset creators and Open source."
    
                            link_text = 'Unlock the asset.'
    
                            url = paths.get_bkit_url() + '/get-blenderkit/' + asset_data['id'] + '/?from_addon=True'
    
                            bpy.ops.wm.blenderkit_url_dialog('INVOKE_REGION_WIN', url=url, message=message,
                                                             link_text=link_text)
                            return {'RUNNING_MODAL'}
                        # go on with drag init
    
    Vilem Duha's avatar
    Vilem Duha committed
                        ui_props.drag_init = True
                        bpy.context.window.cursor_set("NONE")
                        ui_props.draw_tooltip = False
                        ui_props.drag_length = 0
    
                        self.drag_start_x = mx
                        self.drag_start_y = my
    
    Vilem Duha's avatar
    Vilem Duha committed
    
                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):  # and ui_props.drag_length>10:
    
    Vilem Duha's avatar
    Vilem Duha committed
                        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.
    
                                # TODO add other types here if droppable.
    
                                if object is not None and not object.is_library_indirect and object.type in utils.supported_material_drag:
    
    Vilem Duha's avatar
    Vilem Duha committed
                                    target_object = object.name
                                    # create final mesh to extract correct material slot
    
                                    depsgraph = bpy.context.evaluated_depsgraph_get()
    
                                    object_eval = object.evaluated_get(depsgraph)
    
                                    if object.type =='MESH':
                                        temp_mesh = object_eval.to_mesh()
                                        target_slot = temp_mesh.polygons[face_index].material_index
                                        object_eval.to_mesh_clear()
                                    else:
                                        ui_props.snapped_location = object.location
                                        target_slot = 0
    
    Vilem Duha's avatar
    Vilem Duha committed
                                else:
    
                                    if object.is_library_indirect:
                                        ui_panels.ui_message(title='This object is linked from outer file',
                                                             message="Please select the model,"
                                                                     "go to the 'Selected Model' panel "
                                                                     "in BlenderKit and hit 'Bring to Scene' first.")
    
                                    print(object.type)
                                    if object.type not in utils.supported_material_drag:
    
                                        if object.type in utils.supported_material_click:
                                            ui_panels.ui_message(title='Unsupported object type',
                                                                 message=f"Use click interaction for {object.type.lower()} object.")
                                        else:
                                            ui_panels.ui_message(title='Unsupported object type',
                                                                 message=f"Can't assign materials to {object.type.lower()} object.")
    
    Vilem Duha's avatar
    Vilem Duha committed
                                    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
    
                            supported_material_click = ('MESH', 'CURVE', 'META', 'FONT', 'SURFACE', 'VOLUME', 'GPENCIL')
                            if ao != None and not ao.is_library_indirect and ao.type in supported_material_click:
    
    Vilem Duha's avatar
    Vilem Duha committed
                                target_object = bpy.context.active_object.name
                                target_slot = bpy.context.active_object.active_material_index
    
                                # change snapped location for placing material downloader.
                                ui_props.snapped_location = bpy.context.active_object.location
    
    Vilem Duha's avatar
    Vilem Duha committed
                            else:
    
                                if ao != None and ui_props.asset_type == 'MATERIAL' and ao.type not in supported_material_click:
                                    ui_panels.ui_message(title='Unsupported object type',
    
                                                         message=f"Can't assign materials to {ao.type.lower()} object.")
    
    Vilem Duha's avatar
    Vilem Duha committed
                                target_object = ''
                                target_slot = ''
                    # FIRST START SEARCH
    
                    if asset_search_index == -3:
                        return {'RUNNING_MODAL'}
    
    Vilem Duha's avatar
    Vilem Duha committed
                    if asset_search_index > -3:
    
                        asset_data = sr[asset_search_index]
    
    
                        # picking of assets and using them
    
    Vilem Duha's avatar
    Vilem Duha committed
                        if ui_props.asset_type == 'MATERIAL':
                            if target_object != '':
                                # position is for downloader:
                                loc = ui_props.snapped_location
                                rotation = (0, 0, 0)
    
                                utils.automap(target_object, target_slot=target_slot,
                                              tex_size=asset_data.get('texture_size_meters', 1.0))
                                bpy.ops.scene.blenderkit_download(True,
    
    Vilém Duha's avatar
    Vilém Duha committed
                                                                  # asset_type=ui_props.asset_type,
    
    Vilem Duha's avatar
    Vilem Duha committed
                                                                  asset_index=asset_search_index,
                                                                  model_location=loc,
                                                                  model_rotation=rotation,
                                                                  target_object=target_object,
    
                                                                  material_target_slot=target_slot,
                                                                  )
    
    Vilem Duha's avatar
    Vilem Duha committed
    
    
                        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
    
    
                            if 'particle_plants' in asset_data['tags']:
                                bpy.ops.object.blenderkit_particles_drop("INVOKE_DEFAULT",
                                                                         asset_search_index=asset_search_index,
                                                                         model_location=loc,
                                                                         model_rotation=rotation,
                                                                         target_object=target_object)
                            else:
                                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)
    
                        elif ui_props.asset_type == 'HDR':
                            bpy.ops.scene.blenderkit_download('INVOKE_DEFAULT',
                                                              asset_index=asset_search_index,
                                                              # replace_resolution=True,
                                                              invoke_resolution=True,
    
                                                              max_resolution=asset_data.get('max_resolution', 0)
                                                              )
    
                        elif ui_props.asset_type == 'SCENE':
                            bpy.ops.scene.blenderkit_download('INVOKE_DEFAULT',
                                                              asset_index=asset_search_index,
                                                              # replace_resolution=True,
                                                              invoke_resolution=False,
                                                              invoke_scene_settings=True,
                                                              max_resolution=asset_data.get('max_resolution', 0)
                                                              )
    
    Vilem Duha's avatar
    Vilem Duha committed
                        else:
    
                            bpy.ops.scene.blenderkit_download(  # asset_type=ui_props.asset_type,
    
                                asset_index=asset_search_index,
                            )
    
    Vilem Duha's avatar
    Vilem Duha committed
    
                        ui_props.dragging = False
                        return {'RUNNING_MODAL'}
                else:
                    return {'RUNNING_MODAL'}
    
    
    Vilém Duha's avatar
    Vilém Duha committed
            if event.type == 'W' and ui_props.active_index > -1:
    
                sr = bpy.context.window_manager['search results']
    
    Vilem Duha's avatar
    Vilem Duha committed
                asset_data = sr[ui_props.active_index]
    
                a = bpy.context.window_manager['bkit authors'].get(asset_data['author']['id'])
    
    Vilem Duha's avatar
    Vilem Duha committed
                if a is not None:
    
    Vilem Duha's avatar
    Vilem Duha committed
                    if a.get('aboutMeUrl') is not None:
                        bpy.ops.wm.url_open(url=a['aboutMeUrl'])
    
    Vilém Duha's avatar
    Vilém Duha committed
            if event.type == 'A' and ui_props.active_index > -1:
    
                sr = bpy.context.window_manager['search results']
    
                asset_data = sr[ui_props.active_index]
    
                    sprops = utils.get_search_props()
                    sprops.search_keywords = ''
    
                    sprops.search_verification_status = 'ALL'
    
                    utils.p('author:', a)
    
                    search.search(author_id=a)
    
                return {'RUNNING_MODAL'}
    
    Vilém Duha's avatar
    Vilém Duha committed
            if event.type == 'X' and ui_props.active_index > -1:
    
                # delete downloaded files for this asset
    
                sr = bpy.context.window_manager['search results']
    
    Vilem Duha's avatar
    Vilem Duha committed
                asset_data = sr[ui_props.active_index]
    
                bk_logger.info('delete asset from local drive:' + asset_data['name'])
    
    Vilem Duha's avatar
    Vilem Duha committed
                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
    
            sr = bpy.context.window_manager.get('search results')
    
            if self.do_search:
    
    Vilem Duha's avatar
    Vilem Duha committed
                # 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)
    
    Vilem Duha's avatar
    Vilem Duha committed
    
            if ui_props.assetbar_on:
    
                # we don't want to run the assetbar more than once, that's why it has a switch on/off behaviour,
    
    Vilem Duha's avatar
    Vilem Duha committed
                # 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 already running.
    
    Vilem Duha's avatar
    Vilem Duha committed
                    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.
    
    Vilem Duha's avatar
    Vilem Duha committed
            ui_props.assetbar_on = True
            ui_props.turn_off = False
    
    
            if sr is None:
                bpy.context.window_manager['search results'] = []
    
            if context.area.type != 'VIEW_3D':
                self.report({'WARNING'}, "View3D not found, cannot run operator")
                return {'CANCELLED'}
    
            # the arguments we pass the the callback
            args = (self, context)
    
            self.window = context.window
            self.area = context.area
            self.scene = bpy.context.scene
    
            self.has_quad_views = len(bpy.context.area.spaces[0].region_quadviews) > 0
    
            for r in self.area.regions:
                if r.type == 'WINDOW':
                    self.region = r
    
    
            global active_window_pointer, active_area_pointer, active_region_pointer
            active_window_pointer = self.window.as_pointer()
            active_area_pointer = self.area.as_pointer()
            active_region_pointer = self.region.as_pointer()
    
    
            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
    
    Vilém Duha's avatar
    Vilém Duha committed
    
    
            # in an exceptional case these were accessed before  drag start.
    
    Vilém Duha's avatar
    Vilém Duha committed
            self.drag_start_x = 0
            self.drag_start_y = 0
    
    
            return {'RUNNING_MODAL'}
    
    Vilem Duha's avatar
    Vilem Duha committed
    
        def execute(self, context):
            return {'RUNNING_MODAL'}
    
    
    
    Vilém Duha's avatar
    Vilém Duha committed
    class TransferBlenderkitData(bpy.types.Operator):
        """Regenerate cobweb"""
        bl_idname = "object.blenderkit_data_trasnfer"
        bl_label = "Transfer BlenderKit data"
    
        bl_description = "Transfer blenderKit metadata from one object to another when fixing uploads with wrong parenting"
    
    Vilém Duha's avatar
    Vilém Duha committed
        bl_options = {'REGISTER', 'UNDO'}
    
        def execute(self, context):
            source_ob = bpy.context.active_object
            for target_ob in bpy.context.selected_objects:
                if target_ob != source_ob:
                    target_ob.property_unset('blenderkit')
                    for k in source_ob.keys():
                        target_ob[k] = source_ob[k]
            source_ob.property_unset('blenderkit')
            return {'FINISHED'}
    
    
    class UndoWithContext(bpy.types.Operator):
        """Regenerate cobweb"""
        bl_idname = "wm.undo_push_context"
        bl_label = "BlnenderKit undo push"
        bl_description = "BlenderKit undo push with fixed context"
        bl_options = {'REGISTER', 'UNDO', 'INTERNAL'}
    
        # def modal(self, context, event):
        #     return {'RUNNING_MODAL'}
    
    
        message: StringProperty('Undo Message', default='BlenderKit operation')
    
        def execute(self, context):
    
            # C_dict = utils.get_fake_context(context)
    
    Vilém Duha's avatar
    Vilém Duha committed
            # w, a, r = get_largest_area(area_type=area_type)
    
    Vilém Duha's avatar
    Vilém Duha committed
            # wm = bpy.context.window_manager#bpy.data.window_managers[0]
            # w = wm.windows[0]
            #
            # C_dict = {'window': w, 'screen': w.screen}
    
            # bpy.ops.ed.undo_push(C_dict, 'INVOKE_REGION_WIN', message=self.message)
    
            # bpy.ops.ed.undo_push('INVOKE_REGION_WIN', message=self.message)
    
    
    def draw_callback_dragging(self, context):
        img = bpy.data.images.get(self.iname)
        linelength = 35
        scene = bpy.context.scene
        ui_props = scene.blenderkitUI
        ui_bgl.draw_image(self.mouse_x + linelength, self.mouse_y - linelength - ui_props.thumb_size,
                          ui_props.thumb_size, ui_props.thumb_size, img, 1)
        ui_bgl.draw_line2d(self.mouse_x, self.mouse_y, self.mouse_x + linelength,
                           self.mouse_y - linelength, 2, colors.WHITE)
    
    
    def draw_callback_3d_dragging(self, context):
        ''' Draw snapped bbox while dragging. '''
        if not utils.guard_from_crash():
            return
        ui_props = context.scene.blenderkitUI
        # print(ui_props.asset_type, self.has_hit, self.snapped_location)
        if ui_props.asset_type == 'MODEL':
            if self.has_hit:
                draw_bbox(self.snapped_location, self.snapped_rotation, self.snapped_bbox_min, self.snapped_bbox_max)
    
    
    def find_and_activate_instancers(object):
        for ob in bpy.context.visible_objects:
            if ob.instance_type == 'COLLECTION' and ob.instance_collection and object.name in ob.instance_collection.objects:
                utils.activate(ob)
                return ob
    
    
    
    class AssetDragOperator(bpy.types.Operator):
    
        """Drag & drop assets into scene."""
    
        bl_idname = "view3d.asset_drag_drop"
        bl_label = "BlenderKit asset drag drop"
    
        asset_search_index: IntProperty(name="Active Index", default=0)
    
        def handlers_remove(self):
            bpy.types.SpaceView3D.draw_handler_remove(self._handle, 'WINDOW')
            bpy.types.SpaceView3D.draw_handler_remove(self._handle_3d, 'WINDOW')
    
        def mouse_release(self):
            scene = bpy.context.scene
            ui_props = scene.blenderkitUI
    
            if not self.has_hit:
                return {'RUNNING_MODAL'}
    
            if ui_props.asset_type == 'MODEL':
                target_object = ''
                if self.object_name is not None:
                    target_object = self.object_name
                    target_slot = ''
    
            if ui_props.asset_type == 'MATERIAL':
                # first, test if object can have material applied.
                object = bpy.data.objects[self.object_name]
    
                # this enables to run Bring to scene automatically when dropping on a linked objects.
                # it's however quite a slow operation, that's why not enabled (and finished) now.
                # if object is not None and object.is_library_indirect:
                #     find_and_activate_instancers(object)
                #     bpy.ops.object.blenderkit_bring_to_scene()
    
                if object is not None and not object.is_library_indirect and object.type == 'MESH':
                    target_object = object.name
                    # create final mesh to extract correct material slot
                    depsgraph = bpy.context.evaluated_depsgraph_get()
                    object_eval = object.evaluated_get(depsgraph)
                    temp_mesh = object_eval.to_mesh()
                    target_slot = temp_mesh.polygons[self.face_index].material_index
                    object_eval.to_mesh_clear()
    
                # elif object.is_library_indirect:#case for bring to scene objects, will be solved through prefs and direct
                # action
    
                    if object.is_library_indirect:
    
                        ui_panels.ui_message(title='This object is linked from outer file',
                                             message="Please select the model,"
                                                     "go to the 'Selected Model' panel "
                                                     "in BlenderKit and hit 'Bring to Scene' first.")
    
                    self.report({'WARNING'}, "Invalid or library object as input:")
                    target_object = ''
                    target_slot = ''
    
    
            if abs(self.start_mouse_x - self.mouse_x) < 20 and abs(self.start_mouse_y - self.mouse_y) < 20:
                # no dragging actually this was a click.
    
                self.snapped_location = scene.cursor.location
    
                self.snapped_rotation = (0, 0, 0)
    
                if ui_props.asset_type in ('MATERIAL',):
                    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
                        # change snapped location for placing material downloader.
                        self.snapped_location = bpy.context.active_object.location
                    else:
                        target_object = ''
                        target_slot = ''
    
            # picking of assets and using them
            if ui_props.asset_type == 'MATERIAL':
                if target_object != '':
                    # position is for downloader:
                    loc = self.snapped_location
                    rotation = (0, 0, 0)
    
                    utils.automap(target_object, target_slot=target_slot,
                                  tex_size=self.asset_data.get('texture_size_meters', 1.0))
                    bpy.ops.scene.blenderkit_download(True,
                                                      # asset_type=ui_props.asset_type,
                                                      asset_index=self.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 'particle_plants' in self.asset_data['tags']:
                    bpy.ops.object.blenderkit_particles_drop("INVOKE_DEFAULT",
                                                             asset_search_index=self.asset_search_index,
                                                             model_location=self.snapped_location,
                                                             model_rotation=self.snapped_rotation,
                                                             target_object=target_object)
                else:
                    bpy.ops.scene.blenderkit_download(True,
                                                      # asset_type=ui_props.asset_type,
                                                      asset_index=self.asset_search_index,
                                                      model_location=self.snapped_location,
                                                      model_rotation=self.snapped_rotation,
                                                      target_object=target_object)
    
            else:
    
                if ui_props.asset_type == 'SCENE':
                    ui_panels.ui_message(title='Scene will be appended after download',
                                         message='After the scene is appended, you have to switch to it manually.'
                                                 'If you want to switch to scenes automatically after appending,'
                                                 ' you can set it in import settings.')
    
                bpy.ops.scene.blenderkit_download(  # asset_type=ui_props.asset_type,
                    asset_index=self.asset_search_index)
    
        def modal(self, context, event):
            scene = bpy.context.scene
            ui_props = scene.blenderkitUI
            context.area.tag_redraw()
    
            # if event.type == 'MOUSEMOVE':
    
            if not hasattr(self, 'start_mouse_x'):
    
                self.start_mouse_x = event.mouse_region_x
                self.start_mouse_y = event.mouse_region_y
    
            self.mouse_x = event.mouse_region_x
            self.mouse_y = event.mouse_region_y
    
            if event.type == 'LEFTMOUSE' and event.value == 'RELEASE':
                self.mouse_release()
                self.handlers_remove()
                return {'FINISHED'}
    
            elif event.type in {'RIGHTMOUSE', 'ESC'}:
                self.handlers_remove()
                return {'CANCELLED'}
    
            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
    
    
            self.object_name = None
    
            #### TODO - this snapping code below is 3x in this file.... refactor it.
            self.has_hit, self.snapped_location, self.snapped_normal, self.snapped_rotation, self.face_index, object, self.matrix = mouse_raycast(
                context, event.mouse_region_x, event.mouse_region_y)
            if object is not None:
    
                self.object_name = object.name
    
    
            # MODELS can be dragged on scene floor
            if not self.has_hit and ui_props.asset_type == 'MODEL':
                self.has_hit, self.snapped_location, self.snapped_normal, self.snapped_rotation, self.face_index, object, self.matrix = floor_raycast(
                    context,
                    event.mouse_region_x, event.mouse_region_y)
                if object is not None:
                    self.object_name = object.name
    
            if ui_props.asset_type == 'MODEL':
                self.snapped_bbox_min = Vector(self.asset_data['bbox_min'])
                self.snapped_bbox_max = Vector(self.asset_data['bbox_max'])
    
            return {'RUNNING_MODAL'}