Crash when calling UNDO from a python script #28724

Closed
opened 2011-09-23 01:03:37 +02:00 by ji hugen · 7 comments

%%%Blender uses to crash when I export some objects/meshes with an import/export python addon I've written.

Plugin structure:
I'm roughly using the following structure when exporting:

  • turn Undo support off
  • make single user, apply all tranformations, modifers, etc. to all visible objects
  • export the data
  • turn Undo support back on
  • call Undo once to revert all data to the original state

To sum up, the goal is to export the meshes' faces and vertices once they are tranformed by the modifers, scale factor, etc. (I don't know a better way to proceed than applying all transformations before exporting the data...)

Issue:
Blender uses to crash when I export some scenes with this plugin. It does not crash systematically, for example with the scene I'm supplying here, it crashes after 3 runs. With simpler scene (ie. not using modifiers, etc.), it never crashes.

As a note, I know when it's going to crash as Blender slows down and I hear my hdd suddenly getting busy (as if the OS is trying to swap memory, though the memory consumption remains quite stable - see the attached screenshot, Blender's Private Byte was around 50MB when it crashed - and there was not other running apps).

Steps to reproduce:
(I've attached a bare bones version of the plugin and a test scene, both are enough to trigger a crash on my system)

  • copy the attached "undo_python_bug_tracking.py" addon file into the Blender "scripts\addons" folder.
  • activate the plugin: menu "Files \ User prefs \ Add-Ons", search for "Tracking undo bug".
  • load the attached .blend file in Blender (attached file is zipped, textures are not included but it should not matter)
  • launch the plugin: menu "File \ Export \ Tracking undo bug". On my system I have to launch it 3 times to crash Blender.

Workaround:
If I remove the "bpy.ops.ed.undo()" command from the addon and manually use the Ctrl+Z shortcut each time after running the addon, it works as expected and Blender never crashes.

  Blender: official release 2.59, r39307 (the issue was the same with all previous 2.5x releases)
  OS: Windows XP Pro SP3
  CPU: P4 2.4GHz
  RAM: 1024 MB
  GFX: nVidia Geforce FX 5500

%%%

%%%Blender uses to crash when I export some objects/meshes with an import/export python addon I've written. Plugin structure: I'm roughly using the following structure when exporting: - turn Undo support off - make single user, apply all tranformations, modifers, etc. to all visible objects - export the data - turn Undo support back on - call Undo once to revert all data to the original state To sum up, the goal is to export the meshes' faces and vertices once they are tranformed by the modifers, scale factor, etc. (I don't know a better way to proceed than applying all transformations before exporting the data...) Issue: Blender uses to crash when I export some scenes with this plugin. It does not crash systematically, for example with the scene I'm supplying here, it crashes after 3 runs. With simpler scene (ie. not using modifiers, etc.), it never crashes. As a note, I know when it's going to crash as Blender slows down and I hear my hdd suddenly getting busy (as if the OS is trying to swap memory, though the memory consumption remains quite stable - see the attached screenshot, Blender's Private Byte was around 50MB when it crashed - and there was not other running apps). Steps to reproduce: (I've attached a bare bones version of the plugin and a test scene, both are enough to trigger a crash on my system) - copy the attached "undo_python_bug_tracking.py" addon file into the Blender "scripts\addons" folder. - activate the plugin: menu "Files \ User prefs \ Add-Ons", search for "Tracking undo bug". - load the attached .blend file in Blender (attached file is zipped, textures are not included but it should not matter) - launch the plugin: menu "File \ Export \ Tracking undo bug". On my system I have to launch it 3 times to crash Blender. Workaround: If I remove the "bpy.ops.ed.undo()" command from the addon and manually use the Ctrl+Z shortcut each time after running the addon, it works as expected and Blender never crashes. ``` Blender: official release 2.59, r39307 (the issue was the same with all previous 2.5x releases) OS: Windows XP Pro SP3 CPU: P4 2.4GHz RAM: 1024 MB GFX: nVidia Geforce FX 5500 ``` %%%
Author

Changed status to: 'Open'

Changed status to: 'Open'

%%%While this shouldn't crash,
Using undo in python isn't going to work well, you're most likely better off to make a copy, modify that, then throw it away.

or... not even copy it - you can just multiply the vertex location by the object.matrix_world to get their worldspace locations on export.

Like this:
wco = obj.matrix_world * vertex.co

%%%

%%%While this shouldn't crash, Using undo in python isn't going to work well, you're most likely better off to make a copy, modify that, then throw it away. or... not even copy it - you can just multiply the vertex location by the object.matrix_world to get their worldspace locations on export. Like this: wco = obj.matrix_world * vertex.co %%%
Author

%%%Thanks for the world matrix tip, Campbell. I was aware of this solution, but it can't fit for a simple reason: we don't get the full mesh as soon as a modifier is used (even a simple but heavily used one like mirror).
In the early development of this plugin, I was using dummy copies, as you also suggested. But as far as I remember, it was not very efficient. And deleting objects still leaves a lot of junk meshes as long as Blender is not restarted (though I think we can now delete them directly from python API).

Using undo in python isn't going to work well

Too bad. Any shot term / long term improvement foreseeable, or is it definitely hopeless?

A really nice solution would be to have a python way to access the meshes exactly as there are displayed in the scene (ie. after modifiers, scale, etc.) as those data are necessarily somewhere in memory. But I know this is not the place for feature request...

And if there's any test I could conduce or any process I should follow to help you locate the bug, please don't hesitate.%%%

%%%Thanks for the world matrix tip, Campbell. I was aware of this solution, but it can't fit for a simple reason: we don't get the full mesh as soon as a modifier is used (even a simple but heavily used one like mirror). In the early development of this plugin, I was using dummy copies, as you also suggested. But as far as I remember, it was not very efficient. And deleting objects still leaves a lot of junk meshes as long as Blender is not restarted (though I think we can now delete them directly from python API). >> Using undo in python isn't going to work well Too bad. Any shot term / long term improvement foreseeable, or is it definitely hopeless? A really nice solution would be to have a python way to access the meshes exactly as there are displayed in the scene (ie. after modifiers, scale, etc.) as those data are necessarily somewhere in memory. But I know this is not the place for feature request... And if there's any test I could conduce or any process I should follow to help you locate the bug, please don't hesitate.%%%

%%%The most most common way to deal with it:

mesh = obj.to_mesh(scene, True, 'PREVIEW')
... export mesh data ...
bpy.data.meshes.remove(mesh)

see export_ply.py which comes with blender for the full example.

Undo is documented to be problematic with scripts: http://www.blender.org/documentation/blender_python_api_2_59_3/info_gotcha.html#undo-redo

Closing.%%%

%%%The most most common way to deal with it: mesh = obj.to_mesh(scene, True, 'PREVIEW') ... export mesh data ... bpy.data.meshes.remove(mesh) see export_ply.py which comes with blender for the full example. Undo is documented to be problematic with scripts: http://www.blender.org/documentation/blender_python_api_2_59_3/info_gotcha.html#undo-redo Closing.%%%

Changed status from 'Open' to: 'Archived'

Changed status from 'Open' to: 'Archived'
Author

%%%I was unaware of the Python API Gotchas document, which actually holds very valuable info.
Thanks a lot for pointing me out to the right direction.%%%

%%%I was unaware of the Python API Gotchas document, which actually holds very valuable info. Thanks a lot for pointing me out to the right direction.%%%

Changed status from 'Archived' to: 'Resolved'

Changed status from 'Archived' to: 'Resolved'
Sign in to join this conversation.
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender-addons#28724
No description provided.