Get material from template file and apply it to project



  •      Hello everyone.
    

    I want to get material from template file and apply it to project follow the sample material name.
    Can you show code C++, or Python? .
    It is sample video:
    https://www.youtube.com/watch?v=-zPQT0kRdPc&feature=youtu.be
    Thanks you very much.



  • Hello,

    as always, please make yourself familiar with how to post questions on this forum: Read Before Posting

    I assume a "template file" is just a C4D-file that stores some materials?

    You can load a C4D-File with LoadDocument(). Then you can copy the materials found in that loaded document into the active document. See BaseDocument Manual and BaseMaterial Manual.

    import c4d
    import os
    
    def main():
        # select the c4d file to load
        filename = c4d.storage.LoadDialog(type=c4d.FILESELECTTYPE_SCENES, title="Choose File.", flags=c4d.FILESELECT_LOAD, force_suffix="c4d")
        if filename is None:
            return
    
        # checks for c4d suffix
        name, suffix = os.path.splitext(filename)
        if suffix != ".c4d":
            raise RuntimeError('Invalid file selected.')
    
        # load the document (materials)
        loadedDoc = c4d.documents.LoadDocument(filename, c4d.SCENEFILTER_MATERIALS)
        if loadedDoc is None:
            raise RuntimeError('Could not load document.')
        
        # get all materials
        
        mat = loadedDoc.GetFirstMaterial()
        
        while mat is not None:
            
            # clone material
            clone = mat.GetClone()
            if clone is None:
                raise RuntimeError('Could not clone material.')
            
            # insert clone into the active document
            doc.InsertMaterial(clone)
            
            mat = mat.GetNext()
            
        c4d.EventAdd()
    
    if __name__=='__main__':
        main()
    

    best wishes,
    Sebastian



  • Thanks s_bach very much.


Log in to reply