Repository: ynput/OpenPype
Issue: Cannot switch asset from hero version to versioned

 **Describe the bug**
It is impossible to switch an asset from hero version to versioned again. It gives the following error:
```
// Traceback (most recent call last):
//   File "C:\\Dev\\OpenPype\\openpype\\openpype\\tools\\sceneinventory\\view.py", line 202, in _on_switch_to_versioned
//     update_container(item, version_name)
//   File "C:\\Dev\\OpenPype\\openpype\\openpype\\pipeline\\load\\utils.py", line 453, in update_container
//     loader = Loader(get_representation_context(container["representation"]))
//   File "C:\\Dev\\OpenPype\\openpype\\openpype\\pipeline\\load\\utils.py", line 229, in get_representation_context
//     assert all([representation, version, subset, asset, project]), (
// AssertionError: This is a bug
```

**To Reproduce**
Steps to reproduce the behavior:
1. From the Scene Inventory, set any asset as hero version.
2. Try to set the same asset to versioned.


**Desktop (please complete the following information):**
 - OS: Windows 11
 - Host: Maya and Blender (but likely affects all the DCCs)

[cuID:2r78gx9]

Changed files:
openpype/pipeline/load/utils.py, openpype/hosts/nuke/plugins/load/load_clip.py, openpype/client/entities.py, openpype/tools/loader/widgets.py

diff --git a/openpype/client/entities.py b/openpype/client/entities.py
index 67ddb09ddb..f1f1d30214 100644
--- a/openpype/client/entities.py
+++ b/openpype/client/entities.py
@@ -1259,58 +1259,64 @@ def get_representations_parents(project_name, representations):
         dict[ObjectId, tuple]: Parents by representation id.
     """
 
-    repres_by_version_id = collections.defaultdict(list)
-    versions_by_version_id = {}
-    versions_by_subset_id = collections.defaultdict(list)
-    subsets_by_subset_id = {}
-    subsets_by_asset_id = collections.defaultdict(list)
+    repre_docs_by_version_id = collections.defaultdict(list)
+    version_docs_by_version_id = {}
+    version_docs_by_subset_id = collections.defaultdict(list)
+    subset_docs_by_subset_id = {}
+    subset_docs_by_asset_id = collections.defaultdict(list)
     output = {}
-    for representation in representations:
-        repre_id = representation["_id"]
+    for repre_doc in representations:
+        repre_id = repre_doc["_id"]
+        version_id = repre_doc["parent"]
         output[repre_id] = (None, None, None, None)
-        version_id = representation["parent"]
-        repres_by_version_id[version_id].append(representation)
+        repre_docs_by_version_id[version_id].append(repre_doc)
 
-    versions = get_versions(
-        project_name, version_ids=repres_by_version_id.keys()
+    version_docs = get_versions(
+        project_name,
+        version_ids=repre_docs_by_version_id.keys(),
+        hero=True
+    )
+    for version_doc in version_docs:
+        version_id = version_doc["_id"]
+        subset_id = version_doc["parent"]
+        version_docs_by_version_id[version_id] = version_doc
+        version_docs_by_subset_id[subset_id].append(version_doc)
+
+    subset_docs = get_subsets(
+        project_name, subset_ids=version_docs_by_subset_id.keys()
     )
-    for version in versions:
-        version_id = version["_id"]
-        subset_id = version["parent"]
-        versions_by_version_id[version_id] = version
-        versions_by_subset_id[subset_id].append(version)
-
-    subsets = get_subsets(
-        project_name, subset_ids=versions_by_subset_id.keys()
+    for subset_doc in subset_docs:
+        subset_id = subset_doc["_id"]
+        asset_id = subset_doc["parent"]
+        subset_docs_by_subset_id[subset_id] = subset_doc
+        subset_docs_by_asset_id[asset_id].append(subset_doc)
+
+    asset_docs = get_assets(
+        project_name, asset_ids=subset_docs_by_asset_id.keys()
     )
-    for subset in subsets:
-        subset_id = subset["_id"]
-        asset_id = subset["parent"]
-        subsets_by_subset_id[subset_id] = subset
-        subsets_by_asset_id[asset_id].append(subset)
-
-    assets = get_assets(project_name, asset_ids=subsets_by_asset_id.keys())
-    assets_by_id = {
-        asset["_id"]: asset
-        for asset in assets
+    asset_docs_by_id = {
+        asset_doc["_id"]: asset_doc
+        for asset_doc in asset_docs
     }
 
-    project = get_project(project_name)
-
-    for version_id, representations in repres_by_version_id.items():
-        asset = None
-        subset = None
-        version = versions_by_version_id.get(version_id)
-        if version:
-            subset_id = version["parent"]
-            subset = subsets_by_subset_id.get(subset_id)
-            if subset:
-                asset_id = subset["parent"]
-                asset = assets_by_id.get(asset_id)
-
-        for representation in representations:
-            repre_id = representation["_id"]
-            output[repre_id] = (version, subset, asset, project)
+    project_doc = get_project(project_name)
+
+    for version_id, repre_docs in repre_docs_by_version_id.items():
+        asset_doc = None
+        subset_doc = None
+        version_doc = version_docs_by_version_id.get(version_id)
+        if version_doc:
+            subset_id = version_doc["parent"]
+            subset_doc = subset_docs_by_subset_id.get(subset_id)
+            if subset_doc:
+                asset_id = subset_doc["parent"]
+                asset_doc = asset_docs_by_id.get(asset_id)
+
+        for repre_doc in repre_docs:
+            repre_id = repre_doc["_id"]
+            output[repre_id] = (
+                version_doc, subset_doc, asset_doc, project_doc
+            )
     return output
 
 
diff --git a/openpype/hosts/nuke/plugins/load/load_clip.py b/openpype/hosts/nuke/plugins/load/load_clip.py
index b2dc4a52d7..346773b5af 100644
--- a/openpype/hosts/nuke/plugins/load/load_clip.py
+++ b/openpype/hosts/nuke/plugins/load/load_clip.py
@@ -162,7 +162,15 @@ class LoadClip(plugin.NukeLoader):
             data_imprint = {}
             for k in add_keys:
                 if k == 'version':
-                    data_imprint[k] = context["version"]['name']
+                    version_doc = context["version"]
+                    if version_doc["type"] == "hero_version":
+                        version = "hero"
+                    else:
+                        version = version_doc.get("name")
+
+                    if version:
+                        data_imprint[k] = version
+
                 elif k == 'colorspace':
                     colorspace = repre["data"].get(k)
                     colorspace = colorspace or version_data.get(k)
diff --git a/openpype/pipeline/load/utils.py b/openpype/pipeline/load/utils.py
index 9945e1fce4..99d6876d4b 100644
--- a/openpype/pipeline/load/utils.py
+++ b/openpype/pipeline/load/utils.py
@@ -222,13 +222,20 @@ def get_representation_context(representation):
             project_name, representation
         )
 
+    if not representation:
+        raise AssertionError("Representation was not found in database")
+
     version, subset, asset, project = get_representation_parents(
         project_name, representation
     )
-
-    assert all([representation, version, subset, asset, project]), (
-        "This is a bug"
-    )
+    if not version:
+        raise AssertionError("Version was not found in database")
+    if not subset:
+        raise AssertionError("Subset was not found in database")
+    if not asset:
+        raise AssertionError("Asset was not found in database")
+    if not project:
+        raise AssertionError("Project was not found in database")
 
     context = {
         "project": {
diff --git a/openpype/tools/loader/widgets.py b/openpype/tools/loader/widgets.py
index 2d8b4b048d..597c35e89b 100644
--- a/openpype/tools/loader/widgets.py
+++ b/openpype/tools/loader/widgets.py
@@ -1547,6 +1547,11 @@ def _load_representations_by_loader(loader, repre_contexts,
         return
 
     for repre_context in repre_contexts.values():
+        version_doc = repre_context["version"]
+        if version_doc["type"] == "hero_version":
+            version_name = "Hero"
+        else:
+            version_name = version_doc.get("name")
         try:
             if data_by_repre_id:
                 _id = repre_context["representation"]["_id"]
@@ -1564,7 +1569,7 @@ def _load_representations_by_loader(loader, repre_contexts,
                 None,
                 repre_context["representation"]["name"],
                 repre_context["subset"]["name"],
-                repre_context["version"]["name"]
+                version_name
             ))
 
         except Exception as exc:
@@ -1577,7 +1582,7 @@ def _load_representations_by_loader(loader, repre_contexts,
                 formatted_traceback,
                 repre_context["representation"]["name"],
                 repre_context["subset"]["name"],
-                repre_context["version"]["name"]
+                version_name
             ))
     return error_info