I am trying to run a python script from command line as ./blender -P my_script.py, inside the script I have the following loop:
for item in blend_files:
_cleanup()
bpy.ops.wm.open_mainfile(filepath=item)
# commands to be run
override = {'node': bpy.data.node_groups['rad_sim'].nodes['LiVi Simulation']}
bpy.ops.node.livicalc(override, 'INVOKE_DEFAULT')
but this gives me the following error:
.../modules/bpy/ops.py", line 130, in __call__
ret = _op_call(self.idname_py(), C_dict, kw, C_exec, C_undo)
RuntimeError: Operator bpy.ops.node.livicalc.poll() Missing 'window' in context
however if I run the two commands in the interactive console within the blender it works without issues. What is the problem, I cannot understand.
An alternative would be to execute the commands internally in the interactive console since the environmental variables (e.g. node tree, objects, etc) are set and if I do it manually it seems to do the job. I've tried to pass a simple command print('Hello world') to be executed but I am also getting an error:
for item in blend_files:
bpy.ops.wm.open_mainfile(filepath=item)
command = "print('hello world!!')" # here I would pass the commands override = {'node': bpy.data.node_groups['rad_sim'].nodes['LiVi Simulation']} and bpy.ops.node.livicalc(override, 'INVOKE_DEFAULT')
bpy.ops.console.clear_line()
bpy.ops.console.insert(text=command)
bpy.ops.console.execute()
but it gives me an error kind of similar as the above one:
.../2.91/scripts/modules/bpy/ops.py", line 132, in __call__
ret = _op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.console.insert.poll() failed, context is incorrect
my_script.py:
import os
import bpy
import sys
def main():
filename = "path/to/.blend/file"
bpy.ops.wm.open_mainfile(filepath=filename)
command = "print('hello world!!')"
bpy.ops.console.clear_line()
bpy.ops.console.insert(text=command)
bpy.ops.console.execute()
if name == 'main':
print('Start!!!!')
main()
print('End!!!!')
os._exit(0)
Call then ./blender -P my_script.py and in the opened blender window in the interactive console I should get the command prin('hello world!!') executed.
bpy.ops.wm.open_mainfile(). So full UI is accessible and if insert the two command manually in the interactive console are successfully executed. – ttsesm Mar 16 '21 at 14:02livicalc()as you did there forrender()? – ttsesm Mar 16 '21 at 14:28window_manager.windowswill give you a window. Be tempted to look at the source code of livicalc and may be able to to it from module without calling operator. – batFINGER Mar 16 '21 at 14:50bpy.ops.console.execute()operator looking on the example here https://sourcecodequery.com/example-method/bpy.ops.console.execute and then send these two commands but I am doing something wrong I guess. – ttsesm Mar 16 '21 at 15:17print('hello!!!')? How to do that? Could this scrijpt https://github.com/K-410/blender-scripts/blob/master/2.8/run_in_console.py be helpful? I also found a discussion related to that https://blenderartists.org/t/insert-and-execute-code-in-console-by-script/1165543 – ttsesm Mar 16 '21 at 18:02"window" : context.window_manager.windows[-1]into your override dictionary.A simple print test shows it is available on each iteration – batFINGER Mar 16 '21 at 18:43print()command but it doesn't. How to run this"window": context.window_manager.windows[-1]? Should this be in the script? – ttsesm Mar 16 '21 at 20:08