obj.location & obj.rotation_euler help request

Greetings Python masters, i am seeking your help with one script, since my skill in python equals 0.
I am trying to export Location, Rotation and Scale values from F-Curve key frames.
The data will be used to create animations for the Warzone 2010 game.
This is the script i found and modified by adding obj.rotation_euler and obj.scale.

mport bpy


scn = bpy.context.scene
obj = bpy.context.active_object


for f in range(scn.frame_start, scn.frame_end+1):
    # use frame_set() so that keyed values are updated
    scn.frame_set(f)
    print(scn.frame_current, obj.location, obj.rotation_euler, obj.scale)

The output looks like this:
<Vector (0.0000, 0.0000, 0.0000)> <Euler (x=0.0000, y=0.0000, z=0.0000), order=‘XYZ’> <Vector (1.0000, 1.0000, 1.0000)>
1 <Vector (0.0067, 0.0000, 0.0017)> <Euler (x=0.0000, y=-0.0117, z=-0.0000), order=‘XYZ’> <Vector (0.9990, 1.0000, 0.9990)>
2 <Vector (0.0268, 0.0000, 0.0071)> <Euler (x=0.0000, y=-0.0335, z=-0.0000), order=‘XYZ’> <Vector (0.9990, 1.0000, 0.9990)>
3 <Vector (0.0600, 0.0000, 0.0172)> <Euler (x=0.0000, y=-0.0755, z=-0.0000), order=‘XYZ’> <Vector (0.9990, 1.0000, 0.9990)>
4 <Vector (0.1058, 0.0000, 0.0337)> <Euler (x=0.0000, y=-0.1345, z=-0.0000), order=‘XYZ’> <Vector (0.9990, 1.0000, 0.9990)>

Here are my questions:
1, Is it possible to add one more decimal value to location ? for example 0.00672 instead of 0.0067
2, How could i add recalculation of rotation from radians to degrees in the script?
I am grateful for any advice or documentation link.
My Best Regards,
Astorian

You can get the maximum precision from the Vector data by accessing the components of the vector directly.


# Instead of print(obj.location)
print(obj.location.x, obj.location.y, obj.location.z)

That code will just print out the coordinate values, it won’t include the enclosing ‘<Vector (’ and ‘)>’ text but you will get the full resolution of the components.

Likewise, if you want your rotations output in degrees you can access the components and convert them yourself.


from math import degrees

# instead of print(obj.rotaton_euler)
print(degrees(obj.rotation_euler.x), degrees(obj.rotation_euler.y), degrees(obj.rotation_euler.z))

This also removes the “<Euler (” text. But in general for these kinds of outputs you don’t want that extra text. You want to output the raw values and separate the fields by some delimiter (a comma, tab, or | are common choices). This makes reading the file back in much easier.

  1. What you print is only a string for display, the result of Vector.str(). It limits the number of decimal places to 4.

You could use the to_tuple() method instead, which accepts a precision-parameter, or use custom string formatting:

v = Vector((0.123456789, 0, 0))
print(v.to_tuple()) # prints (0.12345679104328156, 0.0, 0.0), note the rounding error
print(v.to_tuple(5)) # prints (0.12346, 0.0, 0.0)

"%f %f %f" % v[:] # prints '0.123457 0.000000 0.000000'

"%.5f %.4f %.3f" % tuple(v) # prints '0.12346 0.0000 0.000'

"{:.3} {} {}".format(*v) # prints '0.123 0.0 0.0'

  1. There’s a utility functions in the math module:
from math import radians, degrees

print(round(degrees(3.141), 1)) # ~PI in degrees: 180.0

print(radians(270)) # 270° in radians 4.71238898038469

list(map(degrees, bpy.context.object.rotation_euler))
# [-13.856588330538813, 18.71071583498131, 47.983940145888404]

Thank you so much kastoria and CoDEmanX for your help and explanation. This helped me a lot in the progress.
May the source be strong with you :slight_smile:
Astorian