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

    How to create a material with the selected object as assigned?

    General Talk
    r20 python
    2
    4
    686
    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.
    • JH23J
      JH23
      last edited by

      I tried to make a script where when generating a material, assign the active object that material but I do not know if it should go the way I am doing it or it is wrong, I also saw in the script history c4d.ID_MATERIALASSIGNMENTS
      I guess it has something to do with it but I don't understand
      mi codigo

      import c4d
      from c4d import gui
      def main():
          cubo = doc.GetActiveObject()
          c4d.CallCommand(13015, 13015) # Nuevo Material
          mat = doc.GetAciveMaterial()
          mat[c4d.MATERIAL_PAGE_ASSIGNMENT] = cubo
          
      if __name__=='__main__':
          main()
      
      ferdinandF 1 Reply Last reply Reply Quote 0
      • ferdinandF
        ferdinand @JH23
        last edited by

        Hello @JH23,

        thank you for reaching out to us. While it is possible to use CallCommand in scripts for the more basic things like creating a material, it is recommended to use the more abstract object-oriented API whenever possible. Below you will find an example script which creates, modifies and assigns a new material to the currently active object.

        Cheers,
        Ferdinand

        """Example for creating a material and assigning it to the currently active 
        object.
        
        As discussed in:
            https://developers.maxon.net/forum/topic/13357
        """
        
        import c4d
        
        def main():
            """
            """
            # op and doc a are module attributes predefined by Cinema 4D for a script 
            # module. doc is the currently active document, op the currently active 
            # object in this active document.
            if op is None:
                raise RuntimeError("Please select an object.")
            
            # Instantiate a standard material and insert it into the document, as 
            # otherwise we could not use it.
            mat = c4d.BaseList2D(c4d.Mmaterial)
            doc.InsertMaterial(mat)
            # Example for modifying the material, here setting its diffuse color.
            mat[c4d.MATERIAL_COLOR_COLOR] = c4d.Vector(1, 0, 0)
        
            # Create a texture tag on the selected object, change the projection mode
            # to uvw and link the newly crated material to it.
            tag = op.MakeTag(c4d.Ttexture)
            tag[c4d.TEXTURETAG_PROJECTION] = c4d.TEXTURETAG_PROJECTION_UVW
            tag[c4d.TEXTURETAG_MATERIAL] = mat
        
            # Notify Cinema 4D of the changes made.
            c4d.EventAdd()
        
        # Execute main()
        if __name__=='__main__':
            main()
        

        MAXON SDK Specialist
        developers.maxon.net

        1 Reply Last reply Reply Quote 0
        • JH23J
          JH23
          last edited by

          Hello @ferdinand
          thank you very much solved my doubts
          Cheers,
          JH23

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

            Hello @JH23,

            without further questions or postings, we will consider this topic as solved by Wednesday and flag it accordingly.

            Thank you for your understanding,
            Ferdinand

            MAXON SDK Specialist
            developers.maxon.net

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