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

    Beginner:How to assign A-material to B-cube by python?

    Cinema 4D SDK
    4
    5
    1.5k
    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.
    • I
      ilad
      last edited by

      (😁 Thanks to all the good people who kindly answer questions to a green hand like me in recent days ,i have certainly made a huge pregress due to you zealous guys in such a super forum)

      Well,i meet the problem to assign a material to another object in my project,i reviewed history post but not so solvable,let me take an easy example of A material and B cube,how to assign A to B?

      Below is example pic
      mat assign.png

      import c4d
      
      def main():
         A = c4d.BaseObject(c4d.Ocube)
      #lost on how to make assignment
      
      
      
      
      

      Thanks !
      ilad

      1 Reply Last reply Reply Quote 0
      • r_giganteR
        r_gigante
        last edited by r_gigante

        Hi Ilad, thanks for reaching out us.

        With regard to your request, I warmly recommend to have a look at Materials and Shaders Overview where the basic of Cinema 4D materials and shading handling are discussed. Consider that even if the documentation refers to C++ the described concepts can be applied to Python as well.

        Briefly the solution to your request is:

        def main():
            # get first object
            cubeA = doc.GetActiveObject()
            if cubeA is None: 
               return
           
            # get second object
            cubeB = op.GetNext()
            if cubeB is None:
                cubeB = op.GetPred()
            
            #get first material
            matA = doc.GetActiveMaterial()
            if matA is None: 
                return
            
            # get second material
            matB = matA.GetNext()
            if matB is None:
                matB = matA.GetPred()
            
            # create the texture tags for A and B
            ttagA = c4d.TextureTag()
            ttagB = c4d.TextureTag()
            
            # assign the swapped materials to the texture tags
            ttagA.SetMaterial(matB)
            ttagB.SetMaterial(matA)
            
            # insert the tags to the corresponding objects
            cubeA.InsertTag(ttagA)
            cubeB.InsertTag(ttagB)
            
            # queue an event
            c4d.EventAdd()
        

        Last but not least, it's recommended for future threads to use the appropriate category and mark it as "Ask as a question" upon creation.

        Best, Riccardo

        I 1 Reply Last reply Reply Quote 4
        • I
          ilad @r_gigante
          last edited by

          @r_gigante Thanks!Greate help for me! I got it!😁

          1 Reply Last reply Reply Quote 0
          • C
            cinamatic
            last edited by cinamatic

            Hi,

            I was able to run this code, however the take system did not record changes to the "Tags" category when running the python script. What do I need to add to this code in order for it to record changes to the current take after cubeA.InsertTag(ttagA) & cubeB.InsertTag(ttagB) are executed.

            ManuelM 1 Reply Last reply Reply Quote 0
            • ManuelM
              Manuel @cinamatic
              last edited by

              hi @cinamatic ,

              You should open a new thread for your question as it is not related to this thread.
              I found this thread where you will find some useful information about takes and material.

              If you have any question, please open your own thread explaining what you are trying to achieve and pointing to those different threads. Futur user will find every information to follow the discussion.

              Cheers,
              Manuel

              MAXON SDK Specialist

              MAXON Registered Developer

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