SOLVED How to import svg in S25 from path

Hello,

I was really delighted to hear that svg import is now supported natively in S25. But the only method I am aware of so far is via drag and drop.

I would like to import an svg file using only its path as input in a python script. Is there a way to do that?

Thank you

Hello @InterfaceGuy,

thank you for reaching out to us. You can just use the existing loading and merging functions in c4d.documents. The snippet below will open a file dialog where you must select a SVG file, and it will then load that SVG file into a new scene. The example will also suppress the options dialog of the importer. There are many options with c4d.documents.LoadDocument and c4d.documents.MergeDocument; to not suppress these options dialogs, to merge with specific documents, etc. If necessary, you could also get hold of the SVG importer plugin first to set the import settings. See our documentation for details on c4d.documents.LoadDocument. And in our GitHub Python SDK repository, in the Files & Media section, you can find examples for manipulating the im- and exporter plugins.

Cheers,
Ferdinand

The result:
d7a200e5-ae4f-4d9d-97ed-4337d4cda711-image.png

The script:

import c4d
import os

def main():
    """Loads an svg file from a file dialog.
    
    You can just replace `file` with any path you would like.
    """
    file = c4d.storage.LoadDialog(title='Select a file')
    if not file or os.path.splitext(file)[1].lower() != ".svg":
        raise RuntimeError("Please select a svg file.")
        
    doc = c4d.documents.LoadDocument(file, c4d.SCENEFILTER_NONE)
    if doc is None:
        raise RuntimeError("Failed to load svg file.")
    
    c4d.documents.InsertBaseDocument(doc)
    c4d.documents.SetActiveDocument(doc)
    
if __name__=='__main__':
    main()

Thank you so much!