Need help fixing MD5 exporter

I get this error http://www.pasteall.org/34086 running this MD5 exporter http://www.pasteall.org/34087/python on Blender 2.62

Can someone please help fixing it? Thanks.

TEST CASE .blend (made in 2.62): http://www.pasteall.org/blend/15417

Never mind, I fixed the error.

Ok, after sleepless night here is what I got:

The orientation of the skeleton and bones seems to be correct, animation plays well, but the mesh is screwed up.

I need help. Here is the edited script: http://www.pasteall.org/34090/python

So I investigated further and I identify a block of code that contributes to the issue.

If I don’t transpose the matrix in the first big block of the code below (I think I have to do that in accordance with changes in 2.62 from 2.61), I get bones oriented properly, mesh intact, but rotated 180 degrees and centered. When I play animation, bones act properly, but mesh gets all jacked up.




def matrix_invert(m):
  det = (m.col[0][0] * (m.col[1][1] * m.col[2][2] - m.col[2][1] * m.col[1][2])
       - m.col[1][0] * (m.col[0][1] * m.col[2][2] - m.col[2][1] * m.col[0][2])
       + m.col[2][0] * (m.col[0][1] * m.col[1][2] - m.col[1][1] * m.col[0][2]))
  if det == 0.0: return None
  det = 1.0 / det
  r = [ [
      det * (m.col[1][1] * m.col[2][2] - m.col[2][1] * m.col[1][2]),
    - det * (m.col[0][1] * m.col[2][2] - m.col[2][1] * m.col[0][2]),
      det * (m.col[0][1] * m.col[1][2] - m.col[1][1] * m.col[0][2]),
      0.0,
    ], [
    - det * (m.col[1][0] * m.col[2][2] - m.col[2][0] * m.col[1][2]),
      det * (m.col[0][0] * m.col[2][2] - m.col[2][0] * m.col[0][2]),
    - det * (m.col[0][0] * m.col[1][2] - m.col[1][0] * m.col[0][2]),
      0.0
    ], [
      det * (m.col[1][0] * m.col[2][1] - m.col[2][0] * m.col[1][1]),
    - det * (m.col[0][0] * m.col[2][1] - m.col[2][0] * m.col[0][1]),
      det * (m.col[0][0] * m.col[1][1] - m.col[1][0] * m.col[0][1]),
      0.0,
    ] ]
  r.append([
    -(m.col[3][0] * r[0][0] + m.col[3][1] * r[1][0] + m.col[3][2] * r[2][0]),
    -(m.col[3][0] * r[0][1] + m.col[3][1] * r[1][1] + m.col[3][2] * r[2][1]),
    -(m.col[3][0] * r[0][2] + m.col[3][1] * r[1][2] + m.col[3][2] * r[2][2]),
    1.0,
    ])
  return r

***********************************************************************************

def generateboundingbox(objects, md5animation, framerange):
  scene = bpy.context.scene #Blender.Scene.getCurrent()
  context = scene.render #scene.getRenderingContext()
  for i in range(framerange[0], framerange[1]+1):
    corners = []
    scene.frame_set( i ) 
    
    for obj in objects:
      data = obj.data #obj.getData()
      if obj.type == 'MESH' and data.faces:
        (lx, ly, lz ) = obj.location
        bbox = obj.bound_box
        matrix = [[1.0,  0.0, 0.0, 0.0],
          [0.0,  1.0, 0.0, 0.0],
          [0.0,  1.0, 1.0, 0.0],
          [0.0,  0.0, 0.0, 1.0],
          ]
        for v in bbox:
          corners.append(point_by_matrix (v, matrix))
    (min, max) = getminmax(corners)
    md5animation.bounds.append((min[0]*scale, min[1]*scale, min[2]*scale, max[0]*scale, max[1]*scale, max[2]*scale))

Script with comments on those matrices http://www.pasteall.org/34104/python

Any ideas?

OK got all info I need to have a try :wink:

@PKHG, i would take care - there are some things not quite “normal”.
For example from the code, the used kind of “determinant-calculation”
for the inverse of a 4x4-matrix uses only 3 components and i know,
i used 4 components for such a conversion.
It might be the matrix is some kind of special limitation, where the 4th-value does not
contribute to the result.
And last, i know i did not check for equal == 0.0 for the determinant-value,
i had to check for some slight range around zero.

edit: a quick view to some older codes (from myself) i notice the remark about this problem and that i did ignore the possible “freak-out” at this point. I only did print out a hint-message, so i might know if it really happens.

and only for the fun again, an old screenshot from my bad results:

(its still available at pasteall … )

thanks test-dr will pay attention, if I success I will not use the own written math things, mathutils seems to have what needed :wink:

i think that this code is from the katsbits exporter. the md5 exporter is not updated to 2.63, but the md3 exporter it has. maybe you could have a look. i have worked a lot of with the md5 in version 2.59. i don’t know exactly what has changed in the way blender calculates the child parent relationships between bones.

MD3 is not a skeletal format and is totally different from MD5.

Here is a threat on KatsBits one of the Blender Add-ons support team members started and I posted whatever we have done with CoDEmanX: http://www.katsbits.com/smforum/index.php?topic=417.msg2422#msg2422

i don’t mean anything bad about this post. i only mean that maybe the error could be resolved in that way. sorry, i think there is a misunderstanding. if something is needed to fix the problems i will try. there is almost 2 months i don’t program anything inside Blender.

got the skeleton exported to skeleton.md5mesh and skeleton.md5anim and in the viewer something
is ‘moving’ … I think I did not select all
Trying that :wink: now

EDIT:
need a simpler model, selection the parts of the skeleton is not so easy … ;-(

But a first … may need some adjustments … will see

motorsep: please give something less complicated … *.blend …

if i remember well, there was a change between row major and col major in 2.6 versions. so the matrix multiplications should be in opposite order.

Yeah, and that’s what I attempted to do to make exporter that worked in 2.61 working in 2.62… I got the bones working, but couldn’t get mesh and deformations to work as my knowledge in Blender’s API and Python is close to zero :stuck_out_tongue:

mmm. understand. another thing to take in account is the vertex groups influences. if the bones are working, the problem is in the class Weight ( from kasbits ) i guess. it will be important to take the influences clean and normalized. ie, if you have used an influence for a mask for example, i guess the algorithm take it in account for the weight calculation. so the deformation will be wrong.

i don’t exactly remembered how the exporter works. i think that weight class stores the vertex location in one bone space, and a value of influence, between 0.0 and 1.0

Well, exporters for 2.49 and 2.57+ did what they needed to do. Exported model was working. This script I was messing with was made originally for 2.57+ and it worked with 2.61. So it should work with 2.62 unless Blender guys changed the way weights are handled :confused:

could u upload the model you are working on?? or private to take a look tomorrow??
i wonder if bmesh … faces with more than 4 vertices??

Please see the first post. I linked a very simple test case. It’s ugly, but should do the job :slight_smile:

WTH, the test case exports correctly with the exporter I tuned for 2.62 o_O

Here is the .blend http://www.pasteall.org/blend/15417

Here is the output www.kot-in-action.com/files/md5_test_case1.zip

Here is the script http://www.pasteall.org/34120/python

The only thing that DOES NOT work is Scale value. No matter what scale I put in, it always exports at the original scale :confused:

Now, the question is why my production model doesn’t work? It’s autoweighted with manual weights adjustments, has IK, has custom shape bones. That’s about how different it is from the test case.

P.S. I just updated link to the .blend file in the first post too

Ook, it’s a game changer now. Kat tested script and it turns out I actually fixed it. The reason it didn’t work on my production model is that I never applied transforms to the mesh :confused:

Here is the “final” version of the script for 2.62: http://www.pasteall.org/34124/python

To successfully export into ID Tech 4 MD5 model, you need to unparent mesh from the Armature; apply Loc, Rot, Scale and Visual Transformations to the mesh, and then export it.

One thing that doesn’t work, and I have no clue why, is Scale. No matter what I set as exporting scale in the exporter’s GUI, I get model in the original size.

So my question now is if someone can fix scale, and make exporter to unparent meshes that are parented to the Armature, then apply transforms and only then save the output.

It would be nice to also add more stuff like better GUI, handling of Actions, using local markers to determine animation length per Action (or first and last frame of the animation in an Action) and add MD5camera export, but that’s not critical right this moment.

EDIT: I found that Scale slider is not referring to scale variable in the code. Instead, there is scale = 1.0 to hardwire scale of the model. I don’t get how to make slider set the scale variable.

EDIT 2: The fix for the slider was trivial, but, now the value for scale needs to be passed into the calculations block way above it and I just have no clue how to do that.

Thanks a bunch!

@motorsep
the 2.62 version does not work in 2.63, because faces have become polygons.

Your example from post #1 is OK!
First try exporting it ==> crashed the modelviewer
Changing quads and … to tris it worked and after exporting the tga it got colors :wink:
Next thing it is so small will see to where scaling should/could be done :wink:

EDIT: Grrr should know more about MD5 too much strange problems
Making the mesh smaller ==> crash modelviewer reason unvisible

stopping for today … maybe later more