0
$\begingroup$

I would like to use this method, but I totally stucked.

A.py

myPath=r"C:\tem\B.py"

text = bpy.data.texts.load(myPath) 

ctx = bpy.context.copy()
ctx['edit_text'] = text
bpy.ops.text.run_script(ctx)

B.py

import bpy

bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_mode(type="FACE")

When I call A.py, bpy.ops.mesh.select_mode(type="FACE") say,
"RuntimeError: Operator bpy.ops.mesh.select_mode.poll() failed, context is incorrect"

When I call B.py from text editor directly, it is fine. So, I'm really confusing.

---Edit and add explanation--------------------

When the object is "object mode", I run B.py directly and it run correct. When the same situation, I run A.py to run B.py, B.py say, "bpy.ops.mesh.select_mode.poll() failed".

B.py has bpy.ops.object.mode_set(mode='EDIT') difinitely, and changed the select mode correctly. But the "mesh.select_mode" is failed, if B.py is called from A.py.

$\endgroup$
3
  • $\begingroup$ Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. $\endgroup$
    – TheLabCat
    Sep 25, 2022 at 2:58
  • $\begingroup$ When the object is "object mode", I run B.py directly and it run correct. When the same situation, I run A.py to run B.py, B.py say, "bpy.ops.mesh.select_mode.poll() failed". B.py has bpy.ops.object.mode_set(mode='EDIT') difinitely, and changed the select mode correctly. But the "mesh.select_mode" is failed, if B.py is called from A.py. $\endgroup$
    – Mrfas
    Sep 25, 2022 at 9:21
  • 1
    $\begingroup$ you can read more about context here blender.stackexchange.com/questions/6101/… $\endgroup$ May 4 at 7:22

1 Answer 1

1
$\begingroup$

The answer to the linked question is from 2015 and the method to overwrite a context just changed with Blender 3.3.

In Blender 3.3, you have to change the last three lines of A.py to

import bpy

myPath=r"C:\tem\B.py"

text = bpy.data.texts.load(myPath) 

with bpy.context.temp_override(edit_text=text):
    bpy.ops.text.run_script()

$\endgroup$
1
  • $\begingroup$ I update my blender 3.1->3.3 and your script and B.py goes fine. Thank you! (I don't know actually, but I feel 3.1 had a bit weird action) $\endgroup$
    – Mrfas
    Sep 25, 2022 at 13:20

You must log in to answer this question.

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