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

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()

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://plugincafe.maxon.net/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()

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://plugincafe.maxon.net/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()

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

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