3
$\begingroup$

So I'm pretty much done with designing a script to export meshes I (or others within my group) make in Blender for use in another piece of software (VoxelStudio).

Everything has been slow going due to a serious lack of documentation/examples, but I have to say these forums have been a god send, thank you people.

The one last part that's really stopping me from actually finishing the script is working out how to get the ID number of the Texture that's applied to a given material on a given Face.

I can get the active material easily enough.... though that took trawling everywhere to work out how. How do I get the active materials active texture for the current face?

For example:

for i in context.selected_objects: i.update_from_editmode( ) tempmesh = bmesh.new( ) tempmesh.from_mesh( i.data ) for f in tempmesh.faces: mat = i.material_slots[f.material_index].material writeString ( file, '%i\r\n' % ( f.material_index ) ) tempmesh.free

You can see that I've got the material index easily enough, and the material itself is held in 'mat' within the 'for f' loop. However, the method of working out what the texture index of the current face is still eludes me.

The model is set up with different Materials having multiple textures assigned, each for different purposes. Just no idea how to ask, what texture is showing on this material on that face.

$\endgroup$

1 Answer 1

1
$\begingroup$

This script will print the names of the the textures assigned to the various materials on the selected objects:

import bpy, bmesh

C = bpy.context
for o in C.selected_objects:
        bm = bmesh.new()
        bm.from_mesh( o.data )
        for f in bm.faces:
            mat = o.material_slots[ f.material_index ].material
            if C.scene.render.engine == 'BLENDER_RENDER':
                for textureName in [ ts.texture.name for ts in mat.texture_slots if ts ]:
                    print( "material_index_{mi}.texture_name_{tn}".format( 
                        mi = f.material_index,
                        tn = textureName
                    ) )
            if C.scene.render.engine == 'CYCLES':
                for textureName in [ n.name for n in mat.node_tree.nodes if 'TEX' in n.type ]:
                    print( "material_index_{mi}.texture_name_{tn}".format( 
                        mi = f.material_index,
                        tn = textureName
                    ) )                    
        bm.free

It will print it in the format of "material_index_X.texture_name_Y" (where X is the material index, and Y is the texture name).

You can of course use the texture slot index, rather than the texture name. Or what might be even more intersting, the image path assigned to the image texture you might be using (accessible through texture.image.filepath).

$\endgroup$
6
  • $\begingroup$ Thank you, But how do I find out which texture is active on a given face? $\endgroup$
    – Eewec
    Commented Jul 22, 2017 at 9:36
  • $\begingroup$ Oh, I thought you had multiple textures. For the active one you simply need to access material.active_texture, or material.active_texture_index. $\endgroup$
    – TLousky
    Commented Jul 22, 2017 at 9:38
  • $\begingroup$ I do have multiple Textures. But each texture is used on different surfaces. I'm trying to figure out how to export the ID of which texture slot is active for a given Face in the mesh as material.active_texture and material.active_texture_index give me the active slot in the Blender UI, not the active slot for the face of the mesh. Or at least they do when I try to use them. $\endgroup$
    – Eewec
    Commented Jul 22, 2017 at 9:58
  • $\begingroup$ The active texture of the active material on that face should be the correct texture. In Blender Internal this will be easier to pull, while in Cycles it's harder to determine which texture is the "active" texture (unless you mean the one that's active in the node editor). $\endgroup$
    – TLousky
    Commented Jul 22, 2017 at 12:57
  • 2
    $\begingroup$ For BI a single material can be assigned to a face, one material can use multiple textures, every enabled texture in the materials texture_slots is used by that material. For cycles a material can have multiple texture nodes, you would need to loop through all the nodes to find what textures it uses. $\endgroup$
    – sambler
    Commented Jul 22, 2017 at 18:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .