Skip to content

exporters

extract_alembic(filepath, frame_start=None, frame_end=None, frame_step=1, sub_frames=1, global_matrix=False, selection=True, doc=None, verbose=False, **kwargs)

Extract a single Alembic Cache.

Source code in client/ayon_cinema4d/api/exporters.py
 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
def extract_alembic(filepath,
                    frame_start=None,
                    frame_end=None,
                    frame_step=1,
                    sub_frames=1,
                    global_matrix=False,
                    selection=True,
                    doc=None,
                    verbose=False,
                    **kwargs):
    """Extract a single Alembic Cache."""
    doc = doc or c4d.documents.GetActiveDocument()

    # Fallback to Cinema4d timeline if no start or end frame provided.
    if frame_start is None:
        frame_start = doc.GetMinTime().GetFrame(doc.GetFps())
    if frame_end is None:
        frame_end = doc.GetMinTime().GetFrame(doc.GetFps())

    # Set export options
    options = get_plugin_imexport_options(c4d.FORMAT_ABCEXPORT,
                                          label="Alembic")

    applied_options = {
        # Animation
        "ABCEXPORT_FRAME_START": frame_start,
        "ABCEXPORT_FRAME_END": frame_end,
        "ABCEXPORT_FRAME_STEP": frame_step,
        "ABCEXPORT_SUBFRAMES": sub_frames,

        # General
        # "ABCEXPORT_SCALE": 1  # "UnitScaleData
        "ABCEXPORT_SELECTION_ONLY": selection,
        "ABCEXPORT_CAMERAS": kwargs.get("cameras", True),
        "ABCEXPORT_SPLINES": kwargs.get("splines", False),
        "ABCEXPORT_HAIR": kwargs.get("hair", False),
        "ABCEXPORT_XREFS": kwargs.get("xrefs", True),
        "ABCEXPORT_GLOBAL_MATRIX": global_matrix,

        # Subdivision surface
        "ABCEXPORT_HYPERNURBS": kwargs.get(
            "subdivisionSurfaces", True
        ),
        "ABCEXPORT_SDS_WEIGHTS": kwargs.get(
            "subdivisionSurfaceWeights", False
        ),
        "ABCEXPORT_PARTICLES": kwargs.get("particles", False),
        "ABCEXPORT_PARTICLE_GEOMETRY": kwargs.get(
            "particleGeometry", False
        ),

        # Optional data
        "ABCEXPORT_VISIBILITY": kwargs.get("visibility", True),
        "ABCEXPORT_UVS": kwargs.get("uvs", True),
        "ABCEXPORT_VERTEX_MAPS": kwargs.get("vertexMaps", False),

        # Vertex normals
        "ABCEXPORT_NORMALS": kwargs.get("normals", False),
        "ABCEXPORT_POLYGONSELECTIONS": kwargs.get("polygonSelections", True),
        "ABCEXPORT_VERTEX_COLORS": kwargs.get("vertexColors", False),
        "ABCEXPORT_POINTS_ONLY": kwargs.get("pointsOnly", False),
        "ABCEXPORT_DISPLAY_COLORS": kwargs.get("displayColors", False),
        "ABCEXPORT_MERGE_CACHE": kwargs.get("mergeCache", False)

        # "ABCEXPORT_GROUP": None,  # ???
        # # Don't export child objects with only selected?
        # "ABCEXPORT_PARENTS_ONLY_MODE": False,
        # "ABCEXPORT_STR_ANIMATION": None,  # ???
        # "ABCEXPORT_STR_GENERAL": None,  # ???
        # "ABCEXPORT_STR_OPTIONS": None,  # ???
    }
    if verbose:
        log.debug(
            "Preparing Alembic export with options: %s",
            json.dumps(applied_options, indent=4),
        )

    for key, value in applied_options.items():
        key_id = getattr(c4d, key)
        # There appears to be a bug where if the value is just set directly
        # that it fails to apply them for the export, e.g. still exporting the
        # whole scene even though `c4d.ABCEXPORT_SELECTION_ONLY` is True.
        # See: https://developers.maxon.net/forum/topic/12767/alembic-export-options-not-working/6  # noqa: E501
        options[key_id] = not value
        options[key_id] = value

    # Ensure output directory exists
    parent_dir = os.path.dirname(filepath)
    os.makedirs(parent_dir, exist_ok=True)

    if c4d.documents.SaveDocument(
        doc,
        filepath,
        c4d.SAVEDOCUMENTFLAGS_DONTADDTORECENTLIST,
        c4d.FORMAT_ABCEXPORT,
    ):
        if verbose:
            log.debug("Extracted Alembic to: %s", filepath)
    else:
        log.error("Extraction of Alembic failed: %s", filepath)

    return filepath

extract_fbx(filepath, verbose=False, **kwargs)

Extract a single fbx file.

Source code in client/ayon_cinema4d/api/exporters.py
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
def extract_fbx(filepath, verbose=False, **kwargs):
    """Extract a single fbx file."""

    doc = c4d.documents.GetActiveDocument()
    options = get_plugin_imexport_options(FBX_EXPORTER_ID,
                                                     label="FBX")

    # File format
    options[c4d.FBXEXPORT_FBX_VERSION] = kwargs.get("fbxVersion", 0)
    options[c4d.FBXEXPORT_ASCII] = kwargs.get("fbxAscii", False)

    # General
    options[c4d.FBXEXPORT_SELECTION_ONLY] = kwargs.get("selectionOnly", False)
    options[c4d.FBXEXPORT_CAMERAS] = kwargs.get("cameras", True)
    options[c4d.FBXEXPORT_SPLINES] = kwargs.get("splines", True)
    options[c4d.FBXEXPORT_INSTANCES] = kwargs.get("instances", True)
    options[c4d.FBXEXPORT_GLOBAL_MATRIX] = kwargs.get("globalMatrix", False)
    options[c4d.FBXEXPORT_SDS] = kwargs.get("subdivisionSurfaces", True)
    options[c4d.FBXEXPORT_LIGHTS] = kwargs.get("lights", True)

    # Animation
    options[c4d.FBXEXPORT_TRACKS] = kwargs.get("tracks", False)
    options[c4d.FBXEXPORT_BAKE_ALL_FRAMES] = kwargs.get(
        "bakeAllFrames", False
    )
    options[c4d.FBXEXPORT_PLA_TO_VERTEXCACHE] = kwargs.get(
        "plaToVertexCache", False
    )

    # Geometry
    options[c4d.FBXEXPORT_SAVE_NORMALS] = kwargs.get("normals", False)
    options[c4d.FBXEXPORT_SAVE_VERTEX_MAPS_AS_COLORS] = kwargs.get(
        "vertexMapsAsColors", False
    )
    options[c4d.FBXEXPORT_SAVE_VERTEX_COLORS] = kwargs.get(
        "vertexColors", False
    )
    options[c4d.FBXEXPORT_TRIANGULATE] = kwargs.get("triangulate", False)
    options[c4d.FBXEXPORT_SDS_SUBDIVISION] = kwargs.get(
        "bakedSubdivisionSurfaces", False
    )
    options[c4d.FBXEXPORT_LOD_SUFFIX] = kwargs.get("lodSuffix", False)

    # Additional
    if hasattr(c4d, "FBXEXPORT_TEXTURES"):
        # Cinema4d S22 doesn't have this option anymore
        options[c4d.FBXEXPORT_TEXTURES] = kwargs.get("textures", False)
    if hasattr(c4d, "FBXEXPORT_BAKE_MATERIALS"):
        # Cinema4d S22 now has the ability to bake materials
        options[c4d.FBXEXPORT_BAKE_MATERIALS] = kwargs.get(
            "bakeMaterials", False
        )
    options[c4d.FBXEXPORT_EMBED_TEXTURES] = kwargs.get(
        "embedTextures", False
    )
    options[c4d.FBXEXPORT_FLIP_Z_AXIS] = kwargs.get("flipZAxis", False)
    options[c4d.FBXEXPORT_SUBSTANCES] = kwargs.get("substances", False)
    options[c4d.FBXEXPORT_UP_AXIS] = kwargs.get(
        "upAxis", c4d.FBXEXPORT_UP_AXIS_Y
    )

    # Ensure output directory exists
    parent_dir = os.path.dirname(filepath)
    if not os.path.exists(parent_dir):
        os.makedirs(parent_dir)

    if verbose:
        log.debug(
            "Preparing FBX export with options: %s",
            json.dumps(kwargs, indent=4),
        )

    if c4d.documents.SaveDocument(
        doc,
        filepath,
        c4d.SAVEDOCUMENTFLAGS_DONTADDTORECENTLIST,
        FBX_EXPORTER_ID,
    ):
        if verbose:
            log.debug("Extracted FBX to: %s", filepath)
    else:
        log.error("Extraction of FBX failed: %s", filepath)

    return filepath

extract_redshiftproxy(filepath, frame_start=None, frame_end=None, frame_step=1, selection=True, export_lights=True, export_compress=True, export_polygon_connectivity=False, doc=None, verbose=False)

Extract a Redshift Proxy.

Source code in client/ayon_cinema4d/api/exporters.py
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
def extract_redshiftproxy(
        filepath,
        frame_start=None,
        frame_end=None,
        frame_step=1,
        selection=True,
        export_lights=True,
        export_compress=True,
        export_polygon_connectivity=False,
        doc=None,
        verbose=False):
    """Extract a Redshift Proxy."""

    # Redshift may not be available so we import here
    import redshift  # noqa

    doc = doc or c4d.documents.GetActiveDocument()

    # Fallback to Cinema4d timeline if no start or end frame provided.
    if frame_start is None:
        frame_start = doc.GetMinTime().GetFrame(doc.GetFps())
    if frame_end is None:
        frame_end = doc.GetMinTime().GetFrame(doc.GetFps())

    # Export at default 1cm scale
    scale = c4d.UnitScaleData()
    scale.SetUnitScale(1.0, c4d.DOCUMENT_UNIT_CM)

    # Set export options
    options = get_plugin_imexport_options(redshift.Frsproxyexport,
                                          label="Alembic")

    applied_options = {
        "REDSHIFT_PROXYEXPORT_ANIMATION_FRAME_END": frame_end,
        "REDSHIFT_PROXYEXPORT_ANIMATION_FRAME_START": frame_start,
        "REDSHIFT_PROXYEXPORT_ANIMATION_FRAME_STEP": frame_step,
        "REDSHIFT_PROXYEXPORT_ANIMATION_RANGE": c4d.REDSHIFT_PROXYEXPORT_ANIMATION_RANGE_MANUAL,
        "REDSHIFT_PROXYEXPORT_EXPORT_COMPRESS": export_compress,
        "REDSHIFT_PROXYEXPORT_EXPORT_LIGHTS": export_lights,
        "REDSHIFT_PROXYEXPORT_EXPORT_POLYGON_CONNECTIVITY": export_polygon_connectivity,
        "REDSHIFT_PROXYEXPORT_OBJECTS": (
            c4d.REDSHIFT_PROXYEXPORT_OBJECTS_SELECTION if selection
            else c4d.REDSHIFT_PROXYEXPORT_OBJECTS_ALL
        ),

        # Proxy Origin:
        #   - World Origin: REDSHIFT_PROXYEXPORT_ORIGIN_WORLD
        #   - Object Bounds: REDSHIFT_PROXYEXPORT_ORIGIN_OBJECTS
        "REDSHIFT_PROXYEXPORT_ORIGIN": c4d.REDSHIFT_PROXYEXPORT_ORIGIN_WORLD,

        # Include default beauty AOV
        # Keep the default beauty config in the proxy. Used primarily when
        # exporting entire scenes for rendering with the redshiftCmdLine tool
        "REDSHIFT_PROXYEXPORT_AOV_DEFAULT_BEAUTY": False,

        "REDSHIFT_PROXYEXPORT_AUTOPROXY_CREATE": False,
        # "REDSHIFT_PROXYEXPORT_AUTOPROXY_PREFIX": "RS Proxy",

        # Do not remove the exported objects
        "REDSHIFT_PROXYEXPORT_REMOVE_OBJECTS": False,

        "REDSHIFT_PROXYEXPORT_SCALE": scale,

        # TODO: Set more parameters
        # "REDSHIFT_PROXYEXPORT_GROUP": ...,
        # "REDSHIFT_PROXYEXPORT_GROUP_ANIMATION": ...,
        # "REDSHIFT_PROXYEXPORT_GROUP_AOV": ...,
        # "REDSHIFT_PROXYEXPORT_GROUP_AUTOPROXY": ...,
        # "REDSHIFT_PROXYEXPORT_GROUP_OPTIONS": ...,
    }
    if verbose:
        log.debug(
            "Preparing Redshift Proxy export with options: %s",
            json.dumps(applied_options, indent=4, default=str),
        )

    for key, value in applied_options.items():
        key_id = getattr(c4d, key)
        # There appears to be a bug where if the value is just set directly
        # that it fails to apply them for the export, e.g. still exporting the
        # whole scene even though `c4d.ABCEXPORT_SELECTION_ONLY` is True.
        # See: https://developers.maxon.net/forum/topic/12767/alembic-export-options-not-working/6  # noqa: E501
        if isinstance(value, (bool, int)):
            options[key_id] = not value
        options[key_id] = value

    # Ensure output directory exists
    parent_dir = os.path.dirname(filepath)
    os.makedirs(parent_dir, exist_ok=True)

    if c4d.documents.SaveDocument(
        doc,
        filepath,
        c4d.SAVEDOCUMENTFLAGS_DONTADDTORECENTLIST,
        redshift.Frsproxyexport,
    ):
        if verbose:
            log.debug("Extracted Redshift Proxy to: %s", filepath)
    else:
        log.error("Extraction of Redshift Proxy failed: %s", filepath)

    return filepath

render_playblast(filepath, frame_start=None, frame_end=None, fps=None, width=1920, height=1080, doc=None)

Create a playblast of the given or active document.

Parameters:

Name Type Description Default
filepath(str)

The filepath to render the movie to.

required
frame_start Optional[int]

Frame start. Defaults to document start time if not provided.

None
frame_end Optional[int]

Frame end. Defaults to document end time if not provided.

None
fps int

Frames per seconds.

None
width int

Resolution width for the render.

1920
height int

Resolution height for the render.

1080
doc Optional[BaseDocument]

Document to operate in. Defaults to active document if not set.

None

Returns:

Name Type Description
str

The filepath of the rendered movie.

Source code in client/ayon_cinema4d/api/exporters.py
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
def render_playblast(filepath,
                     frame_start=None,
                     frame_end=None,
                     fps=None,
                     width=1920,
                     height=1080,
                     doc=None):
    """Create a playblast of the given or active document.

    Args:
        filepath(str): The filepath to render the movie to.
        frame_start (Optional[int]): Frame start.
            Defaults to document start time if not provided.
        frame_end (Optional[int]): Frame end.
            Defaults to document end time if not provided.
        fps (int): Frames per seconds.
        width (int): Resolution width for the render.
        height (int): Resolution height for the render.
        doc (Optional[c4d.documents.BaseDocument]): Document to operate in.
            Defaults to active document if not set.

    Returns:
        str: The filepath of the rendered movie.
    """

    # Retrieves the current active render settings
    doc = doc or c4d.documents.GetActiveDocument()
    doc_fps = doc.GetFps()
    if fps is None:
        fps = doc_fps
    if frame_start is None:
        frame_start = doc.GetMinTime().GetFrame(doc_fps)
    if frame_end is None:
        frame_end = doc.GetMaxTime().GetFrame(doc_fps)

    renderdata = doc.GetActiveRenderData().GetData()
    previous_render_engine = renderdata[c4d.RDATA_RENDERENGINE]
    renderdata[c4d.RDATA_RENDERENGINE] = c4d.RDATA_RENDERENGINE_PREVIEWHARDWARE

    # Set render settings
    for attr, value in PLAYBLAST_SETTINGS.items():
        renderdata[getattr(c4d, attr)] = value

    # Set FPS and frame range
    renderdata[c4d.RDATA_FRAMERATE] = fps
    renderdata[c4d.RDATA_FRAMESEQUENCE] = c4d.RDATA_FRAMESEQUENCE_MANUAL
    renderdata[c4d.RDATA_FRAMEFROM] = frame_start
    renderdata[c4d.RDATA_FRAMETO] = frame_end

    # Set resolution
    renderdata[c4d.RDATA_XRES] = width
    renderdata[c4d.RDATA_YRES] = height

    renderdata[c4d.RDATA_ALPHACHANNEL] = True

    # TODO: Somehow figure out how to (temporarily) overwrite a video post,
    #    or add a new one and remove it afterwards.
    # Set hardware video post
    # hardware_vp = c4d.documents.BaseVideoPost(c4d.RDATA_RENDERENGINE_PREVIEWHARDWARE)
    # for k, v in HARDWARE_SETTINGS.items():
    #     hardware_vp[getattr(c4d, k)] = v
    # renderdata.InsertVideoPost(hardware_vp)
    bmp = c4d.bitmaps.BaseBitmap()
    bmp.Init(x=width, y=height, depth=24)
    if bmp is None:
        raise RenderError(
            "An error occurred during rendering: could not create bitmap."
        )

    renderdata.SetFilename(c4d.RDATA_PATH, filepath)

    # Renders the document
    result = c4d.documents.RenderDocument(
        doc,
        renderdata,
        bmp,
        c4d.RENDERFLAGS_EXTERNAL | c4d.RENDERFLAGS_NODOCUMENTCLONE,
    )
    if result != c4d.RENDERRESULT_OK:
        raise RenderError(
            "Failed to render {filepath}. (error code: {result})".format(
                filepath=filepath, result=result
            )
        )

    # Switch back to previous render engine,
    # although this doesn't seem to be needed.
    renderdata[c4d.RDATA_RENDERENGINE] = previous_render_engine
    return filepath