Multiple material objects

If your topic doesn't fit anywhere else, put it here.

Moderator: joepal

Multiple material objects

Postby blindsaypatten » Thu Sep 14, 2017 2:45 am

I've been learning a bit about scripting Blender and I'm considering writing something that will allow me to both create materials that use Blender's Glass and/or Principled shaders, and have objects with multiple materials.

Because there is a ton of documentation and tutorials on scripting Blender, and very little for MakeHuman, I'm thinking that I'll try to stick to changing the text files MakeHuman produces rather than try to make modifications to MakeHuman and making things work within MakeHuman.

The simplest approach I can think of is to create an additional file that just contains the list of faces, by index, that it applies to and an mhx2 format material description, and then modify the mhx2 importer to look for the additional file when importing a .mhx2 file. I could probably use the uuid of objects to specify which objects the additional materials apply to.

It seems like this would be pretty straight-forward to implement, can anyone think of any reason that it wouldn't work, or know of another simple approach that would be preferable, or any existing code that already handles multiple materials? Does anyone have any idea how hard it would be to properly implement multiple materials in MakeHuman? I am assuming that someone would have already done so if it weren't difficult.

With respect to glass materials (i.e. transparent materials like glass rather than invisible materials as implemented in Blender's Transparent shader), I am thinking that I can just add a property in the mhx2 material description to indicate the Glass shader should be used. Has anyone looked at adding Glass materials to MakeHuman? Again, I have to suspect that it's difficult or someone would have done it by now.

Any (constructive) thoughts at all are welcome.
blindsaypatten
 
Posts: 586
Joined: Tue Mar 14, 2017 11:16 pm

Re: Multiple material objects

Postby blindsaypatten » Sat Sep 16, 2017 3:42 am

I reverted my modified MHX2 importer to place transparency after diffuse and gloss in the material, as the purpose of that was to allow transparent materials (e.g. in eyes) to have gloss rather than be completely invisible and I have since learned that the Transparent node is only intended for invisibility and the Glass node is for transparent surfaces. It still fixes the problem with the stock code where everything gets a roughness of 0.2, and allows for a single diffuse color in the absense of a diffuse texture. I will also put the Principled Shader support back in.

Anyway, the following blender script will make the cornea in my alternative geometry eye transparent and glossy by adding a Glass node as a second material and assigning that material to the appropriate faces. It needs to have the eye in edit mode before running it but I anticipate that being automated when I integrate this into the importer. The set of faces to change is hard coded, as is the material to be set, but it would be straight forward to read these from a file instead. I posted a new version of the eye that this works with in the repository. The old one didn't use the "eye helpers" and therefore wasn't scaled and oriented to match the default eye.
highpoly2Illust.png
There's a thin ring around the iris where I didn't match the geometry to the texture accurately, but that can also be fixed.
Code: Select all
import bpy
import bmesh

bpy.ops.mesh.select_all(action='DESELECT')
me = bpy.context.edit_object.data
bm = bmesh.from_edit_mesh(me)
mat_count = len(me.materials)
bm.faces.ensure_lookup_table()
for i in range(65):
    bm.faces[i].select = True
   
for i in range(350,415):
    bm.faces[i].select = True
   
bmesh.update_edit_mesh(me, True)
#bpy.ops.object.material_slot_add()
mat_name = "HP2GlassCornea"
mat = bpy.data.materials.get(mat_name)
if mat:
    print("material exists")
else:
    print("creating material")
    mat = bpy.data.materials.new(mat_name)
    mat.use_nodes = True
    nodes = mat.node_tree.nodes
    nodes.clear()
    links = mat.node_tree.links
    glassNode = nodes.new('ShaderNodeBsdfGlass')
    glassNode.location = (100, 100)
    outputNode = nodes.new('ShaderNodeOutputMaterial')
    outputNode.location = (400, 100)
    links.new(glassNode.outputs['BSDF'], outputNode.inputs['Surface'])
   
# Need to check if material is on this object here
me.materials.append(mat)
bpy.ops.object.mode_set(mode='OBJECT')
for poly in me.polygons:
    if poly.select:
        poly.material_index = mat_count
       
bpy.ops.object.mode_set(mode='EDIT')
blindsaypatten
 
Posts: 586
Joined: Tue Mar 14, 2017 11:16 pm

Re: Multiple material objects

Postby blindsaypatten » Sun Sep 24, 2017 3:08 am

After putting this aside for a while I got back to it today and made a little bit of progress.

I decided that the easiest way to stay clear of modifying MakeHuman itself was to extend the .mhx2 files, so only the Blender importer needs to be modified. So, I added support for an auxmaterials section following the material property for an object. The auxmaterials consist of a material name from the list of materials in the .mhx2 file, plus a list of the faces, by index, that the material should be assigned to. No changes were necessary to allow adding additional materials to the list of materials in the .mhx2 file. My importer will now handle the auxmaterials entries and set the appropriate material on the specified faces.

In addition, if the Transparent property of a material is True my importer now makes the material glass. For now it just uses the default Glass shader with no setting of additional properties. That's enough for my current purposes but I'll come back and look at what else it should do, and set it up to use the principled shader if that makes sense.

For now I have to manually modify the .mhx2 file so the next step is to write a script that reads a .mhx2 file, looks up additional materials based on object uuid, and insert the additional materials and auxmaterial sections.

Although it has a ways to go, I get a certain degree of satisfaction out of being able to import a character and get proper-looking eyes without having to make any modifications manually in Blender.

Code: Select all
...
            "material" : "Aa_testhp2v2:High_poly2:High_poly2",
            "auxmaterials" : [
                {
                    "material" : "Aa_testhp2v2:High_poly2:High_poly2glass",
                    "faces" : [
0,1,2,3,4,5,6,7,8,9,
10,11,12,13,14,15,16,17,18,19,
20,21,22,23,24,25,26,27,28,29,
30,31,32,33,34,35,36,37,38,39,
40,41,42,43,44,45,46,47,48,49,
50,51,52,53,54,55,56,57,58,59,
60,61,62,63,64,65,66,67,68,69,
70,71,72,73,74,75,76,77,78,79,
80,81,82,83,84,
350,351,352,353,354,355,356,357,358,359,
360,361,362,363,364,365,366,367,368,369,
370,371,372,373,374,375,376,377,378,379,
380,381,382,383,384,385,386,387,388,389,
390,391,392,393,394,395,396,397,398,399,
400,401,402,403,404,405,406,407,408,409,
410,411,412,413,414,415,416,417,418,419,
420,421,422,423,424,425,426,427,428,429,
430,431,432,433,434   
                    ]
                }
            ],
            "mesh" : {
...
blindsaypatten
 
Posts: 586
Joined: Tue Mar 14, 2017 11:16 pm

Re: Multiple material objects

Postby blindsaypatten » Mon Sep 25, 2017 4:40 am

I used the principled shader metalic property to make the belt buckle on the pants shiny, and just for kicks made the fingernails on the body a separate material as well, although it isn't as visible as I had hoped. Actually two materials, one the nail where it is in contact with the skin and then a second for the detached (whitish) part.
BeltMetalic.png

What it looks like by default
BeltDefault.png
blindsaypatten
 
Posts: 586
Joined: Tue Mar 14, 2017 11:16 pm

Re: Multiple material objects

Postby blindsaypatten » Sat Sep 30, 2017 9:30 pm

After further thought and a look at some of the MakeHuman code it seems like one could just add additional materials to the .mhclo file, and add an additional attribute to the proxy class to keep a list of them. The MHX2 export could then add the extra bits to the .mhx2 file and it would work without any extra steps. MakeHuman itself will just ignore the extra attribute until internal support for multiple materials gets added (if ever). The extra lines in the .mhclo file could just be
"auxmaterial myauxmat.mham"
Where the .mham file would include the material descriptor file and a list of faces that it applies to.

Alternatively, one could just allow multiple materials to be listed. But then one would need some way to include the faces to which each applies, which would be a major change to the .mhclo file. The faces could go into the .mhmat file, but that would be an even greater departure in format, and it seems like a material description should be separate and reusable independent of objects. So, having a separate file with the material name and the faces seems like it would be the most minimal change from the status quo. Maybe a single file per object for multiple additional materials would be good to avoid an unnecessary profusion of files.

Does anyone have thoughts or suggestions for the best way to smoothly integrate this?
blindsaypatten
 
Posts: 586
Joined: Tue Mar 14, 2017 11:16 pm

Re: Multiple material objects

Postby blindsaypatten » Sat Oct 07, 2017 2:40 am

A little more progress. I added an auxmaterials line to the .mhclo file and created a .mham file that gives the additional materials and the faces they apply to. The code just stores the filename in the object data structures. The mhx2 exporter then retrieves the file and inserts the extra information into the .mhx2 file. And the mhx2 importer in Blender takes care of assigning the materials to the specified faces. So once I make the changes for an object once it will be used from then on for new models, so no more repetitive changes with each new character and each new import, yay! And it supports transparent solids (glass) and uses the Principled shader where available.
blindsaypatten
 
Posts: 586
Joined: Tue Mar 14, 2017 11:16 pm


Return to General discussions about makehuman

Who is online

Users browsing this forum: No registered users and 1 guest

cron