0
$\begingroup$

I am using this script which I got from the internet and it works perfectly for 2.79. Please could someone help adding selected objects rotation to export to the same txt file? In degrees.... Thanks in advance.

# import the necessary modules we need
# in our case, blender's python API and python's os module
import bpy, os

# get the current selection
selection = bpy.context.selected_objects

# initialize a blank result variable
result = ""

# iterate through the selected objects
for sel in selection:
    # get the current object's dimensions
    dims = sel.dimensions
    # write the selected object's name and dimensions to a string
    result += "%s - %.03fm x %.03fm x %.03fm\n" % (sel.name, dims.x, dims.y, dims.z)

# get path to render output (usually /tmp\)
tempFolder = os.path.abspath (bpy.context.scene.render.filepath)
# make a filename
filename = os.path.join (tempFolder, "newfile.txt")
# open a file to write to
file = open(filename, "w")
# write the data to file
file.write(result)
# close the file
file.close()
$\endgroup$
  • $\begingroup$ This is kind of hard to read $\endgroup$ – WhatAMesh Feb 27 at 7:06
1
$\begingroup$

You already outlined everything that needs to be done. Rotation can be reached at .rotation_euler:

enter image description here

Note that you can find this information by typing stuff to the console and hitting ctrl+space for auto complete.

The values are in radians so you need an easy way to convert them. You can just multiply the values by 57.29577951308232, or you could do from math import degrees or from math import degrees, radians so you have functions that can convert between the two for you.

Everything from math is imported in the console so you could just get the degrees of x rotation of the active object by typing degrees(bpy.context.object.rotation_euler.x) in the console to check that this indeed does work.

In a script this would look something like:

import bpy
from math import degrees

result = ''

for o in bpy.context.selected_objects:
    #something else that you need to get to result
    n = o.name
    r = o.rotation_euler
    result += "%s - X:%.3f, Y:%.3f, Z:%.3f \n" % (n, degrees(r.x), degrees(r.y), degrees(r.z))
    # carry on to do whatever else
#write to file
$\endgroup$
  • $\begingroup$ May be more robust using o.matrix_world.to_euler() will return the visual global rotation of the object. If the object is rotated via constraint for example o.rotation_euler will not reflect this. Remember too py console has convenience variables C = bpy.context, D = bpy.data $\endgroup$ – batFINGER Feb 27 at 13:12

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.