Skip to content

nuke

plugin.workflow.applications.nuke

NukeRender

Bases: WorkflowTaskNode

Renders a Nuke scene.

Source code in client/ayon_workflow/plugins/workflow/applications/nuke.py
 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
class NukeRender(WorkflowTaskNode):
    """ Renders a Nuke scene.
    """
    version = "0.0.1"
    inputs = [
        InputAttribute(
            name="context",
            description="The render context.",
        ),
        InputAttribute(
            name="nuke_script_path",
            description="The path to the Nuke script.",
            widget={
                "name": "filepath",
                "caption": "Select a Nuke scene file",
                "filter": "Nuke Scene (*.nk)",
            },
        ),
        InputAttribute(
            name="input_media",
            description="The input media",
        ),
        InputAttribute(
            name="output_media",
            description="The output media",
        ),
        InputAttribute(
            name="python_script_path",
            description="The path to a python script.",
            widget={
                "name": "filepath",
                "caption": "Select a Python file",
                "filter": "Python Script (*.py)",
            },
        ),
        InputAttribute(
            name="frame_range",
            description="Restrictive frame range.",
        ),
        InputAttribute(
            name="read_node_name",
            description="Explicit a Read node to use.",
        ),
        InputAttribute(
            name="write_node_name",
            description="Explicit a Write node to use.",
        ),
        InputAttribute(
            name="nuke_application_variant",
            description="An application variant to use.",
        ),
        InputAttribute(
            name="log_file",
            description="Path to output logs.",
        ),
        InputAttribute(
            name="restrict_to_task",
            description="Raises if context is not a Task.",
        ),
    ]
    outputs = [
        OutputAttribute(
            name="rendered_media",
            description="The rendered media output.",
        ),
    ]

    def execute(
        self,
        context: ContextItem,
        nuke_script_path: str,
        input_media: MediaType,
        output_media: MediaType,
        python_script_path: Optional[str] = None,
        frame_range: Optional[Union[dict, FrameRange]] = None,
        read_node_name: Optional[str] = None,
        write_node_name: Optional[str] = None,
        nuke_application_variant: Optional[str] = None,
        log_file: Optional[str] = None,
        restrict_to_task: Optional[bool] = False,
    ) -> MediaType:

        if isinstance(frame_range, dict):
            frame_range = FrameRange(**frame_range)

        # Construct render command line args.
        app_args = ["-x"]
        if write_node_name:
            app_args = ["-X", write_node_name]

        if output_media.frame_range:
            frame_range = frame_range or output_media.frame_range

        if frame_range:
            app_args.extend(["-F", str(frame_range.format())])

        if python_script_path:
            python_script_path = _base.check_python_script_path(
                context.project_name,
                python_script_path,
            )
            temp_dir = None
        else:
            temp_dir, python_script_path = _base.get_temp_python_script_path(
                context.project_name,
                default_content=_default_py_render_logic(
                    read_node_name=read_node_name,
                    write_node_name=write_node_name,
                ),
                suffix_name="nuke_render"
            )
        app_args.append(python_script_path)

        if nuke_script_path:
            nuke_script_path = remap_input(
                nuke_script_path,
                context.project_name,
            )
            app_args.append(nuke_script_path)
        if input_media:
            input_media = remap_input(input_media, context.project_name)
            app_args.append(input_media.format())

        # TODO: make output_media optional and
        # identify media from resulting stdout instead.
        # assert the output_media exists
        remapped_output_media = remap_input(output_media, context.project_name)
        app_args.append(remapped_output_media.format())

        if log_file:
            # Ensure logfile is unique per frame chunk.
            if frame_range:
                log_file = (
                    f"{log_file}._chunk{frame_range.first_frame}"
                    f"_{frame_range.last_frame}"
                )

            log_file = remap_input(
                log_file,
                context.project_name,
            )

        # Start application.
        _ = _base.run_application(
            "nuke",
            context,
            app_args=app_args,
            app_application_variant=nuke_application_variant,
            log_file=log_file,
            temporary_directory=temp_dir,
            restrict_to_task=restrict_to_task,
        )

        # Ensure expected output_media exists.
        if isinstance(remapped_output_media, ImageSequence):  # img seq
            for path in remapped_output_media:
                if not os.path.exists(path):
                    raise RuntimeError(
                        f"Expected frame {path} does not exists."
                    )
        else:
            if not os.path.exists(remapped_output_media.path):  # video
                raise RuntimeError(
                    f"Expected media {remapped_output_media.path} "
                    "does not exist."
                )

        return output_media