Skip to content

submit_maya_deadline

Submitting render job to Deadline.

This module is taking care of submitting job from Maya to Deadline. It creates job and set correct environments. Its behavior is controlled by DEADLINE_REST_URL environment variable - pointing to Deadline Web Service and :data:PublishDeadlineJobInfo.use_published property telling Deadline to use published scene workfile or not.

If vrscene or assscene are detected in families, it will first submit job to export these files and then dependent job to render them.

Attributes:

Name Type Description
payload_skeleton dict

Skeleton payload data sent as job to Deadline. Default values are for MayaBatch plugin.

MayaSubmitDeadline

Bases: AbstractSubmitDeadline, AYONPyblishPluginMixin

Source code in client/ayon_deadline/plugins/publish/maya/submit_maya_deadline.py
 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
class MayaSubmitDeadline(abstract_submit_deadline.AbstractSubmitDeadline,
                         AYONPyblishPluginMixin):

    label = "Submit Render to Deadline"
    hosts = ["maya"]
    families = ["renderlayer"]
    targets = ["local"]
    settings_category = "deadline"

    strict_error_checking = False

    tile_assembler_plugin = "DraftTileAssembler"
    tile_priority = 50

    @classmethod
    def get_attribute_defs(cls):
        return [
            NumberDef(
                "tile_priority",
                label="Tile Assembler Priority",
                decimals=0,
                default=cls.tile_priority
            ),
            BoolDef(
                "strict_error_checking",
                label="Strict Error Checking",
                default=cls.strict_error_checking
            ),
        ]

    def get_job_info(self, job_info=None):
        instance = self._instance

        job_info.Plugin = instance.data.get("mayaRenderPlugin", "MayaBatch")

        # Deadline requires integers in frame range
        frames = "{start}-{end}x{step}".format(
            start=int(instance.data["frameStartHandle"]),
            end=int(instance.data["frameEndHandle"]),
            step=int(instance.data["byFrameStep"]),
        )
        job_info.Frames = frames

        return job_info

    def get_plugin_info(self):
        # Not all hosts can import this module.
        from maya import cmds

        instance = self._instance
        context = instance.context

        # Set it to default Maya behaviour if it cannot be determined
        # from instance (but it should be, by the Collector).

        default_rs_include_lights = (
            instance.context.data['project_settings']
                                 ['maya']
                                 ['render_settings']
                                 ['enable_all_lights']
        )

        rs_include_lights = instance.data.get(
            "renderSetupIncludeLights", default_rs_include_lights)
        if rs_include_lights not in {"1", "0", True, False}:
            rs_include_lights = default_rs_include_lights

        attr_values = self.get_attr_values_from_data(instance.data)
        strict_error_checking = attr_values.get("strict_error_checking",
                                                self.strict_error_checking)
        plugin_info = MayaPluginInfo(
            SceneFile=self.scene_path,
            Version=cmds.about(version=True),
            RenderLayer=instance.data['setMembers'],
            Renderer=instance.data["renderer"],
            RenderSetupIncludeLights=rs_include_lights,  # noqa
            ProjectPath=context.data["workspaceDir"],
            UsingRenderLayers=True,
            StrictErrorChecking=strict_error_checking
        )

        plugin_payload = asdict(plugin_info)

        return plugin_payload

    def process_submission(self):
        from maya import cmds
        instance = self._instance

        filepath = self.scene_path  # publish if `use_publish` else workfile

        # TODO: Avoid the need for this logic here, needed for submit publish
        # Store output dir for unified publisher (filesequence)
        expected_files = instance.data["expectedFiles"]
        first_file = next(iter_expected_files(expected_files))
        output_dir = os.path.dirname(first_file)
        instance.data["outputDir"] = output_dir

        # Patch workfile (only when 'use_published' is enabled)
        if self.job_info.use_published:
            self._patch_workfile()

        # Gather needed data ------------------------------------------------
        filename = os.path.basename(filepath)
        dirname = os.path.join(
            cmds.workspace(query=True, rootDirectory=True),
            cmds.workspace(fileRuleEntry="images")
        )

        # Fill in common data to payload ------------------------------------
        # TODO: Replace these with collected data from CollectRender
        payload_data = {
            "filename": filename,
            "dirname": dirname,
        }

        # Submit preceding export jobs -------------------------------------
        export_job = None
        assert not all(x in instance.data["families"]
                       for x in ['vrayscene', 'assscene']), (
            "Vray Scene and Ass Scene options are mutually exclusive")

        auth = self._instance.data["deadline"]["auth"]
        verify = self._instance.data["deadline"]["verify"]
        if "vrayscene" in instance.data["families"]:
            self.log.debug("Submitting V-Ray scene render..")
            vray_export_payload = self._get_vray_export_payload(payload_data)
            export_job = self.submit(vray_export_payload,
                                     auth=auth,
                                     verify=verify)

            payload = self._get_vray_render_payload(payload_data)

        else:
            self.log.debug("Submitting MayaBatch render..")
            payload = self._get_maya_payload(payload_data)

        # Add export job as dependency --------------------------------------
        if export_job:
            job_info, _ = payload
            job_info.JobDependencies.append(export_job)

        if instance.data.get("tileRendering"):
            # Prepare tiles data
            self._tile_render(payload)
        else:
            # Submit main render job
            job_info, plugin_info = payload
            self.submit(self.assemble_payload(job_info, plugin_info),
                        auth=auth,
                        verify=verify)

    def _tile_render(self, payload):
        """Submit as tile render per frame with dependent assembly jobs."""

        # As collected by super process()
        instance = self._instance

        payload_job_info, payload_plugin_info = payload
        job_info = copy.deepcopy(payload_job_info)
        plugin_info = copy.deepcopy(payload_plugin_info)

        # Force plugin reload for vray cause the region does not get flushed
        # between tile renders.
        if plugin_info["Renderer"] == "vray":
            job_info.ForceReloadPlugin = True

        # if we have sequence of files, we need to create tile job for
        # every frame
        job_info.TileJob = True
        job_info.TileJobTilesInX = instance.data.get("tilesX")
        job_info.TileJobTilesInY = instance.data.get("tilesY")

        tiles_count = job_info.TileJobTilesInX * job_info.TileJobTilesInY

        plugin_info["ImageHeight"] = instance.data.get("resolutionHeight")
        plugin_info["ImageWidth"] = instance.data.get("resolutionWidth")
        plugin_info["RegionRendering"] = True

        R_FRAME_NUMBER = re.compile(
            r".+\.(?P<frame>[0-9]+)\..+")  # noqa: N806, E501
        REPL_FRAME_NUMBER = re.compile(
            r"(.+\.)([0-9]+)(\..+)")  # noqa: N806, E501

        exp = instance.data["expectedFiles"]
        if isinstance(exp[0], dict):
            # we have aovs and we need to iterate over them
            # get files from `beauty`
            files = exp[0].get("beauty")
            # assembly files are used for assembly jobs as we need to put
            # together all AOVs
            assembly_files = list(
                itertools.chain.from_iterable(
                    [f for _, f in exp[0].items()]))
            if not files:
                # if beauty doesn't exist, use first aov we found
                files = exp[0].get(list(exp[0].keys())[0])
        else:
            files = exp
            assembly_files = files

        auth = instance.data["deadline"]["auth"]
        verify = instance.data["deadline"]["verify"]

        # Define frame tile jobs
        frame_file_hash = {}
        frame_payloads = {}
        file_index = 1
        for file in files:
            frame = re.search(R_FRAME_NUMBER, file).group("frame")

            new_job_info = copy.deepcopy(job_info)
            new_job_info.Name += " (Frame {} - {} tiles)".format(frame,
                                                                 tiles_count)
            new_job_info.TileJobFrame = frame

            new_plugin_info = copy.deepcopy(plugin_info)

            # Add tile data into job info and plugin info
            tiles_data = _format_tiles(
                file, 0,
                instance.data.get("tilesX"),
                instance.data.get("tilesY"),
                instance.data.get("resolutionWidth"),
                instance.data.get("resolutionHeight"),
                payload_plugin_info["OutputFilePrefix"]
            )[0]

            new_job_info.update(tiles_data["JobInfo"])
            new_plugin_info.update(tiles_data["PluginInfo"])

            self.log.debug("hashing {} - {}".format(file_index, file))
            job_hash = hashlib.sha256(
                ("{}_{}".format(file_index, file)).encode("utf-8"))

            file_hash = job_hash.hexdigest()
            frame_file_hash[frame] = file_hash

            new_job_info.ExtraInfo[0] = file_hash
            new_job_info.ExtraInfo[1] = file

            frame_payloads[frame] = self.assemble_payload(
                job_info=new_job_info,
                plugin_info=new_plugin_info
            )
            file_index += 1

        self.log.debug(
            "Submitting tile job(s) [{}] ...".format(len(frame_payloads)))

        # Submit frame tile jobs
        frame_tile_job_id = {}
        for frame, tile_job_payload in frame_payloads.items():
            job_id = self.submit(
                tile_job_payload, auth, verify)
            frame_tile_job_id[frame] = job_id

        # Define assembly payloads
        assembly_job_info = copy.deepcopy(job_info)
        assembly_job_info.Plugin = self.tile_assembler_plugin
        assembly_job_info.Name += " - Tile Assembly Job"
        assembly_job_info.Frames = 1
        assembly_job_info.MachineLimit = 1

        attr_values = self.get_attr_values_from_data(instance.data)
        assembly_job_info.Priority = attr_values.get("tile_priority",
                                                     self.tile_priority)
        assembly_job_info.TileJob = False

        assembly_job_info.Pool = self.job_info.Pool

        assembly_plugin_info = {
            "CleanupTiles": 1,
            "ErrorOnMissing": True,
            "Renderer": self._instance.data["renderer"]
        }

        assembly_payloads = []
        output_dir = self.job_info.OutputDirectory[0]
        config_files = []
        for file in assembly_files:
            frame = re.search(R_FRAME_NUMBER, file).group("frame")

            frame_assembly_job_info = copy.deepcopy(assembly_job_info)
            frame_assembly_job_info.Name += " (Frame {})".format(frame)
            frame_assembly_job_info.OutputFilename[0] = re.sub(
                REPL_FRAME_NUMBER,
                "\\1{}\\3".format("#" * len(frame)), file)

            file_hash = frame_file_hash[frame]
            tile_job_id = frame_tile_job_id[frame]

            frame_assembly_job_info.ExtraInfo[0] = file_hash
            frame_assembly_job_info.ExtraInfo[1] = file
            frame_assembly_job_info.JobDependencies.append(tile_job_id)
            frame_assembly_job_info.Frames = frame

            # write assembly job config files
            config_file = os.path.join(
                output_dir,
                "{}_config_{}.txt".format(
                    os.path.splitext(file)[0],
                    datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
                )
            )
            config_files.append(config_file)
            try:
                if not os.path.isdir(output_dir):
                    os.makedirs(output_dir)
            except OSError:
                # directory is not available
                self.log.warning("Path is unreachable: "
                                 "`{}`".format(output_dir))

            with open(config_file, "w") as cf:
                print("TileCount={}".format(tiles_count), file=cf)
                print("ImageFileName={}".format(file), file=cf)
                print("ImageWidth={}".format(
                    instance.data.get("resolutionWidth")), file=cf)
                print("ImageHeight={}".format(
                    instance.data.get("resolutionHeight")), file=cf)

            reversed_y = False
            if plugin_info["Renderer"] == "arnold":
                reversed_y = True

            with open(config_file, "a") as cf:
                # Need to reverse the order of the y tiles, because image
                # coordinates are calculated from bottom left corner.
                tiles = _format_tiles(
                    file, 0,
                    instance.data.get("tilesX"),
                    instance.data.get("tilesY"),
                    instance.data.get("resolutionWidth"),
                    instance.data.get("resolutionHeight"),
                    payload_plugin_info["OutputFilePrefix"],
                    reversed_y=reversed_y
                )[1]
                for k, v in sorted(tiles.items()):
                    print("{}={}".format(k, v), file=cf)

            assembly_payloads.append(
                self.assemble_payload(
                    job_info=frame_assembly_job_info,
                    plugin_info=assembly_plugin_info.copy(),
                    # This would fail if the client machine and webserice are
                    # using different storage paths.
                    aux_files=[config_file]
                )
            )

        # Submit assembly jobs
        assembly_job_ids = []
        num_assemblies = len(assembly_payloads)
        for i, payload in enumerate(assembly_payloads):
            self.log.debug(
                "submitting assembly job {} of {}".format(i + 1,
                                                          num_assemblies)
            )
            assembly_job_id = self.submit(
                payload,
                auth=auth,
                verify=verify
            )
            assembly_job_ids.append(assembly_job_id)

        instance.data["assemblySubmissionJobs"] = assembly_job_ids

        # Remove config files to avoid confusion about where data is coming
        # from in Deadline.
        for config_file in config_files:
            os.remove(config_file)

    def _get_maya_payload(self, data):

        job_info = copy.deepcopy(self.job_info)
        if not is_in_tests() and self.job_info.use_asset_dependencies:
            # Asset dependency to wait for at least the scene file to sync.
            job_info.AssetDependency += self.scene_path

        # Get layer prefix
        renderlayer = self._instance.data["setMembers"]
        renderer = self._instance.data["renderer"]
        layer_prefix_attr = RenderSettings.get_image_prefix_attr(renderer)
        layer_prefix = get_attr_in_layer(layer_prefix_attr, layer=renderlayer)

        plugin_info = copy.deepcopy(self.plugin_info)
        plugin_info.update({
            # Output directory and filename
            "OutputFilePath": data["dirname"].replace("\\", "/"),
            "OutputFilePrefix": layer_prefix,
        })

        # This hack is here because of how Deadline handles Renderman version.
        # it considers everything with `renderman` set as version older than
        # Renderman 22, and so if we are using renderman > 21 we need to set
        # renderer string on the job to `renderman22`. We will have to change
        # this when Deadline releases new version handling this.
        renderer = self._instance.data["renderer"]
        if renderer == "renderman":
            try:
                from rfm2.config import cfg  # noqa
            except ImportError:
                raise Exception("Cannot determine renderman version")

            rman_version = cfg().build_info.version()  # type: str
            if int(rman_version.split(".")[0]) > 22:
                renderer = "renderman22"

            plugin_info["Renderer"] = renderer

            # this is needed because renderman plugin in Deadline
            # handles directory and file prefixes separately
            plugin_info["OutputFilePath"] = job_info.OutputDirectory[0]

        return job_info, plugin_info

    def _get_vray_export_payload(self, data):

        job_info = copy.deepcopy(self.job_info)
        job_info.Name = self._job_info_label("Export")

        # Get V-Ray settings info to compute output path
        vray_scene = self.format_vray_output_filename()

        plugin_info = {
            "Renderer": "vray",
            "SkipExistingFrames": True,
            "UseLegacyRenderLayers": True,
            "OutputFilePath": os.path.dirname(vray_scene)
        }

        return job_info, asdict(plugin_info)

    def _get_vray_render_payload(self, data):

        # Job Info
        job_info = copy.deepcopy(self.job_info)
        job_info.Name = self._job_info_label("Render")
        job_info.Plugin = "Vray"
        job_info.OverrideTaskExtraInfoNames = False

        # Plugin Info
        plugin_info = VRayPluginInfo(
            InputFilename=self.format_vray_output_filename(),
            SeparateFilesPerFrame=False,
            VRayEngine="V-Ray",
            Width=self._instance.data["resolutionWidth"],
            Height=self._instance.data["resolutionHeight"],
            OutputFilePath=job_info.OutputDirectory[0],
            OutputFileName=job_info.OutputFilename[0]
        )

        return job_info, asdict(plugin_info)

    def _get_arnold_render_payload(self, data):
        # Job Info
        job_info = copy.deepcopy(self.job_info)
        job_info.Name = self._job_info_label("Render")
        job_info.Plugin = "Arnold"
        job_info.OverrideTaskExtraInfoNames = False

        # Plugin Info
        ass_file, _ = os.path.splitext(data["output_filename_0"])
        ass_filepath = ass_file + ".ass"

        plugin_info = ArnoldPluginInfo(
            ArnoldFile=ass_filepath
        )

        return job_info, asdict(plugin_info)

    def format_vray_output_filename(self):
        """Format the expected output file of the Export job.

        Example:
            <Scene>/<Scene>_<Layer>/<Layer>
            "shot010_v006/shot010_v006_CHARS/CHARS_0001.vrscene"
        Returns:
            str

        """
        from maya import cmds
        # "vrayscene/<Scene>/<Scene>_<Layer>/<Layer>"
        vray_settings = cmds.ls(type="VRaySettingsNode")
        node = vray_settings[0]
        template = cmds.getAttr("{}.vrscene_filename".format(node))
        scene, _ = os.path.splitext(self.scene_path)

        def smart_replace(string, key_values):
            new_string = string
            for key, value in key_values.items():
                new_string = new_string.replace(key, value)
            return new_string

        # Get workfile scene path without extension to format vrscene_filename
        scene_filename = os.path.basename(self.scene_path)
        scene_filename_no_ext, _ = os.path.splitext(scene_filename)

        layer = self._instance.data['setMembers']

        # Reformat without tokens
        output_path = smart_replace(
            template,
            {"<Scene>": scene_filename_no_ext,
             "<Layer>": layer})

        start_frame = int(self._instance.data["frameStartHandle"])
        workspace = self._instance.context.data["workspace"]
        filename_zero = "{}_{:04d}.vrscene".format(output_path, start_frame)
        filepath_zero = os.path.join(workspace, filename_zero)

        return filepath_zero.replace("\\", "/")

    def _patch_workfile(self):
        """Patch Maya scene.

        This will take list of patches (lines to add) and apply them to
        *published* Maya  scene file (that is used later for rendering).

        Patches are dict with following structure::
            {
                "name": "Name of patch",
                "regex": "regex of line before patch",
                "line": "line to insert"
            }

        """
        project_settings = self._instance.context.data["project_settings"]
        patches = (
            project_settings.get(
                "deadline", {}).get(
                "publish", {}).get(
                "MayaSubmitDeadline", {}).get(
                "scene_patches", {})
        )
        if not patches:
            return

        if not os.path.splitext(self.scene_path)[1].lower() != ".ma":
            self.log.debug("Skipping workfile patch since workfile is not "
                           ".ma file")
            return

        compiled_regex = [re.compile(p["regex"]) for p in patches]
        with open(self.scene_path, "r+") as pf:
            scene_data = pf.readlines()
            for ln, line in enumerate(scene_data):
                for i, r in enumerate(compiled_regex):
                    if re.match(r, line):
                        scene_data.insert(ln + 1, patches[i]["line"])
                        pf.seek(0)
                        pf.writelines(scene_data)
                        pf.truncate()
                        self.log.info("Applied {} patch to scene.".format(
                            patches[i]["name"]
                        ))

    def _job_info_label(self, label):
        return "{label} {job.Name} [{start}-{end}]".format(
            label=label,
            job=self.job_info,
            start=int(self._instance.data["frameStartHandle"]),
            end=int(self._instance.data["frameEndHandle"]),
        )

format_vray_output_filename()

Format the expected output file of the Export job.

Example

/_/ "shot010_v006/shot010_v006_CHARS/CHARS_0001.vrscene"

Returns: str

Source code in client/ayon_deadline/plugins/publish/maya/submit_maya_deadline.py
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
def format_vray_output_filename(self):
    """Format the expected output file of the Export job.

    Example:
        <Scene>/<Scene>_<Layer>/<Layer>
        "shot010_v006/shot010_v006_CHARS/CHARS_0001.vrscene"
    Returns:
        str

    """
    from maya import cmds
    # "vrayscene/<Scene>/<Scene>_<Layer>/<Layer>"
    vray_settings = cmds.ls(type="VRaySettingsNode")
    node = vray_settings[0]
    template = cmds.getAttr("{}.vrscene_filename".format(node))
    scene, _ = os.path.splitext(self.scene_path)

    def smart_replace(string, key_values):
        new_string = string
        for key, value in key_values.items():
            new_string = new_string.replace(key, value)
        return new_string

    # Get workfile scene path without extension to format vrscene_filename
    scene_filename = os.path.basename(self.scene_path)
    scene_filename_no_ext, _ = os.path.splitext(scene_filename)

    layer = self._instance.data['setMembers']

    # Reformat without tokens
    output_path = smart_replace(
        template,
        {"<Scene>": scene_filename_no_ext,
         "<Layer>": layer})

    start_frame = int(self._instance.data["frameStartHandle"])
    workspace = self._instance.context.data["workspace"]
    filename_zero = "{}_{:04d}.vrscene".format(output_path, start_frame)
    filepath_zero = os.path.join(workspace, filename_zero)

    return filepath_zero.replace("\\", "/")