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

    Assign materials to Objects using Python Un-subscribe from this thread

    Cinema 4D SDK
    python r25
    3
    3
    529
    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.
    • T
      Tomoya
      last edited by

      Hello!
      I would like to automate the replacement of materials loaded in Alembic.

      How can I change the attached python code to replace the material with a material with the same name as the one applied in Alembic?

      I would appreciate your advice.

      I am new to the forum, so please forgive my unfamiliarity.

      import re
      import c4d
      
      def main():
        doc = c4d.documents.GetActiveDocument()
        oplist = doc.GetObjects()
        if not oplist: return
        matlist = doc.GetMaterials()
        if not matlist: return
        if oplist:
            for op in oplist:
              opname = op.GetName()       
              mat = doc.SearchMaterial(opname)
              if not mat:
                for mat in matlist:
                    mname = mat.GetName()
              mname = mat.GetName()
              match = re.match(opname, mname)
              if match:
                textag = c4d.TextureTag()
                textag.SetMaterial(mat)
                textag[c4d.TEXTURETAG_PROJECTION]=c4d.TEXTURETAG_PROJECTION_UVW
                op.InsertTag(textag)
        c4d.EventAdd()
      if __name__==‘__main__’:
        main()
      
      1 Reply Last reply Reply Quote 0
      • ManuelM
        Manuel
        last edited by

        hi,

        welcome to the forum.

        I would change the fact that if no material is found with the right name, we can just continue the loop with the next object.
        This will add a new material tag to the alembic object and link the tag to the material. That is exactly what you want to do?

        import re
        import c4d
        
        def main():
          doc = c4d.documents.GetActiveDocument()
          oplist = doc.GetObjects()
          if not oplist: 
            return
          # Check if there is at leat one material in the document otherwise return 
          mat = doc.GetFirstMaterial()
          if not mat: 
            return
        
          # we already checked if oplist is not empty we do not need it twice
          for op in oplist:
            opname = op.GetName()
            mat = doc.SearchMaterial(opname)
            # If there is no material with the same name, we can continue the loop to the next object
            if not mat:
              continue
              
            mname = mat.GetName()
            match = re.match(opname, mname)
            if match:
              textag = c4d.TextureTag()
              textag.SetMaterial(mat)
              textag[c4d.TEXTURETAG_PROJECTION]=c4d.TEXTURETAG_PROJECTION_UVW
              op.InsertTag(textag)
          c4d.EventAdd()
          
        if __name__== '__main__':
          main()
        

        Cheers,
        Manuel

        MAXON SDK Specialist

        MAXON Registered Developer

        1 Reply Last reply Reply Quote 0
        • ferdinandF
          ferdinand
          last edited by ferdinand

          Hello @Tomoya,

          without any further questions or postings, we will consider this thread as solved by Friday the 4th, February 2022.

          Thank you for your understanding,
          Ferdinand

          MAXON SDK Specialist
          developers.maxon.net

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