利用者:Ansimionescu/Notes/1

提供: wiki
移動先: 案内検索

Architecture: operators

Operators (wmOperatorType*) manage tools, so by defining a new wmOperatorType you create a new tool. Check out [1] for more info (and conventions) and [2] for a tutorial/more details.

Members

ot->name = "(De)select All"

name: Human readable name for the UI. Capitalized.

ot->idname = "MESH_OT_select_all"

idname: unique identifier.

Set it identical to the method name

Format: <PREFIX>_OT_<action>_<details> (e.g. MESH_OT_select_all)

ot->description = "(De)select all vertices, edges or faces";

description: Tooltip?

PropertyRNA *prop = RNA_def_int(ot->srna, ...)

srna: RNA propery defining the way the operator behaves

ot->exec = edbm_select_all_exec

exec, invoke, modal, cancel: callbacks

Every operator must define either exec or invoke.

  • exec
    • Provided to run the operator without user interaction.
    • Only if this is provided can the ot be recorded for a macro.
  • invoke
    • Will execute after an event (e.g. getting mouse coordinates).
  • modal
    • From the invoke callback, the ot can add itself as a handler, and receive further events in the modal callback.
  • cancel
    • If the operator is cancelled (e.g. program quitting), this callback is called.

Return for exec, invoke and cancel:

  • pass through: Means the operator passes on the event to other operators, as if the callback did not run.
  • running modal: Indicates that the ot is still running and has registered itself to receive modal callbacks.
  • cancelled, finished: Mean the execution of the ot has ended, successfully or not. The operator will be registered only for 'finished'.
ot->poll= ED_operator_editmesh
[...]
int ED_operator_editmesh(bContext *C)
{
    Object *obedit= CTX_data_edit_object(C);
    if(obedit && obedit->type==OB_MESH)
        return NULL != ((Mesh *)obedit->data)->edit_mesh;
    return 0;
}

poll: Used to verify the ot can be executed in the current context. It's often shared for multiple operators. It's always called before executing the operator, so it may be used to gray out or hide UI elements that use this ot automatically.

Caveats

  • Note the distinction between RNA properties and customdata. The properties should only store public parameters as seen by the user, these are saved as part of macro's and written to file. The customdata is an arbitrary pointer, typically to a C struct, that should only exist while the operator is running, an so it is not written to file.
  • The invoke callback also takes properties as inputs and should take those into account. For example a transform operator might have a mode=rotate input, and should then start in rotation mode.


Home:

[0] http://wiki.blender.org/index.php/User:Ansimionescu

Reference:

[1] http://wiki.blender.org/index.php/Dev:2.5/Source/Architecture/Operators

[2] http://wiki.blender.org/index.php/Dev:2.5/Source/Architecture/Operators/Tutorial