I’m currently attempting to create an import addon. So far I’ve managed to add the import option to the menu, and read all of the data from the source file. And now I’m in the process of converting raw values I have to blender structures so they can be added to the scene.
Right now I’m running into the problem of what do to after creating a basic mesh. In terms of coding, I’ve managed to find two approaches to making a basic mesh. One is calling the from_pydata function:
vertices = []
for vertex in self.verts:
pos = vertex['pos']
x = pos[0]
y = pos[1]
z = pos[2]
vertices.append( (x, z, y) )
faces = []
for group in self.faces:
for i in range(0, group['faceCount']):
a = group['indices'][i * 3 + 0]['index']
b = group['indices'][i * 3 + 1]['index']
c = group['indices'][i * 3 + 2]['index']
faces.append( (a, b, c) )
mesh = bpy.data.meshes.new('Mesh')
mesh.from_pydata(vertices, [], faces)
mesh_object = bpy.data.objects.new('Mesh_Object', mesh)
bpy.context.scene.objects.link(mesh_object)
bpy.context.scene.objects.active = mesh_object
And the other is to create the mesh using the bmesh structure
bm = bmesh.new()
mesh = bpy.data.meshes.new('Mesh')
for vertex in self.verts:
pos = vertex['pos']
x = pos[0]
y = pos[1]
z = pos[2]
vert = bm.verts.new( (x, z, y) )
bm.verts.ensure_lookup_table()
for group in self.faces:
for i in range(0, group['faceCount']):
a = bm.verts[group['indices'][i * 3 + 0]['index']]
b = bm.verts[group['indices'][i * 3 + 1]['index']]
c = bm.verts[group['indices'][i * 3 + 2]['index']]
face = bm.faces.new( (a, b, c) )
face.material_index = group['materialIndex']
# face.loop bmloop sequence to add uv
bm.to_mesh(mesh)
bm.free()
mesh_object = bpy.data.objects.new('Mesh_Object', mesh)
bpy.context.scene.objects.link(mesh_object)
But I’m at a loss to where to go from there. I still have materials, textures and uv’s to implement. But I want to prioritize the bones, skinning and animation to put my time into getting those to work before going back and adding in the textures and materials.
For vertex weights, it’s pretty vanilla. For every vertex I have a list of up to four indices which reference a bone by index. And four weights between 0 and 1 corresponding to the weight for each indice.
0 (x, y, z) ( 0, 0, 0, 0) (1.0, 0.0, 0.0, 0.0)
1 (x, y, z) (0, 1, 0, 0), (0.5, 0.5, 0.0, 0.0)
So for example, like above, I have for vertex 0, i have four indices for bones (all 0) and 100% of the vertex is weighted to bone 0. And for vertex 1, four indices, 0 1 0 0 and 50% of the vertex is weighted to bone 0 and 50% is weighted to bone 1.
So I’m trying to find the syntax for how to implement this using the blender python api on my import script. For either bmesh, or the normal way. bmesh seems easier, but I can’t seem to find any code examples of it in the wild. And the normal way has examples, but it takes a long time to dig and read other people’s code in order to gain enough understanding to implement it into your own code.
So from what I can tell, for bmesh, there implementation has something to do with BMLayerAccessVert, and for normal, something to do with vertex groups. But all of the code and examples I’ve read only seem to attach a weight value and I can’t tell how bone indices are actually being assigned for each weight. I’m looking for code on how to do this, and can’t post intermediate code. Because I really am having a hard time finding hints on how to do anything beyond declaring a mesh from a list of vertices and faces.