Skip to content

Commit ef29524

Browse files
authored
Merge pull request #92 from mbrainerd/feature-5308
#5308: differentiate item types on the basis of creation_properties
2 parents 79a840f + 8edb839 commit ef29524

File tree

1 file changed

+73
-37
lines changed

1 file changed

+73
-37
lines changed

hooks/basic/collector.py

Lines changed: 73 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -419,14 +419,14 @@ def _get_item_context_from_path(self, work_path_template, path, parent_item, def
419419
else:
420420
return parent_item.context
421421

422-
def _collect_file(self, settings, parent_item, path):
422+
def _collect_file(self, settings, parent_item, path, creation_properties=None):
423423
"""
424424
Process the supplied file path.
425425
426426
:param dict settings: Configured settings for this collector
427427
:param parent_item: parent item instance
428428
:param path: Path to analyze
429-
:param frame_sequence: Treat the path as a part of a sequence
429+
:param creation_properties: The dict of initial properties for the item
430430
431431
:returns: The item that was created
432432
"""
@@ -470,7 +470,8 @@ def _collect_file(self, settings, parent_item, path):
470470
)
471471
return
472472

473-
file_item = self._add_file_item(settings, parent_item, path, is_sequence, seq_files)
473+
file_item = self._add_file_item(settings, parent_item, path, is_sequence, seq_files,
474+
creation_properties=creation_properties)
474475
if file_item:
475476
if is_sequence:
476477
# include an indicator that this is an image sequence and the known
@@ -498,13 +499,14 @@ def _collect_file(self, settings, parent_item, path):
498499

499500
return file_item
500501

501-
def _collect_folder(self, settings, parent_item, folder):
502+
def _collect_folder(self, settings, parent_item, folder, creation_properties=None):
502503
"""
503504
Process the supplied folder path.
504505
505506
:param dict settings: Configured settings for this collector
506507
:param parent_item: parent item instance
507508
:param folder: Path to analyze
509+
:param creation_properties: The dict of initial properties for the item
508510
509511
:returns: The item that was created
510512
"""
@@ -519,7 +521,8 @@ def _collect_folder(self, settings, parent_item, folder):
519521

520522
file_items = []
521523
for path, seq_files in frame_sequences:
522-
file_item = self._add_file_item(settings, parent_item, path, True, seq_files)
524+
file_item = self._add_file_item(settings, parent_item, path, True, seq_files,
525+
creation_properties=creation_properties)
523526
if file_item:
524527
# include an indicator that this is an image sequence and the known
525528
# file that belongs to this sequence
@@ -545,8 +548,8 @@ def _collect_folder(self, settings, parent_item, folder):
545548

546549
return file_items
547550

548-
def _add_file_item(self, settings, parent_item, path, is_sequence=False, seq_files=None,
549-
item_name=None, item_type=None, context=None, properties=None):
551+
def _add_file_item(self, settings, parent_item, path, is_sequence=False, seq_files=None, item_name=None,
552+
item_type=None, context=None, creation_properties=None):
550553
"""
551554
Creates a file item
552555
@@ -558,7 +561,7 @@ def _add_file_item(self, settings, parent_item, path, is_sequence=False, seq_fil
558561
:param item_name: The name of the item instance
559562
:param item_type: The type of the item instance
560563
:param context: The :class:`sgtk.Context` to set for the item
561-
:param properties: The dict of initial properties for the item
564+
:param creation_properties: The dict of initial properties for the item
562565
563566
:returns: The item that was created
564567
"""
@@ -568,19 +571,22 @@ def _add_file_item(self, settings, parent_item, path, is_sequence=False, seq_fil
568571
if not item_name:
569572
item_name = publisher.util.get_publish_name(path)
570573

571-
# Lookup this item's item_type from the settings object
572-
if not item_type:
573-
item_type = self._get_item_type_from_settings(settings, path, is_sequence)
574-
575-
type_info = self._get_item_type_info(settings, item_type)
576-
ignore_sequence = type_info["ignore_sequences"]
577574
# Define the item's properties
578-
properties = properties or {}
575+
properties = creation_properties or {}
579576

580577
# set the path and is_sequence properties for the plugins to use
581578
properties["path"] = path
582579
properties["is_sequence"] = is_sequence
583580

581+
# Lookup this item's item_type from the settings object
582+
if not item_type:
583+
# use the properties dict here, in case user doesn't use the creation_properties arg
584+
item_type = self._get_item_type_from_settings(settings, path, is_sequence,
585+
creation_properties=properties)
586+
587+
type_info = self._get_item_type_info(settings, item_type)
588+
ignore_sequence = type_info["ignore_sequences"]
589+
584590
# item intentionally ignores sequences
585591
if ignore_sequence:
586592
properties["is_sequence"] = False
@@ -620,22 +626,16 @@ def _add_file_item(self, settings, parent_item, path, is_sequence=False, seq_fil
620626

621627
return file_item
622628

623-
def _get_item_type_from_settings(self, settings, path, is_sequence):
624-
"""
625-
Return the item type for the given filename from the settings object.
629+
def _get_filtered_item_types_from_settings(self, settings, path, is_sequence, creation_properties):
626630

627-
The method will try to identify the file as a common file type. If not,
628-
it will use the mimetype category. If the file still cannot be
629-
identified, it will fallback to a generic file type.
631+
"""
632+
Returns a list of tuples containing (resolution_order, work_path_template, item_type).
633+
This filtered list of item types can then be passed down to resolve the correct item_type.
630634
631635
:param dict settings: Configured settings for this collector
632636
:param path: The file path to identify type info for
633637
:param is_sequence: Bool whether or not path is a sequence path
634-
635-
:return: A string representing the item_type::
636-
637-
The item type will be of the form `file.<type>` where type is a specific
638-
common type or a generic classification of the file.
638+
:param creation_properties: The dict of initial properties for the item
639639
"""
640640
publisher = self.parent
641641

@@ -644,12 +644,6 @@ def _get_item_type_from_settings(self, settings, path, is_sequence):
644644
extension = file_info["extension"]
645645
filename = file_info["filename"]
646646

647-
# default values used if no specific type can be determined
648-
item_type = "file.unknown"
649-
650-
# keep track if a common type was identified for the extension
651-
common_type_found = False
652-
653647
# tuple of resolution_order, work_path_template and item_type
654648
template_item_type_mapping = list()
655649

@@ -678,9 +672,6 @@ def _get_item_type_from_settings(self, settings, path, is_sequence):
678672
# we have a match! update the work_path_template
679673
matched_work_path_template = template.name
680674

681-
# found the extension in the common types lookup.
682-
common_type_found = True
683-
684675
ignore_sequences = type_info["ignore_sequences"]
685676
# If we are dealing with a sequence, first check if we have a
686677
# separate definition for a sequence of this type specifically,
@@ -697,13 +688,58 @@ def _get_item_type_from_settings(self, settings, path, is_sequence):
697688
template_item_type_mapping.append((matched_resolution_order, matched_work_path_template,
698689
current_item_type))
699690

700-
# sort the list on resolution_order, giving preference to a matching template
701-
template_item_type_mapping.sort(key=lambda elem: elem[0] if not elem[1] else -1)
691+
max_resolution_order = max(
692+
[resolution_order for resolution_order, work_path_template, item_type in template_item_type_mapping]
693+
)
694+
695+
# sort the list on resolution_order, giving preference to a matching template
696+
template_item_type_mapping.sort(
697+
key=lambda elem: elem[0] if not elem[1] else elem[0]-max_resolution_order)
698+
699+
return template_item_type_mapping
700+
701+
def _get_item_type_from_settings(self, settings, path, is_sequence, creation_properties):
702+
"""
703+
Return the item type for the given filename from the settings object.
704+
705+
The method will try to identify the file as a common file type. If not,
706+
it will use the mimetype category. If the file still cannot be
707+
identified, it will fallback to a generic file type.
708+
709+
:param dict settings: Configured settings for this collector
710+
:param path: The file path to identify type info for
711+
:param is_sequence: Bool whether or not path is a sequence path
712+
:param creation_properties: The dict of initial properties for the item
713+
714+
:return: A string representing the item_type::
715+
716+
The item type will be of the form `file.<type>` where type is a specific
717+
common type or a generic classification of the file.
718+
"""
719+
publisher = self.parent
720+
721+
# extract the components of the supplied path
722+
file_info = publisher.util.get_file_path_components(path)
723+
extension = file_info["extension"]
724+
filename = file_info["filename"]
725+
726+
# default values used if no specific type can be determined
727+
item_type = "file.unknown"
728+
729+
# keep track if a common type was identified for the extension
730+
common_type_found = False
731+
732+
# tuple of resolution_order, work_path_template and item_type
733+
template_item_type_mapping = self._get_filtered_item_types_from_settings(settings, path,
734+
is_sequence, creation_properties)
702735

703736
# this should work fine in case there is no work path template defined too
704737
# this method gives preference to the first match that we get for any template
705738
# also, there should never be a match with more than one templates, since template_from_path will fail too.
706739
if len(template_item_type_mapping):
740+
# found the extension in the item types lookup.
741+
common_type_found = True
742+
707743
resolution_order, work_path_template, item_type = template_item_type_mapping[0]
708744
# 0 index contains a matching work_path_template if any.
709745
# if there is no match that means we need to ask the user

0 commit comments

Comments
 (0)