BlenderKit: automatically purge collections created by the addon

If the collection is empty, it gets removed on file save.
This will only work in new files, since the collections now get a 'is_blenderkit_collection' property when appending.
Don't registter timers when in background
This commit is contained in:
Vilem Duha 2021-10-03 11:52:05 +02:00
parent 13140e3947
commit 0e4e081406
7 changed files with 33 additions and 20 deletions

View File

@ -151,15 +151,16 @@ def scene_load(context):
@bpy.app.handlers.persistent
def check_timers_timer():
''' checks if all timers are registered regularly. Prevents possible bugs from stopping the addon.'''
if not bpy.app.timers.is_registered(search.search_timer):
bpy.app.timers.register(search.search_timer)
if not bpy.app.timers.is_registered(download.download_timer):
bpy.app.timers.register(download.download_timer)
if not (bpy.app.timers.is_registered(tasks_queue.queue_worker)):
bpy.app.timers.register(tasks_queue.queue_worker)
if not bpy.app.timers.is_registered(bg_blender.bg_update):
bpy.app.timers.register(bg_blender.bg_update)
return 5.0
if not bpy.app.background:
if not bpy.app.timers.is_registered(search.search_timer):
bpy.app.timers.register(search.search_timer)
if not bpy.app.timers.is_registered(download.download_timer):
bpy.app.timers.register(download.download_timer)
if not (bpy.app.timers.is_registered(tasks_queue.queue_worker)):
bpy.app.timers.register(tasks_queue.queue_worker)
if not bpy.app.timers.is_registered(bg_blender.bg_update):
bpy.app.timers.register(bg_blender.bg_update)
return 5.0
conditions = (
@ -1907,7 +1908,7 @@ def register():
asset_bar_op.register()
user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
if user_preferences.use_timers:
if user_preferences.use_timers and not bpy.app.background:
bpy.app.timers.register(check_timers_timer, persistent=True)
bpy.app.handlers.load_post.append(scene_load)

View File

@ -330,6 +330,7 @@ def append_objects(file_name, obnames=[], location=(0, 0, 0), link=False, **kwar
main_object.parent = bpy.data.objects[kwargs['parent']]
main_object.matrix_world.translation = location
#move objects that should be hidden to a sub collection
if len(to_hidden_collection)>0 and collection is not None:
hidden_collection_name = collection_name+'_hidden'
@ -342,8 +343,11 @@ def append_objects(file_name, obnames=[], location=(0, 0, 0), link=False, **kwar
bpy.ops.object.select_all(action='DESELECT')
utils.selection_set(sel)
#let collection also store info that it was created by BlenderKit, for purging reasons
collection['is_blenderkit_asset'] = True
return main_object, return_obs
#this is used for uploads:
with bpy.data.libraries.load(file_name, link=link, relative=True) as (data_from, data_to):
sobs = []

View File

@ -268,7 +268,7 @@ def add_bg_process(location=None, name=None, eval_path_computing='', eval_path_s
def register():
bpy.utils.register_class(KillBgProcess)
user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
if user_preferences.use_timers:
if user_preferences.use_timers and not bpy.app.background:
bpy.app.timers.register(bg_update)

View File

@ -75,6 +75,10 @@ def check_missing():
def check_unused():
'''find assets that have been deleted from scene but their library is still present.'''
# this is obviously broken. Blender should take care of the extra data automaticlaly
#first clean up collections
for c in bpy.data.collections:
if len(c.all_objects) == 0 and c.get('is_blenderkit_asset'):
bpy.data.collections.remove(c)
return;
used_libs = []
for ob in bpy.data.objects:
@ -493,6 +497,9 @@ def append_asset(asset_data, **kwargs): # downloaders=[], location=None,
asset_main['asset_data'] = asset_data # TODO remove this??? should write to blenderkit Props?
asset_main.blenderkit.asset_base_id = asset_data['assetBaseId']
asset_main.blenderkit.id = asset_data['id']
bpy.ops.wm.undo_push_context(message='add %s to scene' % asset_data['name'])
# moving reporting to on save.
# report_use_success(asset_data['id'])
@ -1446,7 +1453,7 @@ def register_download():
bpy.app.handlers.load_post.append(scene_load)
bpy.app.handlers.save_pre.append(scene_save)
user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
if user_preferences.use_timers:
if user_preferences.use_timers and not bpy.app.background:
bpy.app.timers.register(download_timer)

View File

@ -171,10 +171,10 @@ def unpack_asset(data):
#mark asset browser asset
data_block = None
if asset_data['assetType'] == 'model':
for ob in bpy.context.scene.objects:
if ob.parent == None:
ob.asset_mark()
data_block = ob
for c in bpy.data.collections:
if c.get('asset_data') is not None:
c.asset_mark()
data_block = c
elif asset_data['assetType'] == 'material':
for m in bpy.data.materials:
m.asset_mark()
@ -192,7 +192,8 @@ def unpack_asset(data):
tags.remove(t)
tags.new('description: ' + asset_data['description'])
tags.new('tags: ' + ','.join(asset_data['tags']))
#
bpy.ops.wm.save_mainfile(compress=False)
# now try to delete the .blend1 file
try:

View File

@ -179,7 +179,7 @@ def scene_load(context):
purge_search_results()
fetch_server_data()
categories.load_categories()
if not bpy.app.timers.is_registered(refresh_token_timer):
if not bpy.app.timers.is_registered(refresh_token_timer) and not bpy.app.background:
bpy.app.timers.register(refresh_token_timer, persistent=True, first_interval=36000)
update_assets_data()
@ -1625,7 +1625,7 @@ def register_search():
bpy.utils.register_class(c)
user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
if user_preferences.use_timers:
if user_preferences.use_timers and not bpy.app.background:
bpy.app.timers.register(search_timer)
categories.load_categories()

View File

@ -29,7 +29,7 @@ bk_logger = logging.getLogger('blenderkit')
@persistent
def scene_load(context):
user_preferences = bpy.context.preferences.addons['blenderkit'].preferences
if user_preferences.use_timers:
if user_preferences.use_timers and not bpy.app.background:
if not (bpy.app.timers.is_registered(queue_worker)):
bpy.app.timers.register(queue_worker)