HOW TO avoid crash on render time

-------- Edit --------
To avoid crashes on render time it’s better not to use modal timer to manage frame change. A handler is better such as frame_change_post. But the script will work properly in this case only if it doesn’t call any context.

This was the problem(s) on my script. I was toggling between edit and object mode. Bad idea…
Now it’s ok, thanks to Atom. No more context in the main calculation, and I use frame_change_post handler.

Hi everyone,

I’m new to blender, new to python, and new to blender artist… so I hope I won’t do too many wrong things.

I attached a blend file which is a simplification of a complex script I wrote and which does the job. The original script calculates the vertices of an infinite plane that are in the field of the camera. As I’m not comfortable with handlers, I used a timer that checks if camera world matrix has changed. If so, my script re-calculates the vertices that must be displayed. It works fine on playback. But when rendering the animation, blender crashes, sometimes after one frame render, sometimes after 10. I introduced a sleep time using a frame_change_post handler. It helped but not as I hoped. I still experience crashes after 3 to 50 frame renders.
I suppose it is a wrong idea to use a timer. Maybe the script is changing data when blender is trying to render. In fact I don’t know how blender works. And I don’t know how to make the render wait for the end of the scene update before starting. I would like to use handlers

So, as a simplification of my original script, I posted a blend file (hope it works). There is a plane that follows an empty (easier than a camera field). In the tool bar (T key) there is a refresh button that calculates the plane coordinates according to the empty ones (I didn’t use any timer here). So if you change the frame in the timeline, then press the refresh button, it works.
Now, I want to call my “exec_calc” function from an handler. For that, I (you) uncomment the last line of the script and add ‘scn’ as argument in “exec_calc” function (otherwise the handler doesn’t call my function properly).

The result is strange :
When I change the frame in the timeline, my plane disappears as if only half of my script worked, but I have no error in my console. And if I hit the render button, my plane is rendered as it should and I have the following message in the console :
Traceback (most recent call last):
File “/home/olivier/Blender/Tests/plane_test.blend/plane_test”, line 33, in exec_calc
File “/home/olivier/Blender/Tests/plane_test.blend/plane_test”, line 14, in empty_plane
File “/usr/share/blender/2.69/scripts/modules/bpy/ops.py”, line 188, in call
ret = op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.object.mode_set.poll() failed, context is incorrect

So there is a context problem. When I use the refresh button (or a timer) to call my “exec_calc” function, I don’t have any context problem. When I use a handler, I have a context problem.
Why and how to solve this problem ? And if somebody could help me to understand how to avoid crash problems on render time, it would be fine. For me, blender python API is not helpful, it’s too difficult to understand.

Thanks in advance.

Attachments

plane_test.blend (496 KB)

The context is equal to None when rendering.

Change your code. Don’t use the context or bpy.ops if you plan on animating and rendering that animation via code.

Handlers are a breeze to use and offer a valid scene when rendering.

Click on the link in my signature to view some of AddOns that demonstrate how to use handlers. Choose the ones for 2.66 or better.

Thank you very much Atom for your answer. Not sure I understand perfectly what you mean, but I have the feeling that your AddOns will be a great documentation (and more) for me. If I solve my problem, I’ll let you know.

Really really, thanks again.

I had a look to your change frame addon. Thank you for your job. I must confess that I mostly don’t understand the code. I’m not a developper, this is far too complex for me. But what I understood, is that when I put some code in the frameRefesh function, it worked.

Now I think I need to copy your code in my blend file and create the empty with driver. But there is a problem, when I attempt to run your script or install as an addon, I have this message :
Traceback (most recent call last):
File “/home/olivier/Blender/Tests/python_test_v2.blend/Text”, line 50, in <module>
ImportError: No module named ‘add_utils’
Error: Python script fail, look in the console for now…

Copy/paste the code is not a problem, but the empty, driver and animation… I suppose they’re mandatory.

Hi again Atom,

I’m trying to use your change frame add-on (or at least your code) to render my scripted animation without crash. As my script is rather big and not well commented, I made a simpler one.
Attached the blend file of this script. I hope you’ll have 5 minutes to have a look. There is an sliding empty and a function that calculates vertices of a plane that should follow the empty. There is a button “refresh” at the bottom of the left tools panel, just to show that the calculating function works. Unfortunately, I am not able to make my script work with yours. Playback or rendering animation, it’s the same. And after rendering animation, there is an alert in the driver panel : ERROR, Invalid Python expression. I am not an expert but again I have the feeling that it is a context problem… even if I have no message telling that.

Would you have an idea of the problem and how to solve it ?
Thank you very much for your help… and if no help, thank you for your amazing work :-).

Attachments

plane_test_2.blend (122 KB)

Ok, I found the problem. It’s the context. So I looked to your beeble add-on and did the same. Instead of toggling between edit and object modes, I created a new mesh assigned it to the object and removed the old mesh. And now it works… well on my simple script. Now it’s time for me to try this on the bigger one ;-).

I spoke alone, but you were of great help, because of your add-ons. It’s very difficult to find documentation on blender python API, I mean documentation I’m able to understand.
If I were not ashamed of my messy inelegant script, I would share it. I think it maybe useful. It creates a possibly infinite plane on which you can apply modifiers (ocean, particles, dupliverts, etc…) but creates only the vertices in the field of the camera. Just move the camera, and your terrain/background will be here with just the needed vertices. Unfortunately, the script is ugly and cannot be published.

Anyway, thnaks again and bravo.