Skip to content

blender_render

plugin.workflow.applications.blender_render

BlenderRender

Bases: WorkflowTaskNode

Renders a Blender scene using a given script and input resource.

Source code in client/ayon_workflow/plugins/workflow/applications/blender_render.py
 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
class BlenderRender(WorkflowTaskNode):
    """ Renders a Blender scene using a given script and input resource.
    """
    version = "0.0.1"
    inputs = [
        InputAttribute(
            name="context",
            description="The render context.",
        ),
        InputAttribute(
            name="blender_script_path",
            description="The path to the Blender script.",
            widget={
                "name": "filepath",
                "caption": "Select a Blender scene file",
                "filter": "Blender Scene (*.blend)",
            },
        ),
        InputAttribute(
            name="input_resource_path",
            description="A path to an input resource",
            widget={
                "name": "filepath",
            },
        ),
        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="blender_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,
        blender_script_path: str,
        output_media: ImageSequence,
        input_resource_path: Optional[str] = None,
        python_script_path: Optional[str] = None,
        frame_range: Optional[Union[dict, FrameRange]] = None,
        blender_application_variant: Optional[str] = None,
        log_file: Optional[str] = None,
        restrict_to_task: Optional[bool] = False,
    ) -> ImageSequence:
        # Construct render command line args.
        blender_script_path = remap_input(
            blender_script_path,
            context.project_name,
        )

        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(),
                suffix_name="blender_render"
            )

        app_args = [
            "-b",
            "--python-exit-code", "1",  # ensure any exception in python raises
            "-P", python_script_path,
            "--",
            "--blend_file",
            blender_script_path,
        ]

        # Add process-specific args
        if output_media.frame_range:
            frame_range = frame_range or output_media.frame_range

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

            app_args.extend(
                [
                    "--start",
                    str(frame_range.first_frame),
                    "--end",
                    str(frame_range.last_frame),
                ]
            )

        remapped_output_media = remap_input(output_media, context.project_name)
        app_args.extend(
            [
                "--output_path",
                os.path.join(
                    remapped_output_media.directory,
                    f"{remapped_output_media.head}"
                )
            ]
        )

        if input_resource_path:
            input_resource_path = remap_input(
                input_resource_path,
                context.project_name,
            )
            app_args.extend(
                [
                    "--input_path",
                    input_resource_path,
                ]
            )

        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(
            "blender",
            context,
            app_args=app_args,
            app_application_variant=blender_application_variant,
            log_file=log_file,
            temporary_directory=temp_dir,
            restrict_to_task=restrict_to_task,
        )

        # Ensure expected output_media exists.
        for path in remapped_output_media:
            if not os.path.exists(path):
                raise RuntimeError(f"Expected frame {path} does not exists.")

        # TODO: make output_media optional and
        # identify media from resulting stdout instead.
        return output_media