I'm using Blender 3.0 and I would like to dynamically change an operator's tooltip based on conditions that happen inside the operator's poll() function but I can't get it to work. I have tried to follow the example from the 2.81 Release Notes.
Why I want to do this: my understanding of the poll function is that is is meant to be used to disable the operator's button if certain conditions aren't met. If there are multiple potential conditions that could fail, I would like a way to inform the user what specifically isn't met so that they can correct the problem. The sample code below shows only some of the sanity checks I would like to perform. Yes, I could move this into the execute() and then skip execution if the conditions aren't met, but then what's the point of the poll() function?
class MYCOMPANY_OT_ExportSingleObject(bpy.types.Operator):
bl_label = "Export"
bl_idname = "mycompany.exportsingleobject"
myTooltip: bpy.props.StringProperty()
@classmethod
def description(cls, context, properties):
## myTooltip is always empty here
return "Tooltip is [ " + properties.myTooltip + " ]"
@classmethod
def poll(cls, context):
cls.myTooltip = "Tooltip: Set from poll()"
## This prints the correct ID and the updated tooltip
print("[ " + cls.bl_idname + " ] has tooltip [ " + cls.myTooltip +" ]")
## As an aside, cls.report() doesn't seem to do anything. I left it in in case someone also knows if/how to make it work in poll()
if context.active_object is None:
cls.report( {"WARNING"}, "No object selected.")
return False
if context.active_object.type != "MESH":
cls.report( {"WARNING"}, "Can only export objects of type MESH. [ "+context.active_object.name+" ] is of type [ "+context.active_object.type+" ]")
return False
is_enabled, is_loaded = addon_utils.check("io_scene_fbx")
if not (is_enabled and is_loaded):
cls.report(
{"WARNING"},
"Add-on for FBX format is not enabled." +
"Go to 'Edit → Preferences → Add-ons' and enable 'Import-Export: FBX format'")
return False
return True
def execute(self, context):
## Omitted
cls.poll_message_set("reason")
calls before you return False in yourpoll
function. Example $\endgroup$