Python: How to Put Blender Data into a Text File

Oh good.

So you want to get a text file with the coordinates of all the objects in a Blender scene? Luckily, Python has some wonderful built-in functions that can do exactly that.

Commands to Note:

#Open a text file (the file won’t actually open on your screen)
#Use \\ instead of \ in the file path
#The second argument is set to ‘w’ for ‘write’
NameForFile=open(‘File Path’,’w’)

#Type something into it
NameForFile.write(‘TextToInsert’)

#Save and close out the file
NameForFile.close()

#Convert x into a string
str(x)

#Convert x into an integer
int(x)

The Code

import bpy

blenderCipher=open('C:\\Users\\Li\\Desktop\\Cipher 2.0.txt','w')

for obj in bpy.context.selected_objects:
blenderCipher.write('\n'+obj.name+'\n')
blenderCipher.write(str(int(obj.location.x)) + str(int(obj.location.y)) + str(int(obj.location.z)))

blenderCipher.close()

The beginning of the code is pretty straightforward. You tell the computer to import the Blender stuff (import bpy) so it can do Blender things. Then you tell it to open a notepad file, one that we can call blenderCipher for short. Then we start a for loop that will cycle through all the selected objects in the current scene.

Inside the loop, we want it to write the object name and toss in an ‘\n’ or two to make new lines. The tricky bit is the part where we put in the object location—blenderCipher.write(str(int(obj.location.x)).

It looks god awful, but I swear it’s necessary. To understand why, here’s a tangent on data types.

Why Do Data Types Matter in Python?

Because if you don’t use the right one, you will get errors. I know this because I just spent a half hour dealing with this one:

Hey look. Another error.
*Sidenote: Errors in Python will pop up in the Blender System Console.

When something goes wrong the System Console will attempt to point out where you’ve screwed up. In this case, the clue it gave me was:

TypeError: must be str, not float

That’s because functions are picky bastards. Some of them won’t take numbers. They want a block of text.

The write function expects a string which is just that—a block of text. But Blender stores the object’s x coordinate (obj.location.x) in a float—a number.

Which is why you can’t use blenderCipher.write(obj.location.x). You have to put the float into a string, like so: blenderCipher.write(str(obj.location.x)).

If you run it and open the notepad document, that’ll get you here:

Oh my. It appears to be working.

Don’t like numbers with fractions? There’s a different data type for that. Floats have decimals. Integers are whole numbers. If you convert the float to an integer, it’ll shorten up the numbers nicely. Just remember you still have to convert it to a string to write it to the file—blenderCipher.write(str(int(obj.location.x)).

Hey, it works!

Ta da!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: