0xafbf / blender-datasmith-export

Blender addon to export UE4 Datasmith format
GNU General Public License v3.0
400 stars 50 forks source link

Normals are wrong #54

Open RumbleballTheReal opened 2 years ago

RumbleballTheReal commented 2 years ago

Edgesplit The two left boxes show how it should look (flat), the two right boxes show how it actually is imported (looks round).

NormalIssue.zip (All boxes look round)

In your code I found

    # not sure if this is the best way to read normals
    m.calc_normals_split()

Well, it definitely is not the best option, as you change the normals. It would be better to display an export error when the mesh is not triangulated (thus you do not need to recalculate the normals) and let the user handle the triangulation so he can affect the normals. The exporter should just take the normals as is, not modify them.

There is also a weirdo I struggle to understand:

    loops = m.loops
    num_loops = len(loops)

    normals = np.empty(num_loops* 3, np.float32)
    loops.foreach_get("normal", normals)

You read through the loops and get the normals from there. Here are two issues: 1: the normals are 0,0,0 when you remove the _m.calc_normalssplit(). That means the normals of the mesh in the viewport are entirely ignored. 2: The loops array is much bigger then the vertex array -> the normals array is much bigger then the vertex array (they should have the same size). According to the docs of blender: Mesh.loops (reference a single vertex and edge). But the debugging shows that there are much more loops then vertices.

Vertex normals can be accessed via mesh.vertices[0].normal I modified the code to read the normals this way, but unreal now fails to import the mesh. Somehow the far to big normals array was correct, and the correct normal array was wrong.

Edit: Ok, I checked Unreals importer code and further read Blender docs, I see the issue you were facing...

RumbleballTheReal commented 2 years ago

I investigated it further and here is the fix: Blender: Add modifiers: -Triangulate -Weighted Normals -Edge Split

Unreal: Unreals Datasmith importer has issues with reimporting. It detects changed mesh files and reimports them, but the asset itself is not updated correctly with the imported data (though the mesh asset shows als changed). Delete the Datasmith content for the scene and import everything anew