Skip to content

collect_writes

CollectNukeWrites

Bases: InstancePlugin, ColormanagedPyblishPluginMixin

Collect all write nodes.

Source code in client/ayon_nuke/plugins/publish/collect_writes.py
 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
class CollectNukeWrites(pyblish.api.InstancePlugin,
                        publish.ColormanagedPyblishPluginMixin):
    """Collect all write nodes."""

    order = pyblish.api.CollectorOrder + 0.0021
    label = "Collect Writes"
    hosts = ["nuke", "nukeassist"]
    families = ["render", "prerender", "image"]

    settings_category = "nuke"

    # cache
    _write_nodes = {}
    _frame_ranges = {}

    def process(self, instance):

        # compatibility. This is mainly focused on `renders`folders which
        # were previously not cleaned up (and could be used in read notes)
        # this logic should be removed and replaced with custom staging dir
        if instance.data.get("stagingDir_persistent") is None:
            instance.data["stagingDir_persistent"] = True

        group_node = instance.data["transientData"]["node"]

        render_target = instance.data["render_target"]

        write_node = self._write_node_helper(instance)

        if write_node is None:
            self.log.warning(
                "Created node '{}' is missing write node!".format(
                    group_node.name()
                )
            )
            return

        # get colorspace and add to version data
        colorspace = napi.get_colorspace_from_node(write_node)

        if render_target == "frames":
            self._set_existing_files_data(instance, colorspace)

        elif render_target == "frames_farm":
            collected_frames = self._set_existing_files_data(
                instance, colorspace)

            self._set_expected_files(instance, collected_frames)

            self._add_farm_instance_data(instance)

        if render_target == "farm":
            self._add_farm_instance_data(instance)

        # set additional instance data
        self._set_additional_instance_data(instance, render_target, colorspace)

    def _set_existing_files_data(self, instance, colorspace):
        """Set existing files data to instance data.

        Args:
            instance (pyblish.api.Instance): pyblish instance
            colorspace (str): colorspace

        Returns:
            list: collected frames
        """
        collected_frames = self._get_collected_frames(instance)

        representation = self._get_existing_frames_representation(
            instance, collected_frames
        )

        # inject colorspace data
        self.set_representation_colorspace(
            representation, instance.context,
            colorspace=colorspace
        )

        instance.data["representations"].append(representation)

        return collected_frames

    def _set_expected_files(self, instance, collected_frames):
        """Set expected files to instance data.

        Args:
            instance (pyblish.api.Instance): pyblish instance
            collected_frames (list): collected frames
        """
        write_node = self._write_node_helper(instance)

        write_file_path = nuke.filename(write_node)
        output_dir = os.path.dirname(write_file_path)

        instance.data["expectedFiles"] = [
            os.path.join(output_dir, source_file)
            for source_file in collected_frames
        ]

    def _get_frame_range_data(self, instance):
        """Get frame range data from instance.

        Args:
            instance (pyblish.api.Instance): pyblish instance

        Returns:
            tuple: first_frame, last_frame
        """

        instance_name = instance.data["name"]

        if self._frame_ranges.get(instance_name):
            # return cashed write node
            return self._frame_ranges[instance_name]

        write_node = self._write_node_helper(instance)

        # Get frame range from workfile
        first_frame = int(nuke.root()["first_frame"].getValue())
        last_frame = int(nuke.root()["last_frame"].getValue())

        # Get frame range from write node if activated
        if write_node["use_limit"].getValue():
            first_frame = int(write_node["first"].getValue())
            last_frame = int(write_node["last"].getValue())

        # add to cache
        self._frame_ranges[instance_name] = (first_frame, last_frame)

        return first_frame, last_frame

    def _set_additional_instance_data(
        self, instance, render_target, colorspace
    ):
        """Set additional instance data.

        Args:
            instance (pyblish.api.Instance): pyblish instance
            render_target (str): render target
            colorspace (str): colorspace
        """
        product_type = instance.data["productType"]

        # add targeted family to families
        instance.data["families"].append(
            "{}.{}".format(product_type, render_target)
        )
        self.log.debug("Appending render target to families: {}.{}".format(
            product_type, render_target)
        )

        write_node = self._write_node_helper(instance)

        # Determine defined file type
        path = write_node["file"].value()
        ext = os.path.splitext(path)[1].lstrip(".")

        # determine defined channel type
        color_channels = write_node["channels"].value()

        # get frame range data
        handle_start = instance.context.data["handleStart"]
        handle_end = instance.context.data["handleEnd"]
        first_frame, last_frame = self._get_frame_range_data(instance)

        # get output paths
        write_file_path = nuke.filename(write_node)
        output_dir = os.path.dirname(write_file_path)

        # TODO: remove this when we have proper colorspace support
        version_data = {
            "colorspace": colorspace
        }

        instance.data.update({
            "versionData": version_data,
            "path": write_file_path,
            "outputDir": output_dir,
            "ext": ext,
            "colorspace": colorspace,
            "color_channels": color_channels
        })

        if product_type == "render":
            instance.data.update({
                "handleStart": handle_start,
                "handleEnd": handle_end,
                "frameStart": first_frame + handle_start,
                "frameEnd": last_frame - handle_end,
                "frameStartHandle": first_frame,
                "frameEndHandle": last_frame,
            })
        else:
            instance.data.update({
                "handleStart": 0,
                "handleEnd": 0,
                "frameStart": first_frame,
                "frameEnd": last_frame,
                "frameStartHandle": first_frame,
                "frameEndHandle": last_frame,
            })

    def _write_node_helper(self, instance):
        """Helper function to get write node from instance.

        Also sets instance transient data with child nodes.

        Args:
            instance (pyblish.api.Instance): pyblish instance

        Returns:
            nuke.Node: write node
        """
        instance_name = instance.data["name"]

        if self._write_nodes.get(instance_name):
            # return cashed write node
            return self._write_nodes[instance_name]

        # get all child nodes from group node
        child_nodes = napi.get_instance_group_node_childs(instance)

        # set child nodes to instance transient data
        instance.data["transientData"]["childNodes"] = child_nodes

        write_node = None
        for node_ in child_nodes:
            if node_.Class() == "Write":
                write_node = node_

        if write_node:
            # for slate frame extraction
            instance.data["transientData"]["writeNode"] = write_node
            # add to cache
            self._write_nodes[instance_name] = write_node

            return self._write_nodes[instance_name]

    def _get_existing_frames_representation(
        self,
        instance,
        collected_frames
    ):
        """Get existing frames representation.

        Args:
            instance (pyblish.api.Instance): pyblish instance
            collected_frames (list): collected frames

        Returns:
            dict: representation
        """

        first_frame, last_frame = self._get_frame_range_data(instance)

        write_node = self._write_node_helper(instance)

        write_file_path = nuke.filename(write_node)
        output_dir = os.path.dirname(write_file_path)

        # Determine defined file type
        path = write_node["file"].value()
        ext = os.path.splitext(path)[1].lstrip(".")

        representation = {
            "name": ext,
            "ext": ext,
            "stagingDir": output_dir,
            "tags": []
        }

        # set slate frame
        collected_frames = self._add_slate_frame_to_collected_frames(
            instance,
            collected_frames,
            first_frame,
            last_frame
        )

        if len(collected_frames) == 1:
            representation['files'] = collected_frames.pop()
        else:
            representation['files'] = collected_frames

        return representation

    def _get_frame_start_str(self, first_frame, last_frame):
        """Get frame start string.

        Args:
            first_frame (int): first frame
            last_frame (int): last frame

        Returns:
            str: frame start string
        """
        # convert first frame to string with padding
        return (
            "{{:0{}d}}".format(len(str(last_frame)))
        ).format(first_frame)

    def _add_slate_frame_to_collected_frames(
        self,
        instance,
        collected_frames,
        first_frame,
        last_frame
    ):
        """Add slate frame to collected frames.

        Args:
            instance (pyblish.api.Instance): pyblish instance
            collected_frames (list): collected frames
            first_frame (int): first frame
            last_frame (int): last frame

        Returns:
            list: collected frames
        """
        frame_start_str = self._get_frame_start_str(first_frame, last_frame)
        frame_length = int(last_frame - first_frame + 1)

        # this will only run if slate frame is not already
        # rendered from previews publishes
        if (
            "slate" in instance.data["families"]
            and frame_length == len(collected_frames)
        ):
            frame_slate_str = self._get_frame_start_str(
                first_frame - 1,
                last_frame
            )

            slate_frame = collected_frames[0].replace(
                frame_start_str, frame_slate_str)
            collected_frames.insert(0, slate_frame)

        return collected_frames

    def _add_farm_instance_data(self, instance):
        """Add farm publishing related instance data.

        Args:
            instance (pyblish.api.Instance): pyblish instance
        """

        # make sure rendered sequence on farm will
        # be used for extract review
        if not instance.data.get("review"):
            instance.data["useSequenceForReview"] = False

        # Farm rendering
        instance.data.update({
            "transfer": False,
            "farm": True  # to skip integrate
        })
        self.log.info("Farm rendering ON ...")

    def _get_collected_frames(self, instance):
        """Get collected frames.

        Args:
            instance (pyblish.api.Instance): pyblish instance

        Returns:
            list: collected frames
        """

        first_frame, last_frame = self._get_frame_range_data(instance)

        write_node = self._write_node_helper(instance)

        write_file_path = nuke.filename(write_node)
        output_dir = os.path.dirname(write_file_path)

        # get file path knob
        node_file_knob = write_node["file"]
        # list file paths based on input frames
        expected_paths = list(sorted({
            node_file_knob.evaluate(frame)
            for frame in range(first_frame, last_frame + 1)
        }))

        # convert only to base names
        expected_filenames = {
            os.path.basename(filepath)
            for filepath in expected_paths
        }

        # make sure files are existing at folder
        collected_frames = [
            filename
            for filename in os.listdir(output_dir)
            if filename in expected_filenames
        ]

        return collected_frames