Hi all
I think there is an issue with importing vertex colors from Blender files. It seems that the newer Blender versions are not writing the vertex colors in the mclo struct anymore. Instead they write them to mloopcol since they’ve introduces bmeshes. Consequently no vertex colors are imported. So, I’ve added a small patch which should also solve an issue with the RGB color channels previousely being mixed up.
Could someone rievew that and add the patch? Furthermore, there is an issue when I triangulate the faces in Blender, I get an IndexOutOfBoundsException on the example file. There must be something with the quad vs tris handling in the MeshBuilder class, but I haven’t figured it out.
[java]
Index: MeshHelper.java
— MeshHelper.java (revision 10818)
+++ MeshHelper.java (working copy)
@@ -486,21 +486,36 @@
* this exception is thrown when the blend file structure is somehow invalid or corrupted
*/
public List<byte[]> getVerticesColors(Structure meshStructure, BlenderContext blenderContext) throws BlenderFileException {
-
Pointer pMCol = (Pointer) meshStructure.getFieldValue("mcol");
-
List<byte[]> verticesColors = null;
-
List<Structure> mCol = null;
-
if (pMCol.isNotNull()) {
-
verticesColors = new ArrayList<byte[]>();
-
mCol = pMCol.fetchData(blenderContext.getInputStream());
-
for (Structure color : mCol) {
-
byte r = ((Number) color.getFieldValue("r")).byteValue();
-
byte g = ((Number) color.getFieldValue("g")).byteValue();
-
byte b = ((Number) color.getFieldValue("b")).byteValue();
-
byte a = ((Number) color.getFieldValue("a")).byteValue();
-
verticesColors.add(new byte[] { b, g, r, a });
-
}
-
}
-
return verticesColors;
-
List<byte[]> verticesColors = null;
-
-
if(isBMeshCompatible(meshStructure)){
-
Pointer pMloopcol = (Pointer) meshStructure.getFieldValue("mloopcol");
-
if (pMloopcol != null && pMloopcol.isNotNull()){
-
verticesColors = new ArrayList<byte[]>();
-
List<Structure> mCol = pMloopcol.fetchData(blenderContext.getInputStream());
-
for (Structure color : mCol) {
-
byte r = ((Number) color.getFieldValue("r")).byteValue();
-
byte g = ((Number) color.getFieldValue("g")).byteValue();
-
byte b = ((Number) color.getFieldValue("b")).byteValue();
-
byte a = ((Number) color.getFieldValue("a")).byteValue();
-
verticesColors.add(new byte[] { r, g, b, a });
-
}
-
}
-
} else {
-
Pointer pMCol = (Pointer) meshStructure.getFieldValue("mcol");
-
if (pMCol.isNotNull()) {
-
verticesColors = new ArrayList<byte[]>();
-
List<Structure> mCol = pMCol.fetchData(blenderContext.getInputStream());
-
for (Structure color : mCol) {
-
byte r = ((Number) color.getFieldValue("r")).byteValue();
-
byte g = ((Number) color.getFieldValue("g")).byteValue();
-
byte b = ((Number) color.getFieldValue("b")).byteValue();
-
byte a = ((Number) color.getFieldValue("a")).byteValue();
-
verticesColors.add(new byte[] { r, g, b, a });
-
}
-
}
-
}
-
return verticesColors;
}
/**
[/java]
This is the test .blend file: http://www.filedropper.com/test_32
it contains a cube with each side having a different color.
2 Likes
Sorry for bumping this. But the vertex colors from blend files don’t work and I suppose they used to work, but not anymore.
@Kaelthas
Would you mind having a look at my suggestion? Thank you.
^^ Never forget the @persons if you know who is working on that, it#s just to easy to miss a post else.
Hey @arpagaus.
yes I will take a look at it.
Could you please upload the file once more (and maybe soewhere else)? I cannot download it, I am getting redirected to upload page instead of download one …
2 Likes
@arpagaus I just made a commit.
Please update and let me know if it is working now 
2 Likes
@Kaelthas
Thank you for including the patch! And it’s my bad I forgot to mention you in the post 
Now, it’s almost correct. You’ve forgot to include the “b, g, r, a” -> “r, g, b, a” change from my patch. Currently the colors are not correct.
[java]
Index: MeshHelper.java
— MeshHelper.java (revision 10890)
+++ MeshHelper.java (working copy)
@@ -496,7 +496,7 @@
byte g = ((Number) color.getFieldValue(“g”)).byteValue();
byte b = ((Number) color.getFieldValue(“b”)).byteValue();
byte a = ((Number) color.getFieldValue(“a”)).byteValue();
[/java]
And as I’ve said as soon as I triangulate a cube, I get the following Exception:
Caused by: java.lang.IndexOutOfBoundsException: Index: 36, Size: 36
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at com.jme3.scene.plugins.blender.meshes.MeshBuilder.appendFace(MeshBuilder.java:222)
at com.jme3.scene.plugins.blender.meshes.MeshHelper.readBMesh(MeshHelper.java:378)
at com.jme3.scene.plugins.blender.meshes.MeshHelper.toMesh(MeshHelper.java:128)
at com.jme3.scene.plugins.blender.objects.ObjectHelper.toObject(ObjectHelper.java:160)
at com.jme3.scene.plugins.blender.BlenderModelLoader.load(BlenderModelLoader.java:70)
... 9 more
Could you have a look at this and possibly fix it?
I’ve uploaded the test-file here now: https://dl.dropboxusercontent.com/u/14799295/test.blend
Shouldn’t disappear anymore 
1 Like
@arpagaus I made two commits today.
One fixes the colors. The bug was there because it was probably a bug in blender.
I made some research and it turned out that in blender 2.62 and below the color factors were misplaced in the color structure.
The “r” field held the blue factor value and the “b” field - the red one.
Since blender 2.63 it was fixed and that is what caused bad color loading.
The fix I applied takes the blender version into account so it should work nicely in all cases now.
The other commit fixes the bug you had in stacktrace. Check it out - I hope it will work fine now 
3 Likes
@Kaelthas
Cool, thank you! I thought there must be a reason for the weired color order
I didn’t test it with 2.62 and wasn’t aware of that 
I’m really sorry for keeping you busy, but while the latter bugfix solves the exception, now the colors for quads get mixed up.

Those cubes looks the same in Blender, but the left one is built from quads and the right one is triangulated.
Here is the blend file:
https://dl.dropboxusercontent.com/u/14799295/test.blend
1 Like
@arpagaus
I will take a look on it today 
And do not worry to keep me busy. The more bugs will be found the more fixes will be hopefully applied 
I am relly going to make this importer as good as possible.
3 Likes
@arpagaus
The problem is more complicated than I expected.
Vertex colors are stored in a loop order rather than in vertex order in the blender structures.
I will need to rearrange mesh building a little. This will be a nice opportunity for preparing a ground for loading line and point meshes.
But unfortunately this will take some time.
As a workaround I suggest you triangulate your mesh before loading it. This should help for now.
I will let you know when I am done with it 
2 Likes
@Kaelthas
Okay, thank you. Looking at it myself I was afraid it might no be that easy. But using triangulation as a workaround for now should be okay.
@arpagaus
I just commited the fix. Please update to the newest version and let me know how it works.
Hope it is fine now 
1 Like
@Kaelthas
Great job! Thank you. Works like a charm
I’ve tried it with quads and with triangulated meshes and it works both ways.
Topic is closed for me.