-3

How can I properly set up context in plugins?

I am importing Digital Terrain Models using a python plugin.

def register():
    bpy.utils.register_class(DTM)
    bpy.types.INFO_MT_mesh_add.append(add_to_menu)

I get the following error.

C_dict, C_exec, C_undo = BPyOpsSubModOp._parse_args(args) File "C:\Program Files\Blender Foundation\Blender\2.67\scripts\modules\bpy\ops.py", line 142, in _parse_args raise ValueError("1-3 args execution context is supported") ValueError: 1-3 args execution context is supported

How can I fix this?

David
  • 49,291
  • 38
  • 159
  • 317
ogeid
  • 893
  • 2
  • 8
  • 14
  • 1
    Which addon is that? If it isn't the "HiRISE DTM" one, please give a link to the full source code. – Adhi Jun 11 '13 at 02:43
  • 1
    This looks more like a bug report then a question. – ideasman42 Jun 11 '13 at 02:59
  • Maybe it's something in add_to_menu, trying to call an operator with incorrect arguments. – Adhi Jun 11 '13 at 03:16
  • try:
    dtm = bpy.ops.import_img.load(self,context,filepath=self.filepath,scale=self.scale,bin_mode=self.bin_mode, cropVars=False,)
    except ValueError:
    print("location: -1")
    – ogeid Jun 11 '13 at 03:16
  • You don't need self and context when calling an operator. Those first two argument positions are reserved for execution context. – Adhi Jun 11 '13 at 03:18
  • bpy.ops has been updated since last try.
    it seems to be an incorrect file location.

    using:
    filepath = bpy.path.ensure_ext(filepath, filename_ext)
    bpy.ops throws:
    location: -1
    – ogeid Jun 11 '13 at 03:19
  • @AshleyDuhn stop using the html break tag, they get converted to readable characters. Stackexchange uses markdown http://stackoverflow.com/editing-help to help markup your text, read that link -- it's great. – zeffii Jun 11 '13 at 05:54
  • the functions require the argument in a variable=value syntax like: bpy.ops.mesh.decimate(ratio=0.18) if I try bpy.ops.mesh.decimate(0.18) I get that 1-3 params error too. – Aquarius Power Dec 03 '21 at 03:28

1 Answers1

6

When calling an operator with self and context, I get the exact same error message:

ValueError: 1-3 args execution context is supported

The first 3 arguments are reserved for optional arguments. From Blender API documentation on running an operator:

For calling operators keywords are used for operator properties and positional arguments are used to define how the operator is called.

There are 3 optional positional arguments (documented in detail below).

bpy.ops.test.operator(override_context, execution_context, undo)
  • override_context
  • dict type execution_context - string (enum)
  • undo - boolean

Each of these arguments is optional, but must be given in the order above.

In short, calling an operator with self and context as the first two arguments is wrong, and maybe the cause of the error message you've shown.

Adhi
  • 14,310
  • 1
  • 55
  • 62
  • you're right. so using bpy.ops.import_img.load(self, with proper context would return MESH in the best of cases – ogeid Jun 11 '13 at 03:39
  • I got this error when I had the syntax: bpy.ops.object.join(obj2) and resolved it by removing obj2, so just: bpy.ops.object.join() I thought the second object to be joined to the first but it turns out the join operator uses selected objects and an active object... it does not expect objects to be passed as arguments. Makes sense once you know. (Just posting this example in case it helps someone with a similar problem.) – Mentalist Nov 02 '20 at 07:25
  • However... the join operator can accept a copy of the scene context! ctx = bpy.context.copy() then join(ctx) assuming the selected objects and active object in the context are valid. I don't know the details of why this works, but here's the answer that demonstrates it. The manual currently doesn't explicitly mention any ability of join() to accept arguments though. – Mentalist Nov 02 '20 at 08:24
  • I found that the functions require the argument in a variable=value syntax like: bpy.ops.mesh.decimate(ratio=0.18) if I try bpy.ops.mesh.decimate(0.18) I get that 1-3 params error too, I dont know if this is like that only on newer blender versions, mine is 2.93.4 – Aquarius Power Dec 03 '21 at 03:30