User avatar
By deadalvs
#354505
hi !

I checked the docs, maybe I missed a spot ..

is it possible to create mxm materials with python in Cinema4d ? I'd like to create a reference material and then point to a specific mxm on the harddrive (linking it).

any input welcome !



thanks in advance !

m.
By JDHill
#354512
You can do it this way:
Code: Select all
import c4d

ID_MXMATERIAL = 1021237;
ID_MXMFILENAME = 1002;

def main():
    material = c4d.BaseMaterial(ID_MXMATERIAL);
    material[ID_MXMFILENAME] = "C:\\carpaint_cherry.mxm";
    doc.InsertMaterial(material);
    doc.SetActiveMaterial(material); 
    c4d.EventAdd();

if __name__=='__main__':
    main()
There is a slight problem with this, though, since it sets the path through the material's description; what will happen is that the plugin will pop up a dialog asking you if you really want to overwrite the material with the specified MXM. This is because switching from embedded mode to linked mode is a destructive action, even if the material in question was just created a half-second ago. To get around that, you can pre-set the MXM path to a different, valid path first, directly through the material's BaseContainer, then set the path you actually want, like this:
Code: Select all
import c4d

ID_MXMATERIAL = 1021237;
ID_MXMFILENAME = 1002;

def main():
    material = c4d.BaseMaterial(ID_MXMATERIAL);
    data = material.GetDataInstance();
    data.SetFilename(ID_MXMFILENAME, "C:\\dummy.mxm"); 
    material[ID_MXMFILENAME] = "C:\\carpaint_cherry.mxm";
    doc.InsertMaterial(material);
    doc.SetActiveMaterial(material); 
    c4d.EventAdd();

if __name__=='__main__':
    main()
In this case, the plugin will not warn you about damaging an existing embedded material, since the material already links to a different MXM file.
User avatar
By deadalvs
#354541
hey !

thanks a lot so far. importing mxms works so far !

as you may imagine, I am doing this for multiple objects, where each object's name is the same as the referenced material (+ ".mxm"). so to actually assign the material to the mesh, does it have to be selected ? or how would I exchange the material tag to the newly imported material ?
By JDHill
#354544
If you have a TextureTag, you can set its material using TextureTag::SetMaterial. If your object has no TextureTag, you can create one using BaseObject::MakeTag(Ttexture), then call SetMaterial on the created tag:
Code: Select all
import c4d

ID_MXMATERIAL = 1021237;
ID_MXMFILENAME = 1002;

def main():
    material = c4d.BaseMaterial(ID_MXMATERIAL);
    data = material.GetDataInstance();
    data.SetFilename(ID_MXMFILENAME, "C:\\dummy.mxm"); 
    material[ID_MXMFILENAME] = "C:\\carpaint_cherry.mxm";
    doc.InsertMaterial(material);
    doc.SetActiveMaterial(material); 
    obj = doc.GetFirstObject();
    if not obj is None:
        tag = obj.GetTag(c4d.Ttexture);
        if tag is None:
            tag = obj.MakeTag(c4d.Ttexture);
        tag.SetMaterial(material);
    c4d.EventAdd();

if __name__=='__main__':
    main()
User avatar
By deadalvs
#354549
YESS !

got it working !

amazing help, thanks a TON !


the code is quite messy due to my naming convention, but it's working and that's what counts !
SS Pinto Bean

It's bean a while.... I don't know how to insert […]

Never No More Studio Lighting

Hello Mark! Very good tips about the camera setti[…]

Will there be a Maxwell Render 6 ?

https://community.sketchucation.com/category/49/wi[…]

Sadly, this lack of a response demonstrates a mori[…]