I'm not sure I understand the UV image editor and its ability to hold images. I thought it was purely to get a visual comparison between your UVs and the image you use for texture mapping, but it seems that if you have an image in there, it affects OBJ material export. I would like to ask if this is an intended effect, and if not how to fix it, or just remove all UV editor images in a script.
I'm working with a mesh format for a game. I can already convert into this format from an OBJ file with some existing tools.
To export directly from blender, I have written an addon that first writes out an OBJ file and hands it on to the other conversion tool.
The OBJ files blender exports cannot fully support rich materials, but for my application all I really need is the filename of a single texture. My model uses the old blender renderer (no cycles), and has some number of objects/meshes, where each mesh has some number of material slots, and each material has just one shadeless texture and no other properties.
For the most part these are written out into a .mtl file and into usemtl directives in the OBJ.
However some of them are written correctly as
but are then using the wrong .png file in the .mtl file.
I dug into the builtin blender OBJ export addon and found that it writes a different image name by picking up something called the 'face image', which turned out to be Image data blocks that are linked to the UV editor. This only affected certain materials that I had manually loaded a UV Image for to manually align things.
So back in the python console I find the following:
>>> bpy.data.meshes.polygons.material_index 3 >>> bpy.data.meshes.materials.texture_slots.texture.image.name 'om_decals_02a.png' >>> bpy.data.meshes.uv_textures.active.data.image bpy.data.images['om_floor_03d.png']
This shows that it is possible to have one image in the texture, and a different one shown in the UV editor, and it is this latter one that is preferentially exported (but only if it's present).
I can fix all this up in my export by removing all the UV editor images, but my questions are:
- why does the UV image preview affect the exported material when the actual material assigned to that poly disagrees?
- How can I remove them all from python safely and what impact might that have beyond having to load the image again to align UV layouts.
- Have I set this whole thing up wrong and should be building the model in a different way in the first place?
For removing the uv images I have this:
for mesh in bpy.data.meshes: for data in mesh.uv_textures.active.data: data.image = None
Will this do the job, or will it break things? Is there an API that will correctly handle removal of things like this instead? Is the python internal reference counter the same thing as blender's user counts, or does one track the other?