Creating an operator in the Blender Python API, its description is usually specified in its “bl_description” parameter. However, quite often the same operator performs different actions in different cases, and a constant description does not describe all its capabilities. In this case, a dynamic description can be given to an operator.
To assign a dynamic description to an operator, we can use the “description” class method.
1 2 |
@classmethod def description(cls, context, properties): |
Method parameters:
cls – self class pointer
context – pointer to the executing context
properties – with this property, we can access the input parameters of the operator
The return value of the method will be displayed as a description of the operator.
For example, to have one description for the operator when the mesh is in the Edit mode and another – for the Object mode, let’s define simple condition in the “description” method:
1 2 3 4 5 6 |
@classmethod def description(cls, context, properties): if context.mode == 'OBJECT': return 'DESCRIPTION FOR OBJECT MODE' elif context.mode == 'EDIT_MESH': return 'DESCRIPTION FOR EDIT MODE' |
If the operator has input parameters, for example, “param”, and depending on its value, the description of the operator also changes, let’s write the following condition in the “description” method:
1 2 3 4 |
@classmethod def description(cls, context, properties): if properties.param != '': return 'DESCRIPTION BY PARAMETER' |
The operator input parameters are accessed through the “properties”.
Full code for operator with dynamic description:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class TEST_OT_test_op(Operator): bl_idname = 'test.test_op' bl_label = 'Test' bl_description = 'Default Description' bl_options = {'REGISTER'} param: StringProperty( name='param', default='' ) @classmethod def description(cls, context, properties): if properties.param != '': return 'DESCRIPTION BY PARAMETER' elif context.mode == 'OBJECT': return 'DESCRIPTION FOR OBJECT MODE' elif context.mode == 'EDIT_MESH': return 'DESCRIPTION FOR EDIT MODE' def execute(self, context): return {'FINISHED'} |