Skip to content

extract_editorial_package

ExtractEditorialPackage

Bases: Extractor

Extract and Render intermediate file for Editorial Package

Source code in client/ayon_hiero/plugins/publish/extract_editorial_package.py
 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
class ExtractEditorialPackage(publish.Extractor):
    """Extract and Render intermediate file for Editorial Package"""

    label = "Extract Editorial Package"
    order = pyblish.api.ExtractorOrder + 0.45
    families = ["editorial_pkg"]

    @staticmethod
    def _get_anticipated_publish_path(
            instance: pyblish.api.Instance,
            repre_data: dict[str, Any],
        ) -> str:
        anatomy = instance.context.data["anatomy"]
        template_data = deepcopy(instance.data["anatomyData"])
        template_data["root"] = anatomy.roots
        template_data["representation"] = repre_data["name"]
        template_data["ext"] = repre_data["ext"]
        template_data.pop("comment", None)

        template = anatomy.get_template_item("publish", "default", "path")
        template_filled = template.format_strict(template_data)
        file_path = Path(template_filled)
        return file_path.as_posix()

    @staticmethod
    def _remap_all_clips_to_media(
            otio_timeline: otio.schema.Timeline,
            media_path: str
        ):

        # Make new media reference to store in clips
        timeline_fps = otio_timeline.duration().rate
        timeline_duration = otio_timeline.duration().to_frames()
        timeline_start_frame = otio_timeline.global_start_time.to_frames()
        new_media_reference = otio.schema.ExternalReference(
            target_url=media_path,
            available_range=otio.opentime.TimeRange(
                start_time=otio.opentime.RationalTime(
                    value=timeline_start_frame,
                    rate=timeline_fps,
                ),
                duration=otio.opentime.RationalTime(
                    value=timeline_duration,
                    rate=timeline_fps,
                ),
            ),
        )

        # Remap all media clips from the timeline
        for track in otio_timeline.tracks:
            for clip in track:
                if (
                    not hasattr(clip, "media_reference")
                ):
                    # Skip non-media related clips
                    continue

                clip.media_reference = new_media_reference
                clip.source_range = otio.opentime.TimeRange(
                    start_time=otio.opentime.RationalTime(
                        value=(
                            timeline_start_frame
                            + clip.range_in_parent().start_time.value
                        ),
                        rate=timeline_fps,
                    ),
                    duration=clip.range_in_parent().duration,
                )

    def process(self, instance: pyblish.api.Instance):
        instance.data.setdefault("representations", [])

        temp_dir = self.staging_dir(instance)
        seq = instance.data["hiero_sequence"]

        # Export timeline as consolidated media
        output_video = os.path.join(
            temp_dir,
            f"{seq.guid()}_intermediate.mov"
        )
        rendering.render_sequence_as_quicktime(
            output_video,
            sequence=seq
        )
        intermediate_repre = {
            "name": "intermediate",
            "ext": "mov",
            "files": os.path.basename(output_video),
            "stagingDir": temp_dir,
            "tags": ["review"]
        }
        instance.data["representations"].append(intermediate_repre)
        published_path = self._get_anticipated_publish_path(
            instance,
            intermediate_repre
        )
        self.log.info(
            "Added intermediate file representation: "
            f"{intermediate_repre}"
        )

        # Export sequence as OTIO but remap to rendered consolidated media
        otio_timeline = hiero_export.create_otio_timeline(
            sequence=seq,
        )
        self._remap_all_clips_to_media(
            otio_timeline,
            published_path,
        )

        # Export resulting OTIO file and add as representation.
        remap_otio_file = os.path.join(
            temp_dir,
            f"{seq.guid()}_remap.otio"
        )
        otio.adapters.write_to_file(
            otio_timeline,
            remap_otio_file
        )
        representation_otio = {
            "name": "editorial_pkg",
            "ext": "otio",
            "files": os.path.basename(remap_otio_file),
            "stagingDir": temp_dir,
        }
        instance.data["representations"].append(representation_otio)

        self.log.info(
            "Added OTIO file representation: "
            f"{representation_otio}"
        )