1

Losing my Blender flower on this one :)

I'm using an operator to select the filepath of a .txt file.

import bpy

class SelectTxtFile(bpy.types.Operator): bl_idname = "file.select_txt" bl_label = "Select TXT File" bl_options = {"REGISTER", "UNDO"} filepath: bpy.props.StringProperty(subtype="FILE_PATH") filter_glob: bpy.props.StringProperty(default="*.txt", options={"HIDDEN"})

def execute(self, context):
    print(self.filepath)
    return {"FINISHED"}

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

def register(): bpy.utils.register_class(SelectTxtFile)

if name == "main": register()

bpy.ops.file.select_txt('INVOKE_DEFAULT')

enter image description here

I don't want the user to be able to validate the operator (Blue button) if the file they selected isn't a valid .txt file.

I want the button to be clickable only when :

I would like to insert this line somewhere :

layout.enabled = os.path.isfile(self.filepath) and os.path.splitext(self.filepath)[1] == ".txt"

I'd like it to be grayed out if the conditions are not met. But I can't access this particular layout button because it's apparently managed by the window manager.

How can I achieve that ?

Gorgious
  • 30,723
  • 2
  • 44
  • 101
  • Looks promising https://docs.blender.org/api/current/bpy.ops.file.html#bpy.ops.file.execute re the need_active parameter Maybe related https://blender.stackexchange.com/a/207665/15543 – batFINGER Jul 28 '21 at 17:02
  • @batFINGER That looks exactly like what I'm looking for and I learned a lot from your answer and related threads, but I still can't seem to access that particular operator to change this parameter... I'll keep looking :) I'm starting to wonder if I can just remove this bottom strip with the filename, operator and cancel button and redraw it myself. – Gorgious Jul 28 '21 at 19:24
  • dig into wm.fileselect_add which AFAIK invokes the filebrowser to run modally and appends the footer. Highly recommend pathlib over os.path shenanigans – batFINGER Jul 29 '21 at 11:11

0 Answers0