Skip to content

collect_shot_instances

CollectShotInstance

Bases: InstancePlugin

Collect shot instances

Resolving its user inputs from creator attributes to instance data.

Source code in client/ayon_traypublisher/plugins/publish/collect_shot_instances.py
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
class CollectShotInstance(pyblish.api.InstancePlugin):
    """ Collect shot instances

    Resolving its user inputs from creator attributes
    to instance data.
    """

    label = "Collect Shot Instances"
    order = pyblish.api.CollectorOrder - 0.09

    hosts = ["traypublisher"]
    families = ["shot"]

    SHARED_KEYS = [
        "folderPath",
        "fps",
        "handleStart",
        "handleEnd",
        "frameStart",
        "frameEnd",
        "clipIn",
        "clipOut",
        "clipDuration",
        "sourceIn",
        "sourceOut",
        "otioClip",
        "workfileFrameStart"
    ]

    def process(self, instance):
        creator_identifier = instance.data["creator_identifier"]
        if "editorial" not in creator_identifier:
            return

        # get otio clip object
        otio_clip = self._get_otio_clip(instance)
        instance.data["otioClip"] = otio_clip

        # first solve the inputs from creator attr
        data = self._solve_inputs_to_data(instance)
        instance.data.update(data)

        # distribute all shared keys to clips instances
        self._distribute_shared_data(instance)
        self._solve_hierarchy_context(instance)

        self.log.debug(pformat(instance.data))

    def _get_otio_clip(self, instance):
        """ Converts otio string data.

        Convert them to proper otio object
        and finds its equivalent at otio timeline.
        This process is a hack to support also
        resolving parent range.

        Args:
            instance (obj): publishing instance

        Returns:
            otio.Clip: otio clip object
        """
        context = instance.context
        # convert otio clip from string to object
        otio_clip_string = instance.data.pop("otioClip")
        otio_clip = otio.adapters.read_from_string(
            otio_clip_string)

        otio_timeline = context.data["otioTimeline"]

        clips = [
            clip for clip in otio_timeline.find_clips()
            if clip.name == otio_clip.name
            if clip.parent().kind == "Video"
        ]

        otio_clip = clips.pop()

        return otio_clip

    def _distribute_shared_data(self, instance):
        """ Distribute all defined keys.

        All data are shared between all related
        instances in context.

        Args:
            instance (obj): publishing instance
        """
        context = instance.context

        instance_id = instance.data["instance_id"]

        if not context.data.get("editorialSharedData"):
            context.data["editorialSharedData"] = {}

        context.data["editorialSharedData"][instance_id] = {
            _k: _v for _k, _v in instance.data.items()
            if _k in self.SHARED_KEYS
        }

    def _solve_inputs_to_data(self, instance):
        """ Resolve all user inputs into instance data.

        Args:
            instance (obj): publishing instance

        Returns:
            dict: instance data updating data
        """
        _cr_attrs = instance.data["creator_attributes"]
        workfile_start_frame = _cr_attrs["workfile_start_frame"]
        frame_start = _cr_attrs["frameStart"]
        frame_end = _cr_attrs["frameEnd"]
        frame_dur = frame_end - frame_start

        return {
            "fps": float(_cr_attrs["fps"]),
            "handleStart": _cr_attrs["handle_start"],
            "handleEnd": _cr_attrs["handle_end"],
            "frameStart": workfile_start_frame,
            "frameEnd": workfile_start_frame + frame_dur,
            "clipIn": _cr_attrs["clipIn"],
            "clipOut": _cr_attrs["clipOut"],
            "clipDuration": _cr_attrs["clipDuration"],
            "sourceIn": _cr_attrs["sourceIn"],
            "sourceOut": _cr_attrs["sourceOut"],
            "workfileFrameStart": workfile_start_frame,
            "folderPath": _cr_attrs["folderPath"],
            "integrate": False,
        }

    def _solve_hierarchy_context(self, instance):
        """ Adding hierarchy data to context shared data.

        Args:
            instance (obj): publishing instance
        """
        context = instance.context

        final_context = (
            context.data["hierarchyContext"]
            if context.data.get("hierarchyContext")
            else {}
        )

        # get handles
        handle_start = int(instance.data["handleStart"])
        handle_end = int(instance.data["handleEnd"])

        in_info = {
            "entity_type": "folder",
            "folder_type": "Shot",
            "attributes": {
                "handleStart": handle_start,
                "handleEnd": handle_end,
                "frameStart": instance.data["frameStart"],
                "frameEnd": instance.data["frameEnd"],
                "clipIn": instance.data["clipIn"],
                "clipOut": instance.data["clipOut"],
                "fps": instance.data["fps"]
            },
            "tasks": instance.data["tasks"]
        }

        parents = instance.data.get('parents', [])

        folder_name = instance.data["folderPath"].split("/")[-1]
        actual = {folder_name: in_info}

        for parent in reversed(parents):
            parent_name = parent["entity_name"]
            parent_info = {
                "entity_type": parent["entity_type"],
                "children": actual,
            }
            if parent_info["entity_type"] == "folder":
                parent_info["folder_type"] = parent["folder_type"]
            actual = {parent_name: parent_info}

        final_context = self._update_dict(final_context, actual)

        # adding hierarchy context to instance
        context.data["hierarchyContext"] = final_context

    def _update_dict(self, ex_dict, new_dict):
        """ Recursion function

        Updating nested data with another nested data.

        Args:
            ex_dict (dict): nested data
            new_dict (dict): nested data

        Returns:
            dict: updated nested data
        """
        for key in ex_dict:
            if key in new_dict and isinstance(ex_dict[key], dict):
                new_dict[key] = self._update_dict(ex_dict[key], new_dict[key])
            elif not ex_dict.get(key) or not new_dict.get(key):
                new_dict[key] = ex_dict[key]

        return new_dict