0

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)
Peter
  • 2,344
  • 1
  • 12
  • 26

1 Answers1

3

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

batFINGER
  • 84,216
  • 10
  • 108
  • 233
  • thanks your vfmt code is realy handy – Peter Dec 19 '19 at 14:42
  • 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