J'aurai besoin d'un petit coup de main concernant une optimisation de code.
Actuellement, j'ai un petit script fonctionnel qui me permet de lire les informations d'un fichier texte se présentant de cette manière:
0.00000000 0.58435619 0.92419678 0.454902 0.403922 0.333333 -0.999999 0.000978 0.000978
0.00000000 0.86374521 0.72747529 0.458824 0.411765 0.341176 -0.999999 0.000978 0.000978
0.00000000 0.79955196 0.66729391 0.415686 0.32549 0.25098 -0.999999 0.000978 0.000978
...
C'est un très long fichier listant les points d'un nuage de point (Photogrammétrie). Les chiffres d'une ligne sont dans cette ordre : X,Y,Z,R,G,B,Nx,Ny,Nz
Mon scritpt ci-dessous, me permet de générer une cartographie colorimétrique des coordonnées XYZ de chaque point.
Voila le problème: Pour le moment tout fonctionne sur des listes de 10 000 coordonnées. Mais dès que je passe à 100K ça commence à devenir beaucoup trop long, sachant que mes listes vont dans l'avenir faire plusieurs millions. Serait-il possible de forcer mon script à lire plus intelligemment mes listes et éviter qu'à chaque boucle il recommence la lecture de liste à zéro?
- Code : Tout sélectionner
import bpy
import random
import sys
#WARNING!!! Normaliser le modèle 3D en positif X Y Z
# "Xforward" & "Yup" à l'export obj
#rajouter "map_Ka bake.png" dans le mlt du obj avant import CC
# Remove old images
for img in bpy.data.images:
bpy.data.images.remove(img)
#import PointCloud.txt
PointCloudText='/Users/goideleg/Desktop/WOOD/WOOD_10000point_floatRGB.txt'
#PointCloudtxt to coordinateList
with open(PointCloudText) as f:
coordinateList = []
for line in f:
line = line.split() # to deal with blank
if line: # lines (ie skip them)
line = [float(i) for i in line]
coordinateList.append(line)
# Grid Size
width= 100
height = 100
# Error message ratio aspect
if height < len(coordinateList) // width:
print("ERROR")
#Create Image
image_object = bpy.data.images.new(name='test', width=width, height=height,float_buffer=True)
image_object.generated_color = (0,0,0,0)
num_pixels = len(coordinateList)
def grid(x,y):
return x + width * y
# Set Point Color
def drawPointColor (x,y,R,G,B,A):
pixelNumber = grid(x,y)*4
image_object.pixels[pixelNumber] = R
image_object.pixels[pixelNumber+1] = G
image_object.pixels[pixelNumber+2] = B
image_object.pixels[pixelNumber+3] = A
#Draw Point Color
max = max(len(coordinateList),width)
for i in range(0,max,1):
j = i % width
k = i // width
if i < len(coordinateList):
R = coordinateList[i][0] # coordinate X = 0
G = coordinateList[i][1] # coordinate Y = 1
B = coordinateList[i][2] # coordinate Z = 2
drawPointColor(j,k,R,G,B,1)
#Show result in UV/image Editor
for area in bpy.context.screen.areas :
if area.type == 'IMAGE_EDITOR' :
area.spaces.active.image = image_object