Exporting Mesh Data To A TXT-File

In this followup of our Blender 2.5 video tutorial on Creating a Mesh from Python Data, we will be writing a very basic mesh exporter to accommodate for our ‘custom objects’ add-on we wrote in the previous part. We will also be updating our previous add-on, as there has been another API update.

We will first be going over how Blender handles faces and vertices internally, as well as how to present them in a usable manner. We will then go ahead and write this data to a simple txt-file from which we can easily copy-paste what we want for use in our previous script.

Leave Comment


31 Responses to “Exporting Mesh Data To A TXT-File”
  1. Posts: 22

    at the top of the article it says: “April 29, 2011 | by Jonathan Williamson | Intermediate, Patrick Boelens, Tutorials, scripting ”

    Should it be: “By: Patrick Boelens”? =)

    Thanks for the vid!

  2. Posts: 5
    Attila Toth says:

    Very helpful tutorial,thank you.I would like to see more Blender python tutorials.

  3. Posts: 2
    hani says:

    Great Tut, thank you very much Patrick, waiting 4 the next 1

  4. Posts: 12
    Henri Salo says:

    Thank you for these awesome scripting tutorials, keep them coming! :)

    Just wanted to point out that as of Blender 2.57 (maybe even earlier?) the console window is hidden by default.
    The console can now to be enabled through ‘Help > Toggle System Console’.

    • Posts: 12
      Henri Salo says:

      Whoops, forgot to mention that this is the case on windows, don’t know whether it’s true on other OS’s as well. Maybe someone else can tell?

      • Posts: 225

        Oh, thanks for letting me know! I just checked and this is not the case on OSX unfortunately. I’ll be sure to mention this in the next tutorial though. =)

  5. Posts: 27
    dani says:

    sweet….thanks a lot Patrick

    Just an question: if I run blender and I run my script in blender(alt+p). I see in the Info Window (Expand on the Info Header) some debug information. Is this the new place for the Debug information in blender 2.6?

    thanx a lot for your work.


    • Posts: 225

      Oh, I never noticed that before. It just seems to show the last operator you called though, no useful logs or prints.

  6. Posts: 10
    SCUEY says:

    Great! Thanks for the help on this Patrick. As I mentioned before, this will really help save me some time on my current project. I was not expecting something this simple actually. You’ve made me realize I could have figured this out for myself, but this saved me a lot of time and it works great!

    In fact, I’ve made an improvement (in my opinion) by writing the vertex data and face data each to its own text datablock in Blender. To me this is more convenient because then you don’t have to leave Blender to find your vertex/face data in an external file. Also, it removes the need to add spaces in your text file to separate the datasets.

    I’ve pasted my operator class here: http://www.pasteall.org/21308/python

    If anyone’s interested in doing it this way, just replace the operator class in the video with the one at my link above.

    Get as advanced as you want with these bpy tutorials, Patrick. You’re doing great work!

    • Posts: 225

      Hey Scuey,

      Blender-Python makes perfect sense once you get the hang of it, so yes, sometimes the answer turns out to be a lot simpler than expected. =P

      I love your idea of saving the data out to text blocks btw! I’d like to make one suggestion on it though: Rather than iterating though all the text-files, you could do something like this instead:

      numTextFiles = len(bpy.data.texts) #so the last one is bpy.data.texts[numTextFiles-1]
      #then for clarity:
      bpy.data.texts[numTextFiles-1].name = bpy.context.active_object.name + “_vertices” #or _faces, of course

      But again, great idea and thanks for the input! =)


  7. Posts: 8
    Hans Erickson says:

    Patrick, I have certain heroes in my life and you have just become my python hero! I have finally been able to write useful stuff for my blends! I have written a neat panel for bone layers and sliders very customized thanks to your great tuts. http://twitpic.com/4s7350

    This last series is blowing my mind with possibilities. Keep them comming!

    • Posts: 225

      Hey, thank you so much! =) The panel’s looking great there! I actually checked out your site a couple of days ago and it seems you’re definitely on the right track. Best of luck with your animation!


  8. Posts: 21

    I totally agree, thank you Patrick you’ve opened the door wide open for me.
    With the new crysis sdk being released I was wondering if I could possible do an exporter… its still doubtful but you’ve given me a heads start 😀

  9. Posts: 29
    Daniel says:

    Awesome tutorials! It would be great to see some tutorials for python scripting in the Blender Game Engine.

  10. Posts: 29
    Daniel says:

    I am working on the tutorial but I am running into a problem with getting an object to appear in a custom menu. This is part of the code I am using:

    class addObjectOp(bpy.types.Operator):
    bl_idname = “mesh.primitive_Object_add”
    bl_label = “Add Object”
    bl_options = {‘REGISTER’, ‘UNDO’}

    def draw(self, context):
    layout = self.layout
    layout.operator(“mesh.primitive_Object_add”, text= “Add Object”);

    def menu_func(self, context):
    self.layout.menu(“INFO_MT_mesh_custom_menu_add”, icon=”PLUGIN”)

    When I run the code I can find and open the custom menu. However, in the menu instead of “Add Object” showing up I get “mesh.primitive_Object_add” and it is unselectable. I have a few other objects set up this way and they all have the same problem. Can you help me debug this?


    • Posts: 29
      Daniel says:

      When I ran Blender in Terminal and tried to select the object in the menu it said that the mesh.primitive_Object_add was an unknown operator. It also says I have an invalid bl_idname.

  11. Posts: 1

    Hello, great tutorial! To learn it! = D
    Only a doubt! My script exports perfectly! But when you add the object, as is done at the end of the video, with some work and others do not. Why!? Appears very different, not skewed, but snood.
    What can be?!

  12. Posts: 1
    camilo says:

    Excellent tutorial, thank you. This helped me create a script to export to a format that my OpenGL program eats up.

  13. Posts: 2
    Trentin Frederick says:

    How would I write this so that the generated tuple had a indent after each vertex or face? or is there a fast way to do this?

  14. Posts: 5
    visnik says:

    I don’t mean to treat this as a forum, but I am going through your tutorial and I am running into the following Error:

    ‘Mesh’ object has no attribute ‘faces’

    for the following line of code:
    meshFaces = bpy.context.active_object.data.faces

    So I looked in the python console under “bpy.context.active_object.data.” and did a auto-complete to see my options and faces is not in the list. Has it been replaced with something else or moved to another package?

    I am trying this in blender 263


    • Posts: 5
      visnik says:

      OK, looks like it has been changed from “faces” to “polygons”, there was nothing in the API change-log that I found stating this, but seems to work.

  15. Posts: 2

    hi hi… I have a lot of warnings… I think because this is for a previous api version… some help to run this in 2.7 blender and actual api???


    • Posts: 632
      richard w says:

      bpy.context.active_object.data.faces no longer exists within the API.
      If you replace all references to data.faces with data.polygons, then the script will work.

Leave a Comment

You must be logged in to post a comment.