5
$\begingroup$

I'm still a python beginner and I'm writing a script/addon that moves vertices of a mesh following a "long" calcul every time frame changes. Something like :

import bpy

def MoveVertices(scene):
    obj = bpy.data.objects['Cube4']
    '''creating a copied mesh (.to_mesh) here to retrieve vertices positions later
    '+ setting some variables
    '''
    for v in obj.data.vertices:
        '''long calcul to find the vector
        ' like 20 lines of code some if statements and calculus
        '''
        v.co += vector
    ''' setting some variables + removing tmp mesh (.remove)'''
    return {'FINISHED'}

def register():
    bpy.app.handlers.frame_change_post.clear()
    bpy.app.handlers.frame_change_post.append(MoveVertices)

def unregister():
    bpy.app.handlers.frame_change_post.remove(MoveVertices)

if __name__ == "__main__":
    register()

What happens of course is when there is a lot of vertices the system starts to slow down a lot. I know for performance sake I should write in C/C++ and do a custom blender build but I was wondering if there isn't a more efficient way to move bunch of vertices in "real time" with python.

Here are my questions :

  1. does a straight loop through all vertices like I'm doing is the best way to go concerning calcul speed?
  2. would using bmesh help in that matter?
  3. what about slicing the amount of vertices and launching the calcul in parallel for each chunks (with a modal timer operator) ? Would that be an idea ?
  4. is there something I'm missing in the python api that could help ?

Thx a lot for your help.

$\endgroup$
8
  • $\begingroup$ Is it correct that you want to move every vertex of your mesh by the same vector? Is the mesh in edit or object mode? $\endgroup$
    – CodeManX
    Nov 18, 2014 at 19:15
  • $\begingroup$ well the value of "vector" changes actually for each vertex depending on my calcul : vector = f(frame,other parameters set by object custom properties and vertex weight). The mesh is in object mode. thank you for any help. – $\endgroup$ Nov 18, 2014 at 19:47
  • 2
    $\begingroup$ You may wanna use foreach_set(), that's the only chance to speed it up. $\endgroup$
    – CodeManX
    Nov 18, 2014 at 19:55
  • $\begingroup$ Blindingly optimizing is never a good idea, have you tried using the cProfile Python module to isolate where the bottleneck is? $\endgroup$
    – Mike Pan
    Nov 18, 2014 at 20:44
  • $\begingroup$ Thx guys! @CoDEmanX, I'll have a look at {foreach_set()}, it is interesting as it seems to be faster than looping through all vertices with {.co}. Mike Pan, I never used cProfile before but there is a 1st time for everything :D Let you know how it goes. $\endgroup$ Nov 18, 2014 at 21:11

1 Answer 1

1
$\begingroup$

foreach_set() is actually very fast but not usable in my case because I'm not always going through all vertices in all cases. It seems that the "for" loop is the best way to go.

Using bmesh actually is a little faster because it doen't refresh the scene each time the script changes the location of a vertex.

Thx @CoDEmanX and @MikePan for the help.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .