1
$\begingroup$

To generate a point cloud i am using planes in blender, to compensate for unavailability of POINT data primitive. After reading the .ply point cloud file(PCD) the planes are added in the blender scene, initially blender starts off with scene creation very fast then keeps slowing down. Python script used is-

#This script has 2 different loops to 
# 1. To read the .ply file 
# 2. To create the scene by drawing the triangle..
import os
import subprocess
import bpy 
from math import *
from array import *
import time
from mathutils import Vector

# A file to record the time to draw each cycle of 1000 planes; 
# to evaluate blender's performance and to check if it slows down or not.

TimeFile=open("time_for_each_1000_planes.txt","w")  
SplatSize=0.01

########################### ADD THE GEOMETRY ###################################################################
#bpy.ops.mesh.primitive_plane_add(location=(0,0,0))
#bpy.ops.transform.resize(value=(SplatSize,SplatSize,SplatSize))
##bpy.ops.surface.primitive_nurbs_surface_sphere_add(location=(0,0,0))
#ob = bpy.context.object
#obs = []
sce = bpy.context.scene

path=os.getcwd()
print(path)

file=open("chariot.ply","r")

print("\n"+file.name)

for line in file:
    print(line)
    if(line == "end_header\n"):
        break

l=file.readline()
w=l.split()
base_x=float(w[0])
base_y=float(w[1])
base_z=float(w[2])

#for i in range(5):
    #line=file.readline()
count=0
vx=array('f')
vy=array('f')
vz=array('f')
nx=array('f')
ny=array('f')
nz=array('f')
r=array('f')
g=array('f')
b=array('f')
a=array('f')

for line in file:  
#for i in range(10000):
    #line=file.readline()
    word=line.split()
    vx.append(float(word[0]) - base_x)  
    vy.append(float(word[1]) - base_y)
    vz.append(float(word[2]) - base_z)
    nx.append(float(word[3])) 
    ny.append(float(word[4]))
    nz.append(float(word[5]))
    r.append(float(word[6])/255)
    g.append(float(word[7])/255)
    b.append(float(word[8])/255)
    a.append(float(word[9]))
    count+=1

print("\nVertices read ",count)

bpy.ops.material.new()

########################################start time################################
start_time=time.time()
loops=count/1000 
plane_cycle_time=time.time()  

for j in range(50):
    bpy.ops.mesh.primitive_plane_add(location=(0,0,0))
    bpy.ops.transform.resize(value=(SplatSize,SplatSize,SplatSize))
    ob = bpy.context.object
    obs = []
    #print("\n %d",j)
    #input("Vishesh")    
    for i in range(1000):
        try:
#            if j==1 :
#                input("V1")
            index=j*1000+i
            x=nx[index]
            y=ny[index]
            z=nz[index]  

            n=sqrt(x*x+y*y+z*z)
            theta=asin(y/n)
            phi=atan(x/z)
#            if j==1 :
#                input("V2")
            copy = ob.copy()

            copy.location += Vector((vx[index],vy[index],vz[index]))
            copy.rotation_euler = Vector((theta,phi,0))
            copy.data = copy.data.copy() # also duplicate mesh, remove for linked duplicate
            #copy.scale=Vector((SplatSize,SplatSize,SplatSize)) 

            ############ ADD MATERIAL COLORS###########
            mat=bpy.data.materials.new(str(index))
            mat.diffuse_color=(r[index],g[index],b[index])
            copy.active_material=mat
            ###########################################
            obs.append(copy)
            print("\n",i)
            #print("%f \t %f \t %f",r[i],g[i],b[i])
            #print(r[i] + "\t" + g[i] + "\t" + b[i]) 
            #bpy.ops.mesh.primitive_plane_add(location=(vx[i],vy[i],vz[i]), rotation=(theta,phi,0))
            #bpy.context.active_object.scale=(100,100,100)
            #print("\n",vx,vy,vz,nx,ny,nz,count)
        except Exception as e:
            print("\n"+str(e))
            input("V")



    print("\nPlanes drawn")
    for ob in obs:
        sce.objects.link(ob)
    print("\nObjects Linked")

    sce.update() # don't place this in either of the above loops!
    print("\nScene Updated")

    #################################### LINK ALL OBJECTS IN A SINGLE OBJECT ###################################################################

    #bpy.context.scene.objects['Lamp'].select=False
    #bpy.context.scene.objects['Camera'].select=False
    obs[0].select=False
    obs[0].select=True
    bpy.ops.object.join()

    bpy.ops.object.select_all(action='TOGGLE')
    bpy.ops.object.select_all(action='TOGGLE')
    bpy.ops.object.join()

    print("\nObjects Joined")
    del obs

    TimeFile.write(str(time.time()-plane_cycle_time)+"\n")
    print("time for 1000 planes \t" + str(time.time()-plane_cycle_time))
    plane_cycle_time=time.time()
    #input("\n\npress enter to exit ;")


#############################################################################################################################################

print("\nTime Taken\t",time.time()-start_time," Seconds")

####################################end time#########################################



TimeFile.close()
file.close()

Slow down in blender performance can be seen from the below data where each line represents the time taken in seconds to draw sets of consecutive 1000 planes.

26.79153299331665
26.65752387046814
28.741644144058228
30.28873300552368
32.21084189414978
33.99994492530823
35.764045000076294
37.720157861709595
39.598265171051025
41.57837796211243
37.80416202545166
39.386253118515015
40.98534393310547
42.07340693473816
43.78650403022766
45.42959904670715
47.141695976257324
48.8407940864563
50.68189787864685
52.37799596786499
53.14903998374939
54.75613212585449
56.50923180580139
57.913313150405884
59.77441906929016
61.81753492355347
63.92265701293945
65.804762840271
67.84588122367859
70.84805178642273
71.98911809921265
75.10729598999023
76.6093819141388
79.5775511264801
79.98557496070862
81.87668299674988
82.94774508476257
83.71978807449341
85.63689804077148
87.45800304412842
88.59806704521179
90.43417310714722
92.85531091690063
94.94843101501465
96.56852293014526
97.14555621147156
99.41268587112427
100.7247622013092
101.08678102493286
102.45886087417603

total time=3163 seconds

As can be seen time per 1000 planes, to draw the planes in the scene keeps on increasing.

Please help me fix this problem of blender speed slowdown and explain what is exactly going wrong??

$\endgroup$