7
$\begingroup$

I am trying to call bpy.ops.object.parent_clear in blender background mode (no GUI) and it fails with an "context is incorrect" message. I would like to read the source code of the operator to better understand what is going on. I downloaded the source code of the blender 2.74 distribution. The error message indicates where the error occurs:

File ".../Resources/2.74/scripts/modules/bpy/ops.py", line 189, in  __call__
ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.object.parent_clear.poll() failed, context is incorrect

However, at that location one can only find

ret = op_call(self.idname_py(), None, kw)

which is another call. Where do I find the actual implementation of parent_clear?

$\endgroup$
3
  • $\begingroup$ You are right. For parent_clear it seems to be ED_object_parent_clear, which is performing the operation in the end. But for duplicate_move_linked for example? $\endgroup$ Commented May 4, 2015 at 10:21
  • $\begingroup$ you can see that duplicate_move_linked is just two calls on for OBJECT_OT_duplicate and one for TRANSFORM_OT_translate see this page $\endgroup$
    – Chebhou
    Commented May 4, 2015 at 10:25
  • 1
    $\begingroup$ tips on figurating out what to do about the context is incorrect error: blender.stackexchange.com/questions/6101/… $\endgroup$
    – zeffii
    Commented May 4, 2015 at 10:49

2 Answers 2

8
$\begingroup$

The naming scheme is object.parent_clear -> OBJECT_OT_parent_clear.

To find the source code you will usually find the declaration of the operator in the headerfile, somewhere in source/blender/editors/*, in this case it is source/blender/editors/object/object_intern.h

The comment points to the source file source/blender/editors/object/object_relations.c

 541 void OBJECT_OT_parent_clear(wmOperatorType *ot)
 542 {
 543         /* identifiers */
 544         ot->name = "Clear Parent";
 545         ot->description = "Clear the object's parenting";
 546         ot->idname = "OBJECT_OT_parent_clear";
 547 
 548         /* api callbacks */
 549         ot->invoke = WM_menu_invoke;
 550         ot->exec = parent_clear_exec;
 551 
 552         ot->poll = ED_operator_object_active_editable;
 553 
 554         /* flags */
 555         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 556 
 557         ot->prop = RNA_def_enum(ot->srna, "type", prop_clear_parent_types, CLEAR_PARENT_ALL, "Type", "");
 558 }

The poll method checks for the right context. The exec method is located in the same file

 523 /* note, poll should check for editable scene */
 524 static int parent_clear_exec(bContext *C, wmOperator *op)
 525 {
 526         Main *bmain = CTX_data_main(C);
 527         const int type = RNA_enum_get(op->ptr, "type");
 528 
 529         CTX_DATA_BEGIN (C, Object *, ob, selected_editable_objects)
 530         {
 531                 ED_object_parent_clear(ob, type);
 532         }
 533         CTX_DATA_END;
 534 
 535         DAG_relations_tag_update(bmain);
 536         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);
 537         WM_event_add_notifier(C, NC_OBJECT | ND_PARENT, NULL);
 538         return OPERATOR_FINISHED;
 539 }

I would recommend an IDE like QTCreator to follow the symbols though you might use grep/search the files manually.

$\endgroup$
1
$\begingroup$

you can search in the source code available online on this page , also one of blender developers Campbell Barton has great videoes on searching and navigating the source code : Tips on Searching/Navigating Blenders Code and Learning to Navigate Source & Simple Fix

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .