0
$\begingroup$

I built a human model in Blender first and then loaded a bvh file into ths model. Now I have 200 frames which show a running scene in Blender. I can iterate every frame. I want to get all the vertex coordinates from the 200 frames. I did it with the following code segment, but, want is to get the vertex coordinates of the original human model.

Can someone help me?

for frame in range(scene.frame_start, scene.frame_end):
    scene.frame_set(frame)
    scene.update()
    bpy.ops.object.mode_set(mode='OBJECT',toggle = True)
    obj = bpy.context.active_object
    loops = len(obj.data.loops)
    verts = len(obj.data.vertices)
    mat = bpy.context.active_object.matrix_world
    vertex = [mat * verts.co for verts in obj.data.vertices]
    the_file = "pointCloud_f" + str('{:03d}'.format(scene.frame_current)) + ".ply"
    print("loading "+the_path + the_file+'\n')
    with open(the_path + the_file, 'w') as wFile:
        wFile.write(headerOfPLY)
        count = 0
        for vcol in obj.data.vertex_colors:
            colors={}
            for i in range(loops):
                v=obj.data.loops[i].vertex_index
                c = vcol.data[i].color
                colors[v]=c
        for j in range(len(vertex)):
            wFile.write('%f %f %f %d %d %d \n' %(vertex[j].x,vertex[j].y,vertex[j].z,int(colors[j].r*255),int(colors[j].g*255),int(colors[j].b*255)))
    print("writing completed!")
    wFile.close()
return {'FINISHED'}

Shape of frame 1 of the scene Shape of frame 1 of the scene

Shape of original human model Shape of original human model

$\endgroup$
1
$\begingroup$

The mesh data always contains the original locations of vertices. Not sure if it is the only way but you can use object.to_mesh to get a copy of the mesh data that is deformed.

import bpy

obj = bpy.context.active_object
scn = bpy.context.scene

mod_mesh = obj.to_mesh(scn, True, 'RENDER')

for v in mod_mesh.vertices:
    print(v.co)

Note that this creates a new mesh object, so as you are iterating over each frame you will want to remove each copy as you go with -

bpy.data.meshes.remove(mod_mesh)
$\endgroup$
  • $\begingroup$ Thanks! It works. following question: is color information under vertex group? how can I get the color of each vertex correspondingly? $\endgroup$ – 龚文臣 Dec 6 '16 at 1:36
0
$\begingroup$

This is how I get the color of the deformed mesh, but I am not sure if it corresponds to vertex. For example, is color[0] the color of vertex 0? not sure

obj = bpy.context.active_object
mod_mesh = obj.to_mesh(scene,True,'RENDER')

loops = len(mod_mesh.loops)
verts = len(mod_mesh.vertices)

for vcol in mod_mesh.vertex_colors:
      colors={}
      for i in range(loops):
           v=mod_mesh.loops[i].vertex_index
           c = vcol.data[i].color
           colors[v]=c
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.