How can i use the value in column 'lon' and 'lat' as the position of my uv_sphere? For example,read the values from line 4, (116.4559081, 39.9139083), and make these 2 numbers as my uv_sphere x and y position in blender? and gives different colors for different mode, for example i have 4 modes in the csv , i want to make the uv sphere that represents the bus mode to be red, and subway to be green , rail to be blue and tram to be yellow., and use the row ['from'] and ['to'] to connect the nodes with a cylinder with x scale 0.5 , y scale 0.5 and the z scale with the value in ['length'] value.
You can use the csv library to read and parse CSVs.
If you have a basic bus object on a different layer (or a sphere called
bus), this script will duplicate it and then assign the longitude and latitude values from your CSV as the new object's x and y values, for each row in your CSV.
import bpy, csv fp = "C:/csvs/buses.csv" with open( fp ) as csvfile: rdr = csv.reader( csvfile ) for i, row in enumerate( rdr ): if i == 0: continue # Skip column titles lon, lat = row[3:5] # Generate UV sphere at x = lon and y = lat (and z = 0 ) bpy.ops.mesh.primitive_uv_sphere_add( location = ( float(lon), float(lat), 0 ) )
I used this CSV to test this code:
mode,country/city,ID,lon,lat bus,CN/Beijing,bus81254,65.98592786,51.75899959 bus,CN/Beijing,bus73859,92.83295925,87.98592343 bus,CN/Beijing,bus52924,88.43513405,88.79596166 bus,CN/Beijing,bus52141,92.68713387,15.49215037 bus,CN/Beijing,bus8170,36.0964094,42.77106261 bus,CN/Beijing,bus46545,84.46655319,43.23791824 bus,CN/Beijing,bus60033,66.14547792,61.71886078 bus,CN/Beijing,bus63664,35.46744528,4.412149752 bus,CN/Beijing,bus62759,89.04704861,93.61280167 bus,CN/Beijing,bus72522,25.00040058,9.672330218 bus,CN/Beijing,bus55786,96.79310251,73.2168682 bus,CN/Beijing,bus55381,44.75153181,23.6187773 bus,CN/Beijing,bus21913,39.26107112,98.37814343 bus,CN/Beijing,bus2981,35.8751682,23.26682901 bus,CN/Beijing,bus52068,56.09661959,97.26439994 bus,CN/Beijing,bus59329,54.47486082,87.98768438 bus,CN/Beijing,bus40450,6.994144595,29.2284396 bus,CN/Beijing,bus51603,95.90526567,26.46823662