0
$\begingroup$

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.

(it's not about bones it's about normal meshes).

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)
$\endgroup$
  • $\begingroup$ That's python in blender, this is Python in Blender $\endgroup$ – Martynas Žiemys Dec 18 '19 at 15:45
  • $\begingroup$ well its a start (and a funny joke), the thing i have problems with is exporting and importing to a .csv $\endgroup$ – user219279 Dec 19 '19 at 7:42
  • $\begingroup$ 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 $\endgroup$ – Martynas Žiemys Dec 19 '19 at 11:00
  • $\begingroup$ yes but the way blender logs those values (as in above of my script) isnt a normal table with values logge4d as <vector > etc $\endgroup$ – user219279 Dec 19 '19 at 12:32
  • $\begingroup$ 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. $\endgroup$ – Martynas Žiemys Dec 19 '19 at 13:02
2
$\begingroup$

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

csv file reading

Exporting coordinates of vertices to CSV

Import coordinates from CSV and create sphere at each position

$\endgroup$
  • $\begingroup$ thanks your vfmt code is realy handy $\endgroup$ – user219279 Dec 19 '19 at 14:42
  • $\begingroup$ 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. $\endgroup$ – batFINGER Dec 19 '19 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.