Import data from txt file?

Hi guys,

I’m very new to python, but picking it up fairly quickly because I have some familiarity with other programming languages.

I just need some advice on a specific aspect.

I’m trying to visualise a vector field based on data in a text file. I’m having trouble working out how to import and reference the data.

The data is in the form of a space delimited text file with scientific notation. So, for example, a 3x4 array would look something like this:

0 12 3.45 5e7
2.23e-3 98 3 43
32e-12 2.45 23.4 234

At the moment I’m working with data that is 32x32, but I have arrays that are 128x128x128 and would visualise anything up to that size in future depending on what my system can handle.

Anyway, I wanted to avoid installing numpy to avoid any complications that may arise installing stuff, just until I get more familiar with python.

I tried doing it as a list, but not sure exactly how to access the elements, or if I need to do anything to help it recognize the scientific notation.

I know that the rest of my code works, because the following works

import bpy
import math
from math import pi

for x in range(1,32):
    for y in range(1,32):
        
        
        bpy.ops.mesh.primitive_cone_add(location=((x-17)/2,(y-17)/2,0),rotation=(0,0,0), )
        bpy.ops.transform.resize(value = (0.05, 0.05, 0.3) )
        bpy.ops.transform.rotate(value = (pi/2,), axis = (1,1,0),)


but it doesn’t work when I try to use data for each object from a text file

import bpy
import math
from math import pi



uExpFile = open(r'D:\simulations\Electromagnetic\images\uExp.txt')
uExp = []
for line in uExpFile.readlines():
    y = [value for value in line.split()]
    uExp.append(y)
uExpFile.close()

vExpFile = open(r'D:\simulations\Electromagnetic\images\vExp.txt')
vExp = []
for line in vExpFile.readlines():
    y = [value for value in line.split()]
    vExp.append(y)
vExpFile.close()




for x in range(1,32):
    for y in range(1,32):
        
        
        bpy.ops.mesh.primitive_cone_add(location=((x-17)/2,(y-17)/2,0),rotation=(0,0,0), )
        bpy.ops.transform.resize(value = (0.05, 0.05, 0.3) )
        bpy.ops.transform.rotate(value = (pi/2,), axis = (vExp[x,y],uExp[x,y],0),)

Any help with this would be appreciated :slight_smile:
Cheers.

wild guess:

change vExp[x,y] to vExp[x][y]

EDIT: and uExp the same way

niko

Thanks, I had already tried that and it didn’t work. I had changed it because I read something that said you can do it either way with lists. I ended up installing numpy, and it was pretty simple. As numpy is specifically for this sort of thing, there’s plenty of documentation on this sort of thing.

Anyway, now I have it working with:


import bpy
import numpy
import math
from math import pi
from math import sin
from math import sqrt
from math import pow


uExp = numpy.genfromtxt(r'D:\simulations\Electromagnetic\images\uExp.txt', delimiter = ' ')
vExp = numpy.genfromtxt(r'D:\simulations\Electromagnetic\images\vExp.txt', delimiter = ' ')

bpy.ops.object.camera_add (location=(0,-20,10),rotation=(pi/3,0,0), )
bpy.ops.object.lamp_add(location=(0,0,5), rotation = (0,0,0), )


for x in range(0,31):
    for y in range(0,31):
        
        
        bpy.ops.mesh.primitive_cone_add(location=((x-16)/2,(y-16)/2,0),rotation=(0,0,0), )
        bpy.ops.transform.resize(value = (0.05, 0.05, sqrt(vExp[x][y]*vExp[x][y]+uExp[x][y]*uExp[x][y])*1e7),)
        bpy.ops.transform.rotate(value = (pi/2,), axis = (vExp[x][y],uExp[x][y],0),)


Thanks again :slight_smile: