[Bf-blender-cvs] [7a68cd02980] asset-engine: Amber: Add initial real asset preview.

Bastien Montagne noreply at git.blender.org
Fri Oct 27 12:22:21 CEST 2017


Commit: 7a68cd02980b9b2834847a2adacf822a59df1b75
Author: Bastien Montagne
Date:   Thu Oct 26 09:07:17 2017 +0200
Branches: asset-engine
https://developer.blender.org/rB7a68cd02980b9b2834847a2adacf822a59df1b75

Amber: Add initial real asset preview.

Design is a bit weak, ideally we'd rathe use directly .blend files
previews, but we need some RNA API work to handle that...

===================================================================

M	release/scripts/startup/bl_operators/amber/engine.py
M	release/scripts/startup/bl_operators/amber/operators.py
M	release/scripts/startup/bl_operators/amber/repository.py
M	release/scripts/startup/bl_operators/amber/ui.py
M	release/scripts/startup/bl_operators/amber/utils.py

===================================================================

diff --git a/release/scripts/startup/bl_operators/amber/engine.py b/release/scripts/startup/bl_operators/amber/engine.py
index 766001680d9..6c28fd68ee5 100644
--- a/release/scripts/startup/bl_operators/amber/engine.py
+++ b/release/scripts/startup/bl_operators/amber/engine.py
@@ -138,14 +138,28 @@ class AmberJobList(AmberJob):
 class AmberJobPreviews(AmberJob):
     @staticmethod
     def preview(uuid):
-        time.sleep(0.1)  # 100% Artificial Lag (c)
-        w = random.randint(2, 8)
-        h = random.randint(2, 8)
+        repo_uuid = uuid[0]
+        repo_path = AmberDataRepositoryList().repositories.get(repo_uuid, (None, None))[1]
+        if repo_path is None:
+            return [0, 0, []]
+
+        repo = AmberDataRepository()
+        repo.from_dict(repo.ls_repo(os.path.join(repo_path, utils.AMBER_DB_NAME)), repo_path)
+
+        preview_path = os.path.join(repo_path, repo.assets[uuid[1]].preview_path)
+
+        if preview_path and preview_path.endswith(".dat"):
+            w, h, pixels = utils.preview_read_dat(preview_path)
+            return [w, h, list(pixels)]
+
+        #~ time.sleep(0.1)  # 100% Artificial Lag (c)
+        w = random.randint(8, 32)
+        h = random.randint(8, 32)
         return [w, h, [random.getrandbits(32) for i in range(w * h)]]
 
     def start(self, uuids):
         self.nbr = 0
-        self.preview_tasks = {uuid.uuid_asset[:]: self.executor.submit(self.preview, uuid.uuid_asset[:]) for uuid in uuids.uuids}
+        self.preview_tasks = {uuid.uuid_asset[:]: self.executor.submit(self.preview, (uuid.uuid_repository[:], uuid.uuid_asset[:])) for uuid in uuids.uuids}
         self.tot = len(self.preview_tasks)
         self.status = {'VALID', 'RUNNING'}
 
@@ -159,27 +173,28 @@ class AmberJobPreviews(AmberJob):
         del_uuids = old_uuids - new_uuids
         new_uuids -= old_uuids
 
-        for uuid in del_uuids:
-            self.preview_tasks[uuid].cancel()
-            del self.preview_tasks[uuid]
+        for uuid_asset in del_uuids:
+            self.preview_tasks[uuid_asset].cancel()
+            del self.preview_tasks[uuid_asset]
 
-        for uuid in new_uuids:
-            self.preview_tasks[uuid] = self.executor.submit(self.preview, uuid)
+        for uuid_asset in new_uuids:
+            uuid = uuids[uuid_asset]
+            self.preview_tasks[uuid_asset] = self.executor.submit(self.preview, (uuid.uuid_repository[:], uuid_asset))
 
         self.tot = len(self.preview_tasks)
         self.nbr = 0
 
         done_uuids = set()
-        for uuid, tsk in self.preview_tasks.items():
+        for uuid_asset, tsk in self.preview_tasks.items():
             if tsk.done():
                 w, h, pixels = tsk.result()
-                uuids[uuid].preview_size = (w, h)
-                uuids[uuid].preview_pixels = pixels
+                uuids[uuid_asset].preview_size = (w, h)
+                uuids[uuid_asset].preview_pixels = pixels
                 self.nbr += 1
-                done_uuids.add(uuid)
+                done_uuids.add(uuid_asset)
 
-        for uuid in done_uuids:
-            del self.preview_tasks[uuid]
+        for uuid_asset in done_uuids:
+            del self.preview_tasks[uuid_asset]
 
         self.progress = self.nbr / self.tot
         if not self.preview_tasks:
diff --git a/release/scripts/startup/bl_operators/amber/operators.py b/release/scripts/startup/bl_operators/amber/operators.py
index 2bb5d788dcb..dd24f8bbfd1 100644
--- a/release/scripts/startup/bl_operators/amber/operators.py
+++ b/release/scripts/startup/bl_operators/amber/operators.py
@@ -102,52 +102,59 @@ class AmberOpsRepositoryAdd(Operator, AmberOpsEditing):
 
 
 class AmberOpsRepositoryRemove(Operator, AmberOpsEditing):
-    """Remove an Amber repository from this listing, and delete its db and preview files (*not* the data itself) """
-    """(WARNING! No undo!)"""
+    """Remove an Amber repository from this listing, and optionally delete its db and preview files """
+    """(*not* the data itself) (WARNING! No undo!)"""
     bl_idname = "amber.repository_remove"
     bl_label = "Remove Repository"
     bl_options = set()
 
+    do_erase = BoolProperty(name="Erase", description="Actually erase active repository")
+
     def execute(self, context):
         ae = context.space_data.asset_engine
-        repo_pg = ae.repositories_pg.repositories[ae.repositories_pg.repository_index_active]
-        repo_path = repo_pg.path
-        repo_uuid = repo_pg.uuid[:]
-        db_path = os.path.join(repo_path, utils.AMBER_DB_NAME)
-        preview_path = os.path.join(repo_path, utils.AMBER_PREVIEW_STORAGE)
-        data_path = os.path.join(repo_path, utils.AMBER_LOCAL_STORAGE)
-
-        if not os.path.exists(db_path):
-            self.report({'INFO'}, "No Amber repository found at '%s'" % repo_path)
-            return {'CANCELLED'}
 
-        repo = AmberDataRepository.ls_repo(db_path)
+        if self.do_erase:
+            repo_pg = ae.repositories_pg.repositories[ae.repositories_pg.repository_index_active]
+            repo_path = repo_pg.path
+            repo_uuid = repo_pg.uuid[:]
+            db_path = os.path.join(repo_path, utils.AMBER_DB_NAME)
+            preview_path = os.path.join(repo_path, utils.AMBER_PREVIEW_STORAGE)
+            data_path = os.path.join(repo_path, utils.AMBER_LOCAL_STORAGE)
+
+            if not os.path.exists(db_path):
+                self.report({'INFO'}, "No Amber repository found at '%s'" % repo_path)
+                return {'CANCELLED'}
 
-        if os.path.isdir(preview_path):
-            for asset in repo.assets.items:
-                if asset.preview_path:
-                    asset_preview_path = os.abspath(os.path.join(repo_path, asset.preview_path))
-                    if os.path.isfile(asset_preview_path) and os.path.commonpath((preview_path, asset_preview_path)) == preview_path:
-                        os.remove(asset_preview_path)
-                # TODO allow also removing of asset .blend files? would rather not, dangerous option imho...
+            repo = AmberDataRepository.ls_repo(db_path)
 
-            if not os.listdir(preview_path):
-                os.rmdir(preview_path)
+            if os.path.isdir(preview_path):
+                for asset in repo.assets.items:
+                    if asset.preview_path:
+                        asset_preview_path = os.abspath(os.path.join(repo_path, asset.preview_path))
+                        if os.path.isfile(asset_preview_path) and os.path.commonpath((preview_path, asset_preview_path)) == preview_path:
+                            os.remove(asset_preview_path)
+                    # TODO allow also removing of asset .blend files? would rather not, dangerous option imho...
 
-        if os.path.isdir(data_path):
-            if not os.listdir(data_path):
-                os.rmdir(data_path)
+                if not os.listdir(preview_path):
+                    os.rmdir(preview_path)
+
+            if os.path.isdir(data_path):
+                if not os.listdir(data_path):
+                    os.rmdir(data_path)
 
         ae.repositories_pg.repositories.remove(ae.repositories_pg.repository_index_active)
 
-        os.remove(db_path)
+        if self.do_erase:
+            os.remove(db_path)
 
         bpy.ops.file.refresh()
 
         return {'FINISHED'}
 
     def invoke(self, context, event):
-        return context.window_manager.invoke_confirm(self, event)
+        if self.do_erase:
+            return context.window_manager.invoke_confirm(self, event)
+        return self.execute(context)
 
 
 class AmberOpsAssetAdd(Operator, AmberOpsEditing):
@@ -202,6 +209,16 @@ class AmberOpsAssetAdd(Operator, AmberOpsEditing):
         asset.blender_type = utils.BLENDER_TYPES_TO_ENUMVAL[type(datablock)]
         asset.uuid = utils.uuid_asset_gen(set(repository.assets.keys()), ae.repository_pg.uuid, path_sublib, asset.name, [])
 
+        if datablock.preview:
+            w, h = datablock.preview.image_size
+            if 0 not in {w, h}:
+                path_dir = os.path.join(repository.path, utils.AMBER_PREVIEW_STORAGE)
+                if not os.path.exists(path_dir):
+                    os.mkdir(path_dir)
+                path_preview = os.path.join(path_dir, asset.name + "_" + utils.uuid_pack(asset.uuid) + ".dat")
+                utils.preview_write_dat(path_preview, w, h, datablock.preview.image_pixels)
+                asset.preview_path = path_preview
+
         if self.copy_local:
             path_dir = os.path.join(repository.path, utils.AMBER_LOCAL_STORAGE)
             if not os.path.exists(path_dir):
diff --git a/release/scripts/startup/bl_operators/amber/repository.py b/release/scripts/startup/bl_operators/amber/repository.py
index 90896b5fc8a..ca219ade099 100644
--- a/release/scripts/startup/bl_operators/amber/repository.py
+++ b/release/scripts/startup/bl_operators/amber/repository.py
@@ -432,6 +432,7 @@ class AmberDataAssetPG(PropertyGroup):
     blender_type = EnumProperty(items=[(e.identifier, e.name, e.description, e.value) for e in AssetEntry.bl_rna.properties["blender_type"].enum_items],
                                 name="Blender Type", description="Blender data-block type of the asset", update=asset_data_update)
 
+    preview_path = StringProperty(name="Preview Path", description="File path of the preview of this item", subtype='FILE_PATH')
     tags = CollectionProperty(name="Tags", type=AmberDataTagPG, description="Tags of the asset")
     tag_index_active = IntProperty(name="Active Tag", options={'HIDDEN'})
 
@@ -739,15 +740,21 @@ class AmberDataRepositoryList:
 
     def __new__(cls, path=...):
         if cls.singleton is None:
-            cls.singleton = super().__new__(cls)
+            return super().__new__(cls)
         return cls.singleton
 
     def __init__(self, path=...):
-        if path is ...:
-            path = os.path.join(bpy.utils.user_resource('CONFIG', create=True), utils.AMBER_LIST_FILENAME)
-        self._path = ""
-        self.repositories = {}
-        self.path = path
+        if self != self.__class__.singleton:
+            if path is ...:
+                path = os.path.join(bpy.utils.user_resource('CONFIG', create=True), utils.AMBER_LIST_FILENAME)


@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list