Where is the material number stored in the MPoly struct?

Compiling, libraries, modules, coding guidelines and porting

Moderators: jesterKing, stiv

Talin
Posts: 4
Joined: Mon May 14, 2012 10:57 am

Where is the material number stored in the MPoly struct?

Postby Talin » Mon May 14, 2012 11:06 am

Hi, I'm working on updating my .blend file reader to the be able to read BMesh data. At this point, I have everything working except for the material index - The value in MPoly.mat_nr doesn't seem to correspond to the material index as I previously understood it. For example, I have a mesh with 3 materials saved as a .blend file. When I look at the Mesh.mat array, I see that there are in fact 3 materials in the list. But when I look at the Mesh.mpoly list, all of the values for mat_nr are zero.

(If you are wondering why I am reading .blend files, this is to do offline bulk conversions.)

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Wed May 16, 2012 12:27 am

material_index of 0 means the first material of object is assigned to that polygon. If there wasn't a material assigned, it would be -1

This is python, but i believe the relationships equal in C code:

Code: Select all

mat_index = bpy.data.objects['Mesh'].data.polygons[0].material_index

mat = bpy.data.objects['Mesh'].data.materials[mat_index]

# mat is bpy.data.materials['Material']
I'm sitting, waiting, wishing, building Blender in superstition...

Talin
Posts: 4
Joined: Mon May 14, 2012 10:57 am

Postby Talin » Wed May 16, 2012 9:03 am

CoDEmanX wrote:material_index of 0 means the first material of object is assigned to that polygon. If there wasn't a material assigned, it would be -1

This is python, but i believe the relationships equal in C code:

Code: Select all

mat_index = bpy.data.objects['Mesh'].data.polygons[0].material_index

mat = bpy.data.objects['Mesh'].data.materials[mat_index]

# mat is bpy.data.materials['Material']


I've inspected the data using the methods you suggested. What I am seeing is that every polygon has a material index of 0, even though the mesh uses three different materials. So I'm still confused.

And before you ask:

* It's a single object, not several different objects.
* The object has multiple materials each having a different texture.
* I can see all three materials in the 3d modeling window.

However, this does clear up one point - since I'm seeing the same thing in memory as I am seeing in the .blend file, that means that it's not a problem with my .blend file reader.

But I don't understand how I can be seeing multiple materials in the editor, and yet every polygon has a material_index of 0.

Talin
Posts: 4
Joined: Mon May 14, 2012 10:57 am

Postby Talin » Wed May 16, 2012 9:06 am

Here's an example of what I am seeing:

Code: Select all

>>> [poly.material_index for poly in bpy.data.objects['Wall'].data.polygons]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Talin
Posts: 4
Joined: Mon May 14, 2012 10:57 am

Postby Talin » Wed May 16, 2012 9:24 am

I figured it out - they were all using the same material, but different textures because the UV maps were different. The relationship between the UV map texture and the material texture is something that I frequently find confusing.


Return to “Coding Blender”

Who is online

Users browsing this forum: No registered users and 2 guests