0

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.

ttsesm
  • 409
  • 3
  • 10
  • I've tried 'EXEC_DEFAULT' but didn't work, also when I run the script is not in background mode, the UI opens with the .blend file since I am calling 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:02
  • I guess that it might be related to this answer from you @batFINGER https://blender.stackexchange.com/a/144555 but how I can do the same here with livicalc() as you did there for render()? – ttsesm Mar 16 '21 at 14:28
  • Oops re-read q and not bg mode. Worth a try. window_manager.windows will 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:50
  • Is it possible somehow, since the window is open, to execute the two commands in the interactive console (as I do now manually and works). Because my node tree is set and everything is fixed and I just need to run these two commands in the interactive console prompt. I've tried to play with the bpy.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:17
  • It's been my experience that looping on open mainfile is difficult. Try a search on the operator, many have tried. IIRC some have found ways. In the end my suggestion would be write a script to do do its thing on one blend file. Then another to loop the command line to open a file (or as many as your OS can handle) process close, until all blend files are processed. – batFINGER Mar 16 '21 at 17:47
  • Ok, lets forget about the loop over mainfiles. What if I just open one .blend file and want to execute a command in the interactive console like print('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
  • Have own version of run in console based on a pink_vertex script. (redirects stdout, stderr) There is also one by codemanx that does similar. (Both of which can be run from text editor without the need to install as addon) Don't think that this is necessary here. As mentioned have a squiz at addon code that livicalc. btw did you try putting "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:43
  • Can you add an example how to do that (related also to the my_script.py file that I've added)? The script opens any .blend file and supposedly should execute the print() 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

0 Answers0