My problems with quads to tris scripting.

I am working on a .md3 export script and want to add quads to tris functionality into the script itself.
The problem I am having is that quad to tris needs to be run on the selected object, in edit mode… Whenever I run it in my script it overwrites the entire scene with the new triangulated mesh.
Does anyone know how I can get around this and still retain my ability to export more than one object?
You can grab the script I am working on here, although it is from before I started working on the quads to tris conversion.

I just tried the script (from that link) on the Default scene and it made a log and md3 file. The script did crash until I added a UV layer to the cube (you might want to trap for that). But it did not leave any extra copy of the cube in the scene.

Can you post a BLEND file that demonstrates the problem…?

I have done something similar and the OBJ export script also has this option. Check out the Object.to_mesh(…) function. It actually generates a new Mesh based on your current Mesh and you have the options to apply modifiers to that mesh and do some other tricks. When you are done with it you just do

Here’s a blend file with the script attached.

About the UV map error. I left it in, with a new message explaining to unwrap the mesh. MD3s are all used for gaming and need skins, so I like that it returns an error there.

Line 505 is where triangulate is defined, it’s called 20 - 30 lines later in save. I’ve got the functionality there (it does run quad to tris), I just need it to return to blender with the original objects still there unaltered. Otherwise I would just insist on triangulating meshes before exporting like always and be done with it. There are many pros to modelling with quads, and this was requested of me by a friend, so I am giving it a shot. Thanks for taking the time to look at this. It is a good tool for game engines that use Quake 3 models.


TEST_ExportMD3.blend (441 KB)

I see what you mean.

You can try something like this. Save the mesh data before you operate upon the object. Then once the object is written to disk, restore the mesh data back to the object.

  for obj in bpy.context.selected_objects:
    convert_to_tris = False
    if obj.type == 'MESH':
      me_SaveMesh =
      # Operate and convert to triangles and export to file. = me_SaveMesh

I did what you recommended Atom. It works, but only for one of the two quad face cubes. The other is still triangulated. Thanks a lot, this is definitely enough for me to iron out the last little kinks. I had tried something like this but with a copy of the entire object, it got really messy and ended up changing the objects name. This is much cleaner. Thanks again.

Now, I’m not asking for any more help (yet), I have not yet tried to fix this issue, but…

If anyone is interested in looking, this script has one more major flaw. The objects location is not being exported (everything comes out at 0,0,0)
To test this get Nphernos md3 compiler to look at the exported test cubes. The little one that is offset comes out centered and hidden inside the larger cube in the .md3.

Link to Npherno
Blender 2.4 also has working .md3 importers you can use, or I’ve read here at Blender Artists someone is writing an importer for 2.6 so you could try it (I haven’t yet)

The script does support Quake 3 style shader path labeling. Add a custom property to the object named “md3shader” then make the property value a string defining the shader path. This was mandatory in the script before I got it, some game engines don’t need this so I removed the ignoreUV property and just name shaders NULL if it not defined. I willforce myself to write a read me when I release this version.

I have the quad to tris working the way I wanted, I need to test it still with skins, but the meshes get exported as tris and stay quads in Blender. Thanks again Atom.
I have also fixed the object location issue, animations work correctly now. I needed to add to each vert for each axis obj.location[#]
I will look at your importer again test-dr, It has interested me for some time, I’ve read your thread once and quickly glanced at your script.

@Drek: ok, if you go this direction, i can only give some hints. I am “full” concentrated on the mdx/mdm thing based on enemy-territory-engine.
To use the blender-built-in quad2tris you have to check in script the object is the active object (there are samples about how to do this) and then you can create a duplicate object of it and apply the quat2tris to it. Then use the modified vertices of this new generated object. Delete it after usage. This is not the same like the “mesh-creation” for every shape in the md3-export, but it is the way to go. There is the way to reverse any recorded blender-action — like in some addons, where you change the settings and the display will revert to the last setting – this may be a more time-saving way to use the same object (do some modifications, export the changes and revert to the old version).
And !urgent! i have not checked the new blender-2.62 modifiers for mesh-manipulations (like those to convert to a box-style-like-interpretation). There may be some modifiers with new ideas.