0

I've asked a very similar question yesterday, but I have a more concrete example here. I'm using the template "Operator Modal View3d Raycast" with some modifications:

class ViewOperatorRayCast(bpy.types.Operator):
bl_idname = "view3d.modal_operator_raycast"
bl_label = "RayCast View Operator"

def modal(self, context, event):

    if context.space_data.type != 'VIEW_3D': # This doesn't work           
        return {'PASS_THROUGH'} # This doesn't work

    if event.type == 'RIGHTMOUSE' and event.value == 'PRESS': # The operator only does any thing for RMB
        main(context, event) # This is where the raycasting goes on. See the template                           
        return {'RUNNING_MODAL'}

    elif event.type in {'ESC'}:
        return {'CANCELLED'}

    return {'PASS_THROUGH'} # I want the operator to keep going

def invoke(self, context, event):
    if context.space_data.type == 'VIEW_3D':
        context.window_manager.modal_handler_add(self)
        return {'RUNNING_MODAL'}
    else:
        self.report({'WARNING'}, "Active space must be a View3d")            
        return {'CANCELLED'}

def Execute(self, context)
    return {'RUNNING_MODAL'}

I can start the operator fine by pressing F3:

Invoking the modal operator

But I would like to start the operator from python. I have tried by adding the execute method, but this doesn't work since the context is either the text editor or console (not the view3d). I got the idea of using execute here (top link), but he just mentions the problem with context and not the resolution - if any:

https://www.youtube.com/watch?v=A8S-s7tuTdY (about 15:30) Constantly running modal operator (also have some of what I seek)

Is it possible to trick Blender to think that I called the operator from view3d or something like that?

DrDress
  • 563
  • 4
  • 19
  • You can right-click the yt video and select "Copy video URL at current time": https://youtu.be/A8S-s7tuTdY?t=904 – brockmann Dec 16 '20 at 08:48
  • https://docs.blender.org/api/current/bpy.ops.html?highlight=invoke_default#execution-context -> TLDR; By default the execute method will be called, see https://blender.stackexchange.com/a/73548/31447 (helped me a lot a few years ago). To answer your question you should add how do you want to call it... "I want to call it with python" not pretty useful TBH. – brockmann Dec 16 '20 at 08:54
  • Take out the conditional? See "overriding context" – batFINGER Dec 16 '20 at 09:47
  • @ brockmann. I missed the documentation you linked. Looks like what I need. Thanks. I want to call the operator from a script like this: bpy.ops.view3d.modal_operator_raycast() which calles the execute method, but from a wrong context (the text editor). – DrDress Dec 16 '20 at 10:40
  • See the second code snippet in the docs. Also I'd recommend add any relevant information to your q since others may not read the comments. – brockmann Dec 16 '20 at 10:45
  • Answered your question from yesterday. Of opinion this is a dupe of that, or may as well be. – batFINGER Dec 16 '20 at 10:49
  • @batFINGER it is basically the same, but I didn't see your an answer ???, so I though that I ought to make it a more concrete with the template raycast operator. I'll look at your answer. Thanks! – DrDress Dec 16 '20 at 13:44
  • In future recommend editing original question over asking pretty much the same question as a new question on the next day. – batFINGER Dec 16 '20 at 15:41

0 Answers0