Hi, i need to open a file in my script but i don´t know how can i show filedialog i blender 2.5
In a operator class, you would add:
def invoke(self, context, event):
wm = context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
i am trying to do it, but when i run the code i can´t see the file selector
import bpy
class OpenFile(bpy.types.Operator):
def invoke(self, context, event):
wm = context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'}
Sorry, I assumed you already had code that you were just trying to add to. Look at the operator_export.py template.(you’ll have to have a object selected to run it)
what i am trying to do is adapt a simple script i made for blender 2.49 but the first problem i have is that i don´t know how to open the filedialog to open a file:
this is my old script
#!BPY
"""
Name: 'Import CFF'
Blender: 2.48
Group: 'Import'
Tip: 'Import CFF Files'
"""
__author__ = "Jor1980"
__url__ = ("", "")
__version__ = "1.0"
__bpydoc__ = ""
# ***** BEGIN GPL LICENSE BLOCK *****
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
import Blender
from Blender import Window
from Blender import sys as bsys
from Blender import *
import bpy
REG_KEY = 'cff_reg'
EXPORT_DIR = ''
IMPORT_DIR = ''
def update_RegistryInfo():
d = {}
d['EXPORT_DIR'] = EXPORT_DIR
d['IMPORT_DIR'] = IMPORT_DIR
Blender.Registry.SetKey(REG_KEY, d, True)
rd = Blender.Registry.GetKey(REG_KEY, True)
if rd:
try:
EXPORT_DIR = rd['EXPORT_DIR']
IMPORT_DIR = rd['IMPORT_DIR']
except KeyError: update_RegistryInfo()
else:
update_RegistryInfo()
global import_dir,tipo
def load_data(file):
global import_dir,tipo
file.next()
CffType=file.next().replace('Type=','')
Scene_Name=CffType
sc=Scene.New(Scene_Name)
sc.makeCurrent()
#sc = Scene.GetCurrent()
screen_list = Window.GetScreens()
for screens in screen_list:
Window.SetScreen(screens)
sc.makeCurrent()
Window.SetScreen(screen_list[0])
numeroparts=int(file.next().replace('Number of blocks=',''))
print numeroparts
numerovertxpart=[]
numerofacexpart=[]
file.next()
for i in range(0,numeroparts):
line=file.next()
print line.split()[0]
print line.split()[1]
numerovertxpart.append(int(line.split()[0]))
print numerovertxpart[i]
numerofacexpart.append(int(line.split()[1]))
print numerofacexpart[i]
malla=[]
ob=[]
for i in range(0,numeroparts):
malla.append(bpy.data.meshes.new("2K_Part"))
uv = []
for j in range(0,numerovertxpart[i]):
line=file.next()
x,y,z,u,v=float(line.split()[0].replace(',','.')),float(line.split()[1].replace(',','.')),float(line.split()[2].replace(',','.')),float(line.split()[3].replace(',','.')),float(line.split()[4].replace(',','.'))
uv.append((u,v))
print x,y,z
malla[i].verts.extend(float(x),float(y),float(z))
aa = []
bb = []
cc = []
for j in range(0,numerofacexpart[i]):
line=file.next()
a,b,c=int(line.split()[0]),int(line.split()[1]),int(line.split()[2])
aa.append(a)
bb.append(b)
cc.append(c)
print a,b,c
bface = Blender.NMesh.Face()
bface.v.extend((a,b,c))
malla[i].faces.extend(bface)
malla[i].faceUV = True
for q in range(len(malla[i].faces)):
malla[i].faces[q].mode |= Blender.Mesh.FaceModes['TWOSIDE']
malla[i].faces[q].transp = Blender.NMesh.FaceTranspModes['ALPHA']
malla[i].faces[q].uv=Mathutils.Vector(uv[aa[q]]), Mathutils.Vector(uv[bb[q]]), Mathutils.Vector(uv[cc[q]])
scn = bpy.data.scenes.active
ob.append(scn.objects.new(malla[i], CffType))
ob[i].modifiers.append(Modifier.Types.SUBSURF)
ob[i].modifiers.append(Modifier.Types.SUBSURF)
def fs_callback(filename):
global IMPORT_DIR, EXPORT_DIR, import_dir, weather
weather = ''
if not filename.endswith('.cff'): filename = '%s.cff' % filename
Blender.Window.WaitCursor(1)
starttime = bsys.time()
import_dir = bsys.dirname(filename)
if import_dir != IMPORT_DIR:
IMPORT_DIR = import_dir
update_RegistryInfo()
file = open(filename,"r")
header = file.readline()
if header != '//Ciber Face Converter File (c)2009 by Jor1980
':
Blender.Draw.PupMenu('This is not a *.cff file')
print header
file.close()
return
if not load_data(file):
Blender.Draw.PupMenu('Move the cursor please')
file.close()
return
file.close()
endtime = bsys.time() - starttime
print "Data imported in %.3f seconds." % endtime
Blender.Window.WaitCursor(0)
return
objs = Blender.Object.Get()
fname = "*.CFF"
if IMPORT_DIR:
fname = bsys.join(IMPORT_DIR, bsys.basename(fname))
Window.FileSelector(fs_callback,"Import *.cff",fname)
This is the template I was referring to:
import bpy
def write_some_data(context, filepath, use_some_setting):
print("running write_some_data...")
f = open(filepath, 'w')
f.write("Hello World %s" % use_some_setting)
f.close()
return {'FINISHED'}
# ExportHelper is a helper class, defines filename and
# invoke() function which calls the file selector.
from bpy_extras.io_utils import ExportHelper
from bpy.props import StringProperty, BoolProperty, EnumProperty
class ExportSomeData(bpy.types.Operator, ExportHelper):
'''This appears in the tooltip of the operator and in the generated docs.'''
bl_idname = "export.some_data" # this is important since its how bpy.ops.export.some_data is constructed
bl_label = "Export Some Data"
# ExportHelper mixin class uses this
filename_ext = ".txt"
filter_glob = StringProperty(default="*.txt", options={'HIDDEN'})
# List of operator properties, the attributes will be assigned
# to the class instance from the operator settings before calling.
use_setting = BoolProperty(name="Example Boolean", description="Example Tooltip", default=True)
type = EnumProperty(items=(('OPT_A', "First Option", "Description one"),
('OPT_B', "Second Option", "Description two."),
),
name="Example Enum",
description="Choose between two items",
default='OPT_A')
@classmethod
def poll(cls, context):
return context.active_object != None
def execute(self, context):
return write_some_data(context, self.filepath, self.use_setting)
# Only needed if you want to add into a dynamic menu
def menu_func_export(self, context):
self.layout.operator(ExportSomeData.bl_idname, text="Text Export Operator")
def register():
bpy.utils.register_class(ExportSomeData)
bpy.types.INFO_MT_file_export.append(menu_func_export)
def unregister():
bpy.utils.unregister_class(ExportSomeData)
bpy.types.INFO_MT_file_export.remove(menu_func_export)
if __name__ == "__main__":
register()
# test call
bpy.ops.export.some_data('INVOKE_DEFAULT')
Hi,
how to show a folder selection dialog box to create a directory in a user specified directory using blender addon… ?
Please give me any running example.
Thank you…