# Saving and loading object name location and rotation of a selection

I would like to know how I can save selected object names, location and rotation using Python in Blender 2.8 in a CSV.

And then using the same CSV apply this information to another blend file with the same objects in different locations in a new frame.

Beeing new to Blender Python scripting, I hope for some coding hints here.

so far i got this (works in console but not in script file)

import bpy
import os
ob_active = bpy.context.selected_objects
for obj in ob_active:
print(obj.name,obj.location,obj.rotation_euler)

• – Martynas Žiemys Dec 18 '19 at 15:45
• well its a start (and a funny joke), the thing i have problems with is exporting and importing to a .csv – Peter Dec 19 '19 at 7:42
• CSV is just comma separated values in a text file with .csv extension. The first Google result for "Python write to file" will help: w3schools.com/python/python_file_write.asp – Martynas Žiemys Dec 19 '19 at 11:00
• yes but the way blender logs those values (as in above of my script) isnt a normal table with values logge4d as <vector > etc – Peter Dec 19 '19 at 12:32
• Look into string formatting in Python. You need to convert the values you are using to strings as well. You can do: some_string_var = ', '.join([str(x) for x in obj.rotation_euler]) to get the rotation values separated by commas as a string. – Martynas Žiemys Dec 19 '19 at 13:02

Surely this has been asked before?

Rather than spending any more time searching for the elusive dupe, to the export part, I'm sure it's out there

Here is an example using f-string formatting.

import bpy
context = bpy.context

def sfmt(s):
return f"{s :10s}"

def vfmt(vec):
return ", ".join(f"{v : 10.4f}" for v in vec)

for obj in sorted(context.selected_objects, key=lambda o: o.name):
output = ",".join((
sfmt(obj.name),
vfmt(obj.location),
vfmt(obj.rotation_euler),
))
print(output)


Output of default scene, remember rotations are in radians.

Camera    ,    7.4811,    -6.5076,     5.3437,    1.1093,     0.0000,     0.8149
Cube      ,    0.0000,     0.0000,     0.0000,    0.0000,    -0.0000,     0.0000
Lamp      ,    4.0762,     1.0055,     5.9039,    0.6503,     0.0552,     1.8664


A number of other csv related questions.

How to read a csv file and use the values as x and y points in blender?

Massive import with CSV file

Moving and rotating using CSV

Generate and edit csv-file to reimport and use it with blender

How do I move an object using a csv file?

How to import a 3d object by csv file

• Actually prob shouldn't restrict name string to 10 characters. For any length if put last it wont naff formatting. math.degrees(v) will convert if degrees needed for csv. – batFINGER Dec 19 '19 at 14:47