Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Get material from template file and apply it to project

    Cinema 4D SDK
    2
    3
    504
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • N
      ngvanphong2012
      last edited by s_bach

           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:


      Thanks you very much.

      1 Reply Last reply Reply Quote 0
      • S
        s_bach
        last edited by s_bach

        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

        MAXON SDK Specialist

        Development Blog, MAXON Registered Developer

        1 Reply Last reply Reply Quote 0
        • N
          ngvanphong2012
          last edited by

          Thanks s_bach very much.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post