3
$\begingroup$

I'm trying to create a folder using python in blender and run in in blender text editor. I don't know how I can do this.

I want to export the vertices of a mesh in this specific created folder. Currently I'm doing this which create the file in the blender folder.

fout = open("mesh.verts", "w")

$\endgroup$
6
$\begingroup$

You have following options. I use the file name myfile.xxx as placeholder for whatever file shall be opened.

Open within the folder where blender is installed:

    fout = open("myfile.xxx") 

However you might run into permission issues when the installation folder is write protected.

Open in the user Application folder (Appdata)

    import bpy
    import os
    userdir = bpy.utils.resource_path('USER') # see note
    mypath  = os.path.join(userdir,"myfile.xxx")

    fout    = open(mypath)

Note: Please check also bpy.utils.user_resource(...)

Open in the same folder where the current blend file is located

    import bpy
    import os
    blenddir = py.path.abspath('//')
    mypath   = os.path.join(userdir, "myfile.xxx")

    fout     = open(mypath)

Note: If the current session has not yet been saved to a blend file, then blenddir is returned as empty string! You might need to test for this in your script.

Open in addon folder

    import bpy
    import os
    import myaddon
    myhome = os.path.dirname(myaddon.__file__)
    mypath = os.path.join(myhome, "myfile.xxx")

    fout     = open(mypath)

Hint: When you have installed the Addon by using the Addon Tab in User Preferences, then myhome points to Blender's scripts/addons/myaddon folder in the Appdata directory.

Open in Preset folder (where to store addon presets)

    import bpy
    import os
    import myaddon
    presets   = bpy.utils.user_resource('SCRIPTS', "presets")
    mypresets = os.path.join(presets, myaddon.__name__)
    if not os.path.exists(mypresets):
        os.makedirs(mypresets)    
    mypath = os.path.join(mypresets, "myfile.xxx")

    fout     = open(mypath)
$\endgroup$
1
$\begingroup$

I figured it out using:

os.mkdir('')
$\endgroup$
  • 1
    $\begingroup$ Blender includes some path utilities that you may want to use to easily get a path that is relative to the current blend file. $\endgroup$ – sambler Sep 27 '14 at 9:55

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.