On 11/11/2015 at 05:50, xxxxxxxx wrote:
I am using numpy for array calculations on a c4d.pointobject. The manipulations within numpy are super fast, but it turns out that converting to and from c4d.Vectors is a real bottleneck. Since this is an essential step when using numpy, I was hoping that someone else has already found a good solution for it?
The fastest code I could come up with is attached below. Having a look at the execution times is interesting, because it shows that the internal getting and setting of vectors is much faster than the conversion to and from numpy of those vector objects.
list2np : 0.455179
np manipulation: 0.067916
np2list : 0.439967
c4d.SetAllPoints : 0.030023
Does anybody have suggestions as to further speed up this code? Especially the np2list function is critical, since it has to be called every frame (when used in a generator). I am open to exotic solutions such as cython, numba or whatever, as long as I can get it working on my machine.
Your help is really appreciated!
from c4d import Vector
import numpy as np
#import c4d2numpy as npc
now = time.clock
Vec = Vector
def ipts(pts) :
for p in pts:
def tprint(tl) :
t0 = [t for t in tl]
s0 = [t for t in tl]
print ' '
for i,t in enumerate(t0) :
if i==0: continue
print s0 _, t-t0[i-1]
def list2np(lst) :
A = np.fromiter(ipts(lst), dtype=np.float, count=3*len(lst))
return A.reshape((len(lst), 3))
def np2list(A) :
return map(Vec, A[:,0], A[:,2], A[:,1])
def main() :
t = 
t.append( (now(), 'start' ) )
pts = op.GetAllPoints()
t.append( (now(), 'c4d.GetAllPoints:' ) )
A = list2np(pts)
t.append( (now(), 'list2np :' ) )
B = A + 10. * np.random.random((A.shape))
t.append( (now(), 'np manipulation:' ) )
pts = np2list(B)
t.append( (now(), 'np2list :' ) )
t.append( (now(), 'c4d.SetAllPoints:' ) )
On 11/11/2015 at 14:09, xxxxxxxx wrote:
mmm, quiet here…
After some further investigation I found the numpy function "np.frompyfunc" and it does what it says pretty fast. For those of you interested, see an example of the code below. It executed in about half the time as the previous np2list, in 0.288461 sec.
Meanwhile, I've played around with multiprocessing a bit, but the overhead for a function with return value seems just too big, it only takes longer on more processes. So I am fairly happy with his latest result, it's still about three times faster than an ordinary for loop.
But if anybody has a better suggestion, I would love to hear it!
vec_array = np.frompyfunc(Vector, 3, 1)
def np2list_2(A) :
y & z are reversed because of left-handed
system in cinema 4d
pts = (vec_array(A[:,0], A[:,2], A[:,1])).tolist()
On 12/11/2015 at 02:08, xxxxxxxx wrote:
not exacly for conversation (as name of topic), but i made several tests by wrapping into pyCuda or cythonize script-body(with numpy code too). And found it was faster. I test with pyVoro(python extesion for Voro++) and pyOpenVDB.
On 12/11/2015 at 04:56, xxxxxxxx wrote:
What do you mean by 'not exactly for conversion'? It is meant to be working from within c4d.
PyVoro, PyCuda, PyOpenVDB sound a little too exciting for me. As far as I can see, they require building extra packages, and I am on mac osx, so it'd be different than on windows.
Cython might be an option though, but I am having trouble typing stuff right. For example, do you know how to cdef/ctypedef the Vector object from c4d? Without it, I only got a marginal increase in performance. How much (%) did you get, btw?
On 12/11/2015 at 06:18, xxxxxxxx wrote:
several colleagues and i had tasks for scripting environment to speed up conversation of mesh/procedural data to numpy arrays and then to pyVoro/OpenVDB. Yes, i compiled these py-extesions. also a few processing was boosted in cuda/gpu compute environment or slightly modified cython.
I forgot to point at such moment - Hermen, try to create topic somewhere else, forum, social network in your region, ... from my experince and search over support forum - teamers do not support 3d party's plugins and extensions.
For example french4d has topic about speed algos in python
i use network between several math/programming russian universities
On 12/11/2015 at 15:16, xxxxxxxx wrote:
OK, I've got cython running as well, but the performance increase is very marginal compared to the function I posted previously. That's good enough for me, I guess, and if I need more speed, I'll check elsewhere. Thanks for your suggestions!