4
$\begingroup$

I have to allow users of my addon to select a path via the directory browser window of blender.

Instead of using this

enter image description here

I used Operator in panel to get the folder path from user as follows

enter image description here

I tried lot of ways and I referred this and still trying....

Here is my code, for operator to browser the folder and set it.

class OBJECT_OT_find(bpy.types.Operator):
bl_idname = "object.find"
bl_label = "Select Path "

directory = StringProperty(maxlen=1024, subtype='FILE_PATH', options={'HIDDEN', 'SKIP_SAVE'})
filter_image = BoolProperty(default=True, options={'HIDDEN', 'SKIP_SAVE'})
filter_folder = BoolProperty(default=True, options={'HIDDEN', 'SKIP_SAVE'})
filter_glob = StringProperty(default="", options={'HIDDEN', 'SKIP_SAVE'})

def execute(self, context):
    # what to write
    return {'FINISHED'}

def draw(self, context):
    self.layout.operator('file.select_all_toggle')        

def invoke(self, context, event):
    wm = context.window_manager
    wm.fileselect_add(self)
    return {'RUNNING_MODAL'}

def register():
 bpy.utils.register_class(OBJECT_OT_find)
 bpy.utils.register_module(__name__)
 bpy.types.Scene.FPath = StringProperty(name="Set Source to Find",
  attr="custompath",
  description="searching Images",
  maxlen= 1024,
  subtype='DIR_PATH',
  default= "")
def unregister():
 bpy.utils.register_class(OBJECT_OT_find)
 bpy.utils.unregister_module(__name__)
 del bpy.types.Scene.FPath

How can I get that path? I want to store and read it.

$\endgroup$
  • $\begingroup$ AFAIK there's no operator to directly do that. Why not your first solution, a DIR_PATH-typed StringProperty? $\endgroup$ – Adhi Nov 29 '13 at 11:32
  • $\begingroup$ But the operator works fine. It shows window and I could select folders to set but how to get that path? DIR_PATH is to select only folders from all files $\endgroup$ – Rasa Mohamed Nov 29 '13 at 11:42
5
$\begingroup$

As far as I know, it's bad practice to create the custom property for the scene in the register function. You can do this in the execute function. Also you shouldn't create it on bpy.types.Scene but on the current scene itself. For the execute function you can use this code:

def execute(self, context):
    d = self.properties.directory
    bpy.context.scene['image_filepath'] = d   #store the path for later use
    [...]
    return {'FINISHED'}

You only need the bpy.context.scene['image_filepath'] = d if you want to store the path for later use.

You can simplify your register and unregister functions:

def register():
    bpy.utils.register_module(__name__)

def unregister():
    bpy.utils.unregister_module(__name__)

You can now run your operator and choose a folder to do something with. If you later want to access the path to the folder you can use bpy.context.scene['image_filepath'].

$\endgroup$
  • $\begingroup$ d = self.properties.directory using d itself I got the path value then why this line bpy.context.scene['image_filepath'] = d... $\endgroup$ – Rasa Mohamed Dec 2 '13 at 3:44
  • $\begingroup$ I thought you, wanted to store the path for later use. If you only need it in the operator, you can skip this. I've edited my answer to make this clear. $\endgroup$ – jasperge Dec 2 '13 at 12:08
  • $\begingroup$ How to refresh or set null path when Blender opens save file or new file. is there any way to assign NULL. if I use this, the path wont be null after reopen the blender any idea? $\endgroup$ – Rasa Mohamed Dec 3 '13 at 10:17
  • $\begingroup$ The file path is only saved for the active scene in the current file. If you open another file, the property won't be there. But if you reopen the saved file, it is. To me this seems like the proper behavior. Can you describe what it is you want to achieve exactly and how it should work in your opinion? $\endgroup$ – jasperge Dec 3 '13 at 11:29
  • $\begingroup$ Im searching files in the path that what the variable 'fm' have. using fm = bpy.context.scene['image_filepath'] = d. So if I chosen a path in a blender file that would remains in Newly opened Blender file or open saved blender file. I need when I open new or saved file the path want to be null. How? $\endgroup$ – Rasa Mohamed Dec 3 '13 at 12:06

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.