codes clean up & make sure it supports mulitple asset loading per project

This commit is contained in:
Kayla Man 2024-11-15 21:58:59 +08:00
parent 0bec953dec
commit 574ea3580d
4 changed files with 127 additions and 94 deletions

View file

@ -105,9 +105,9 @@ class SceneInventoryController:
def get_container_items_by_id(self, item_ids):
return self._containers_model.get_container_items_by_id(item_ids)
def get_representation_info_items(self, representation_ids):
def get_representation_info_items(self, project_name, representation_ids):
return self._containers_model.get_representation_info_items(
representation_ids
project_name, representation_ids
)
def get_version_items(self, project_name, product_ids):

View file

@ -129,43 +129,54 @@ class InventoryModel(QtGui.QStandardItemModel):
self._clear_items()
items_by_repre_id = {}
project_names = set()
items_by_repre_id = collections.defaultdict(list)
repre_ids_by_project = collections.defaultdict(set)
for container_item in container_items:
# if (
# selected is not None
# and container_item.item_id not in selected
# ):
# continue
project_name = (
container_item.project_name or
self._controller.get_current_project_name()
)
repre_id = container_item.representation_id
project_name = container_item.project_name
items = items_by_repre_id.setdefault(repre_id, [])
items.append(container_item)
project_names.add(project_name)
items_by_repre_id[repre_id].append(container_item)
repre_ids_by_project[project_name].add(repre_id)
repre_id = set(items_by_repre_id.keys())
repre_info_by_id = self._controller.get_representation_info_items(
repre_id
)
repre_info_by_id = {}
for project_name, repre_ids in repre_ids_by_project.items():
repre_info = self._controller.get_representation_info_items(
project_name, repre_ids
)
repre_info_by_id.update(repre_info)
product_ids = {
repre_info.product_id
for repre_info in repre_info_by_id.values()
if repre_info.is_valid
}
project_products = {project_name: set() for project_name in project_names}
for representation_id, items in items_by_repre_id.items():
project_products = collections.defaultdict(set)
for container_item in container_items:
representation_id = container_item.representation_id
project_name = (
container_item.project_name or
self._controller.get_current_project_name()
)
repre_info = repre_info_by_id.get(representation_id)
if repre_info and repre_info.is_valid:
product_id = repre_info.product_id
for item in items:
project_name = item.project_name
project_products[project_name].add(product_id)
project_products[project_name].add(product_id)
version_items_by_product_id = {}
for project_name, product_ids in project_products.items():
version_items_by_product_id.update(self._controller.get_version_items(
version_items = self._controller.get_version_items(
project_name, product_ids
))
)
version_items_by_product_id.update(version_items)
# SiteSync addon information
progress_by_id = self._controller.get_representations_site_progress(
repre_id
@ -299,7 +310,7 @@ class InventoryModel(QtGui.QStandardItemModel):
group_item.setData(active_site_icon, ACTIVE_SITE_ICON_ROLE)
group_item.setData(remote_site_icon, REMOTE_SITE_ICON_ROLE)
group_item.setData(False, IS_CONTAINER_ITEM_ROLE)
print(group_item)
if version_color is not None:
group_item.setData(version_color, VERSION_COLOR_ROLE)

View file

@ -193,20 +193,18 @@ class ContainersModel:
self._containers_by_id = {}
self._container_items_by_id = {}
self._container_items_by_project = {}
self._project_name_by_repre_id = {}
self._version_items_by_product_id = {}
self._repre_info_by_id = {}
self._product_id_by_project = {}
self._product_ids_by_project = {}
def reset(self):
self._items_cache = None
self._containers_by_id = {}
self._container_items_by_id = {}
self._container_items_by_project = {}
self._project_name_by_repre_id = {}
self._version_items_by_product_id = {}
self._repre_info_by_id = {}
self._product_id_by_project = {}
self._product_ids_by_project = {}
def get_containers(self):
self._update_cache()
@ -228,20 +226,17 @@ class ContainersModel:
for item_id in item_ids
}
def get_representation_info_items(self, representation_ids):
def get_representation_info_items(self, project_name, representation_ids):
output = {}
missing_repre_ids_by_project = {}
current_project_name = self._controller.get_current_project_name()
if project_name is None:
project_name = self._controller.get_current_project_name()
for repre_id in representation_ids:
try:
uuid.UUID(repre_id)
except ValueError:
output[repre_id] = RepresentationInfo.new_invalid()
continue
project_name = self._project_name_by_repre_id.get(repre_id)
if project_name is None:
project_name = current_project_name
repre_info = self._repre_info_by_id.get(repre_id)
if repre_info is None:
missing_repre_ids_by_project.setdefault(
@ -256,6 +251,7 @@ class ContainersModel:
repre_hierarchy_by_id = get_representations_hierarchy(
project_name, missing_ids
)
self._product_ids_by_project[project_name] = set()
for repre_id, repre_hierarchy in repre_hierarchy_by_id.items():
kwargs = {
"folder_id": None,
@ -287,20 +283,22 @@ class ContainersModel:
repre_info = RepresentationInfo(**kwargs)
self._repre_info_by_id[repre_id] = repre_info
self._product_id_by_project[project_name] = repre_info.product_id
self._product_ids_by_project[project_name].add(
repre_info.product_id)
output[repre_id] = repre_info
return output
def get_version_items(self, project_name, product_ids):
if not product_ids:
return {}
if project_name is None:
project_name = self._controller.get_current_project_name()
missing_ids = {
product_id
for product_id in product_ids
if product_id not in self._version_items_by_product_id
}
current_product_id = self._product_id_by_project.get(project_name)
current_product_ids = self._product_ids_by_project.get(project_name)
if missing_ids:
status_items_by_name = {
status_item.name: status_item
@ -309,22 +307,19 @@ class ContainersModel:
def version_sorted(entity):
return entity["version"]
if current_product_id not in missing_ids:
return
current_missing_ids = current_product_ids.intersection(missing_ids)
version_entities_by_product_id = {
product_id: []
for product_id in missing_ids
for product_id in current_missing_ids
}
version_entities = list(ayon_api.get_versions(
project_name,
product_ids={current_product_id},
product_ids=current_missing_ids,
fields={"id", "version", "productId", "status"}
))
version_entities.sort(key=version_sorted)
for version_entity in version_entities:
product_id = version_entity["productId"]
if product_id not in missing_ids:
continue
version_entities_by_product_id[product_id].append(
version_entity
)
@ -402,5 +397,4 @@ class ContainersModel:
self._containers_by_id = containers_by_id
self._container_items_by_id = container_items_by_id
self._project_name_by_repre_id = project_name_by_repre_id
self._items_cache = container_items

View file

@ -192,11 +192,20 @@ class SceneInventoryView(QtWidgets.QTreeView):
container_item = container_items_by_id[item_id]
active_repre_id = container_item.representation_id
break
repre_ids_by_project = collections.defaultdict(set)
for container_item in container_items_by_id.values():
repre_id = container_item.representation_id
project_name = (
container_item.project_name or
self._controller.get_current_project_name()
)
repre_ids_by_project[project_name].add(repre_id)
repre_info_by_id = {}
for project_name, repre_ids in repre_ids_by_project.items():
repre_info = self._controller.get_representation_info_items(
project_name, repre_ids)
repre_info_by_id.update(repre_info)
repre_info_by_id = self._controller.get_representation_info_items({
container_item.representation_id
for container_item in container_items_by_id.values()
})
valid_repre_ids = {
repre_id
for repre_id, repre_info in repre_info_by_id.items()
@ -206,20 +215,20 @@ class SceneInventoryView(QtWidgets.QTreeView):
# Exclude items that are "NOT FOUND" since setting versions, updating
# and removal won't work for those items.
filtered_items = []
project_products = {}
product_ids_by_project = collections.defaultdict(set)
version_ids = set()
for container_item in container_items_by_id.values():
repre_id = container_item.representation_id
project_name = container_item.project_name
project_name = (
container_item.project_name or
self._controller.get_current_project_name()
)
repre_info = repre_info_by_id.get(repre_id)
if repre_info and repre_info.is_valid:
filtered_items.append(container_item)
version_ids.add(repre_info.version_id)
product_id = repre_info.product_id
if project_name not in project_products:
project_products[project_name] = set()
project_products[project_name].add(product_id)
print("p_products", project_products)
product_ids_by_project[project_name].add(product_id)
# remove
remove_icon = qtawesome.icon("fa.remove", color=DEFAULT_COLOR)
remove_action = QtWidgets.QAction(remove_icon, "Remove items", menu)
@ -231,11 +240,12 @@ class SceneInventoryView(QtWidgets.QTreeView):
menu.addAction(remove_action)
return
version_items_by_product_id = {}
for project_name, product_ids in project_products.items():
version_items_by_product_id.update(
self._controller.get_version_items(
project_name, product_ids)
for project_name, product_ids in product_ids_by_project.items():
version_items = self._controller.get_version_items(
project_name, product_ids
)
version_items_by_product_id.update(version_items
)
has_outdated = False
has_loaded_hero_versions = False
has_available_hero_version = False
@ -741,38 +751,47 @@ class SceneInventoryView(QtWidgets.QTreeView):
container_items_by_id = self._controller.get_container_items_by_id(
item_ids
)
print(container_items_by_id, "container")
repre_ids = {
container_item.representation_id
for container_item in container_items_by_id.values()
}
repre_info_by_id = self._controller.get_representation_info_items(
repre_ids
)
repre_ids_by_project = collections.defaultdict(set)
for container_item in container_items_by_id.values():
repre_id = container_item.representation_id
project_name = (
container_item.project_name or
self._controller.get_current_project_name()
)
repre_ids_by_project[project_name].add(repre_id)
repre_info_by_id = {}
for project_name, repre_ids in repre_ids_by_project.items():
repre_info = self._controller.get_representation_info_items(
project_name, repre_ids
)
repre_info_by_id.update(repre_info)
product_ids = {
repre_info.product_id
for repre_info in repre_info_by_id.values()
}
project_products = {}
product_ids_by_project = collections.defaultdict(set)
for container_item in container_items_by_id.values():
repre_id = container_item.representation_id
project_name = container_item.project_name
project_name = (
container_item.project_name or
self._controller.get_current_project_name()
)
repre_info = repre_info_by_id.get(repre_id)
if repre_info and repre_info.is_valid:
if project_name not in project_products:
project_products[project_name] = set()
product_id = repre_info.product_id
project_products[project_name].add(product_id)
print("proj_product", project_products)
if not repre_info or not repre_info.is_valid:
continue
product_ids_by_project[project_name].add(
repre_info.product_id
)
active_repre_info = repre_info_by_id[active_repre_id]
active_version_id = active_repre_info.version_id
active_product_id = active_repre_info.product_id
version_items_by_product_id = {}
for project_name, product_ids in project_products.items():
version_items_by_product_id.update(
self._controller.get_version_items(
project_name, product_ids))
for project_name, project_product_ids in product_ids_by_project.items():
version_items = self._controller.get_version_items(
project_name, project_product_ids
)
version_items_by_product_id.update(version_items)
version_items = list(
version_items_by_product_id[active_product_id].values()
)
@ -949,35 +968,44 @@ class SceneInventoryView(QtWidgets.QTreeView):
def _on_switch_to_versioned(self, item_ids):
# Get container items by ID
containers_items_by_id = self._controller.get_container_items_by_id(item_ids)
repre_ids = {
container_item.representation_id
for container_item in containers_items_by_id.values()
}
# Extract project names and their corresponding representation IDs
project_name_to_repre_ids = {}
repre_ids_by_project = collections.defaultdict(set)
for container_item in containers_items_by_id.values():
project_name = container_item.project_name
project_name = (
container_item.project_name or
self._controller.get_current_project_name()
)
repre_id = container_item.representation_id
if project_name not in project_name_to_repre_ids:
project_name_to_repre_ids[project_name] = set()
project_name_to_repre_ids[project_name].add(repre_id)
repre_ids_by_project[project_name].add(repre_id)
# Get representation info items by ID
repre_info_by_id = self._controller.get_representation_info_items(repre_ids)
repre_info_by_id = {}
for project_name, repre_ids in repre_ids_by_project.items():
repre_info = self._controller.get_representation_info_items(
project_name, repre_ids)
repre_info_by_id.update(repre_info)
# Create a dictionary to map project names to sets of product IDs
project_products = {
project_name: set() for project_name in project_name_to_repre_ids.keys()
}
print("project_products", project_products)
version_items_by_product_id = {}
for project_name, product_ids in project_name_to_repre_ids.items():
version_items_by_product_id.update(
self._controller.get_version_items(
project_name, product_ids
)
product_ids_by_project = collections.defaultdict(set)
for container_item in containers_items_by_id.values():
repre_id = container_item.representation_id
project_name = (
container_item.project_name or
self._controller.get_current_project_name()
)
repre_info = repre_info_by_id.get(repre_id)
if not repre_info or not repre_info.is_valid:
continue
product_ids_by_project[project_name].add(
repre_info.product_id
)
version_items_by_product_id = {}
for project_name, product_ids in product_ids_by_project.items():
version_items = self._controller.get_version_items(
project_name, product_ids
)
version_items_by_product_id.update(version_items)
update_containers = []
update_versions = []