Home Marmoset

Assigning Textures to Materials in Python

Offline / Send Message
Pinned
Hey there!

I am tasked with writing a python script for automated object baking.
I am stuck after importing the model. Only the albedo map is assigned to the material.
Maps like metallic, normal and roughness are not assigned.

It would be no problem for me to assign those texures in the python script but i haven't found any functionality exposed that would allow me to do that.
So i am stuck with the import functionality that does not import all my maps.

Do you have any suggestions to this problem?

Greetings,
Mamu

Replies

  • fatique
    Offline / Send Message
    fatique polycounter lvl 6
    Hey.

    There are two examples in plugin folder that can help you with materials scripting:

    Creating Materials from Images.py
    Editing Materials.py

    Shortly:

    # Create new material
    mat = mset.Material(name) 
    # Delete it
    mat.destroy()

    # Assign material to mesh
    mat.assign(mesh)

    # Assign texture to material, where texture - path to file
    mat.surface.setField('Normal Map', texture)

    mat.setSubroutine("occlusion", "Occlusion")

    mat.occlusion.setField('Occlusion Map', texture)
  • ryanroye
    Offline / Send Message
    ryanroye polycounter lvl 4
    To add to this post, i'd like to inquire about how to change image settings (more specifically, ticking on/off sRGB or Use Mipmaps). This doesn't seem possible using the setfield function nor does it show up when using "getfieldnames", and I've tried using variants of the "sRGB" function in the texture class but can't get the settings changed through python.

  • ryanroye
    Offline / Send Message
    ryanroye polycounter lvl 4
    I got some help from Luxary (author of this script for Marmoset Toolbag), as I noticed their plugin affected the things I wanted to change. To save others the same frustration, I will relay the info I received from them here.

    Example of setting the Albedo Map to use sRGB, and not use mipmaps.

    material.albedo.getField("Albedo Map").sRGB = True
    material.surface.getField("Normal Map").useMipmaps = False

    I guess I was thrown off by the idea of "get" meaning to obtain information rather than set parameters.

    NOTE: As of Marmoset Toolbag 3.08, you will not see the checkbox update in the UI until you save and reload the scene. As far as I know, this is only a cosmetic issue, it still works as intended.
  • Clark Coots
    Offline / Send Message
    Clark Coots polycounter lvl 13
    thanks @ryanroye for linking me to this thread. I spoke with a tech artist friend and he came up with the exact code you have to set the Albedo map to sRGB:

    material.albedo.getField("Albedo Map").sRGB = True

    I ran into the same UI issues. I didn't try save/reloading. But it was not keeping my sRGB checked sometimes. It would revert to False after I open the UI popup and close it again. Other than that I ran a batch render screenshot script after creating materials and the renders came out fine.

  • lorig
    Thanks this was helpful.  Does anyone know a way to set the subdivision with the Toolbag?
  • EarthQuake
    lorig said:
    Thanks this was helpful.  Does anyone know a way to set the subdivision with the Toolbag?
    Unfortunately, this functionality was lost when the sub-d settings moved from material to mesh properties. We've added this to our todo list now. The next update will have many additions and fixes to the Python system, and we'll see if we can get this in.
Sign In or Register to comment.