Degrees instead of radians

Hey there everyone.
A while ago i worked on a script to export locations, rotations & the scale of objects to a .txt file so i can use Blender as a placement editor for game creation. I have just run into a problem with the rotations being written out as radians as apposed to the degrees shown in the “transform properties” window.
Anyone know how to write out the information to degrees?
Here is the original script.


bl_info = {
    "name": "Object Location, Scale, Rotation & Custom Properties",
    "author": "Robert Hogg",
    "blender": (2,69,0),
    "version": (0,0,1),
    "location": "File > Import-Export",
    "description": "Object Location, Scale, Rotation & Custom Properties.",
    "category": "Import-Export"}


import bpy
from bpy_extras.io_utils import ExportHelper




class ExportMyFormat(bpy.types.Operator, ExportHelper):
    bl_idname = "export_my_format1.txt";
    bl_label = "Export .Txt";
    bl_options = {'PRESET'};
    filename_ext = ".txt";
    
    filepath = bpy.props.StringProperty("FILE_PATH")
    global a


    def execute(self, context):


        file = open(self.filepath,'w')
        file.write('[start]')
        file.write("
")
        for a in bpy.context.selected_objects:
            file.write('Object:'+a.name)
            file.write("
")
            file.write('X#:'+str(a.location[0]))
            file.write("
")
            file.write('Y#:'+str(a.location[2]))
            file.write("
")
            file.write('Z#:'+str(a.location[1]))
            file.write("
")
            file.write('Xrot#:'+str(a.rotation_euler[0]))
            file.write("
")
            file.write('Yrot#:'+str(a.rotation_euler[2]))
            file.write("
")
            file.write('Zrot#:'+str(a.rotation_euler[1]))
            file.write("
")
            file.write('Xsc#:'+str(a.scale[0]))
            file.write("
")
            file.write('Ysc#:'+str(a.scale[2]))
            file.write("
")
            file.write('Zsc#:'+str(a.scale[1]))
            file.write("
")
            
            
            objname = a.name
            ob = bpy.data.objects[objname]
            for e in ob.event_settings:
                file.write("%s=%s
" % (e.event_field, e.event_value))
            
            file.write('[next]')        
            file.write("

")
            
        file.write('[end]')
    
        return {'FINISHED'};
    
    def invoke(self, context, event):
        context.window_manager.fileselect_add(self)
        return {'RUNNING_MODAL'}
    


    
def menu_func(self, context):
    self.layout.operator(ExportMyFormat.bl_idname, text="My Model Format1(.txt)");


def register():
    bpy.utils.register_module(__name__);
    bpy.types.INFO_MT_file_export.append(menu_func);


def unregister():
    bpy.utils.unregister_module(__name__);
    bpy.types.INFO_MT_file_export.remove(menu_func);


if __name__ == "__main__":
    register()


You should use the math module :

from math import *

deg = degrees(rad)

Thanks for that DoubleZ.
Would i implement this like:

file.write(‘Xrot#:’+str(degrees(a.rotation_euler[0])))

That should work, although I would use a slightly different notation:

 file.write("Xrot#:%s" % degrees(a.rotation_euler.x))

Instead of a string, you might want to consider %f for float (e.g. fixed amount of decimal places, %.6f for six)

Thanks CoDEmanX
I have changed the code but keep getting an error when i run the script

Name Error: global name ‘degrees’ is not defined

The % operator is deprecated; the newer way uses the format function, e.g.

 file.write("Xrot#:{:.6f}".format(degrees(a.rotation_euler.x)))

You haven’t imported the math library.

Best wishes,
Matthew

Thankyou MCollett
Got it working, i tried to used the import math & had to change

file.write(“Xrot#:{:.6f}”.format(degrees(a.rotation_euler.x)))

to

file.write(“Xrot#:{:.6f}”.format(math.degrees(a.rotation_euler.x)))

:slight_smile:

MCollett: is it really deprecated or are there just other, more powerful alternatives?
Because it’s simple (=pythonic?!) and sufficient for common use cases.

Hmm, the ‘official’ line seems to have changed. I was recalling the comment in the 3.2 documentation, which says

The formatting operations described here are obsolete and may go away in future versions of Python. Use the new String Formatting in new code.

‘obsolete and may go away’ is exactly what is meant by ‘deprecated’.
But by 3.2.5 this had softened to

As the new String Formatting syntax is more flexible and handles tuples and dictionaries naturally, it is recommended for new code. However, there are no current plans to deprecate printf-style formatting.

while in 3.4.0 it has become

The formatting operations described here exhibit a variety of quirks that lead to a number of common errors … . Using the newer str.format() interface helps avoid these errors, and also provides a generally more powerful, flexible and extensible approach to formatting text.

So old hands may take their pick, but I see no point in a ‘newbie’ bothering with the old-style version.

Best wishes,
Matthew