Sir, I need some help from this forum.

I am new to blender & want to use scripting (I use VBA code in PowerPoint) to import various text data from CSV files. I have some data regarding a country like country name, capital, GDP & population in four different blocks in blender & it denotes as

For Block 1 ---> Text_country.1, Text_capital.1, Text_gdp.1, Text_population.1

For Block 2 ---> Text_country.2, Text_capital.2, Text_gdp.2, Text_population.2

For Block 3 ---> Text_country.3, Text_capital.3, Text_gdp.3, Text_population.3

For Block 4 ---> Text_country.4, Text_capital.4, Text_gdp.4, Text_population.4

Now I want to import all these data(for the different countries) from a CSV file.

Is it possible?


1 Answer 1


Yes. Here's an example that would read your data and create text blocks for each line. You may want to read the manual entry for [Text][1] for more details on editing the content of a text block.

import bpy
import csv

csvFilename = r'C:\tmp\info.csv'
index = 0
with open(csvFilename, 'r') as csvFile:
    csvreader = csv.reader(csvFile, delimiter=',', 
    for row in csvreader:
        print(f"{row[0]} is ({row[1]}, {row[2]}, {row[3]})")
        # Use the strings as you want before moving on to the next row
        if index != 0: # skip the header
            blockname = f"block{index:02d}"
            block = bpy.data.texts[blockname]
            block.write(row[0] + "," + row[1] + "," + row[2] + "," + row[3] + "," + row[3])
        index += 1```

You would, of course, change the filename from `r'C:\tmp\info.csv'` to the file you want to read.

Note that your spreadsheet has commas in the income values.  You'll have to either change the delimiter to something other than a comma, or convert the spreadsheet to CSV without the commas.

The csv file reader reads one line of data at a time, splitting it into an array where each value is in a separate entry.

  [1]: https://docs.blender.org/api/current/bpy.types.Text.html
  • $\begingroup$ thanks, sir for your quick response & helping me to educate about scripting in blender. sir, I want to replace all data from CSV file & I am trying like txt=bpy.data.objects['Text_country.1'] ; txt.data.body='france' # france will be fetched from CSV & so on. I will upload an image for a better understanding of where I used four blocks for example but in actuality, I want 30/40/ or any custom number of blocks. $\endgroup$
    – SAHU
    Apr 26, 2022 at 18:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .