Ability to Reset Blender to an 'empty-file' #47418

Closed
opened 2016-02-15 15:45:36 +01:00 by Campbell Barton · 32 comments

This would allow you to ask for a new, Blend file that contains no data.
This is mainly useful to Python developers (use case from developer question )

If for example you're writing a script that loads a Blend file and renders it (or saves it to a new file).

Removing all data in a file is not very convenient.

  • Calling bpy.ops.wm.read_homefile() will load the users startup.blend, which may contain any number of data-blocks, so there is no certainty that the file can be easily cleared afterwards. Note that a custom file can be passed but this means the developer has to keep an empty file available.
  • Calling bpy.ops.wm.read_factory_settings() will give a predicable outcome, however it also re-initializes the user-prefereces, reloads fonts, all add-ons, so this is also not great either.

Here's an example of how this can be done currently.

eg:

  import bpy
  def reset_blend():
      bpy.ops.wm.read_factory_settings()
      for scene in bpy.data.scenes:
          for obj in scene.objects:
              scene.objects.unlink(obj)
    # only worry about data in the startup scene
      for bpy_data_iter in (
              bpy.data.objects,
              bpy.data.meshes,
              bpy.data.lamps,
              bpy.data.cameras,
              ):
          for id_data in bpy_data_iter:
              bpy_data_iter.remove(id_data)
  reset_blend()

Instead, we could have an option to reset with no data. eg:

eg:

bpy.ops.wm.read_homefile(empty=True)
bpy.ops.wm.read_factory_settings(empty=True)

Internally this will likely just remove the data as the script above is doing, however it can be done in one place so developers don't have to do themselves.

This would allow you to ask for a new, Blend file that contains no data. This is mainly useful to Python developers ([use case from developer question ](http://blender.stackexchange.com/q/46990/55)) If for example you're writing a script that loads a Blend file and renders it (or saves it to a new file). Removing all data in a file is not very convenient. - Calling `bpy.ops.wm.read_homefile()` will load the users `startup.blend`, which may contain any number of data-blocks, so there is no certainty that the file can be easily cleared afterwards. Note that a custom file can be passed but this means the developer has to keep an empty file available. - Calling `bpy.ops.wm.read_factory_settings()` will give a predicable outcome, however it also re-initializes the user-prefereces, reloads fonts, all add-ons, so this is also not great either. Here's an example of how this can be done currently. eg: ``` import bpy ``` ``` def reset_blend(): bpy.ops.wm.read_factory_settings() ``` ``` for scene in bpy.data.scenes: for obj in scene.objects: scene.objects.unlink(obj) ``` # only worry about data in the startup scene ``` for bpy_data_iter in ( bpy.data.objects, bpy.data.meshes, bpy.data.lamps, bpy.data.cameras, ): for id_data in bpy_data_iter: bpy_data_iter.remove(id_data) ``` ``` reset_blend() ``` Instead, we could have an option to reset with no data. eg: eg: `bpy.ops.wm.read_homefile(empty=True)` `bpy.ops.wm.read_factory_settings(empty=True)` Internally this will likely just remove the data as the script above is doing, however it can be done in one place so developers don't have to do themselves.
Author
Owner

Changed status to: 'Open'

Changed status to: 'Open'
Author
Owner

Added subscriber: @ideasman42

Added subscriber: @ideasman42
Campbell Barton changed title from Ability to reset Blender to an empty file. to Ability to Reset Blender to an 'empty-file' 2016-02-15 15:47:01 +01:00

Added subscriber: @rdengate

Added subscriber: @rdengate
RG Dengate self-assigned this 2016-02-28 05:05:25 +01:00

t47418_fix.diff

Hi there,

I've attached a git diff that addresses this. This is my first time coding in Blender and so I'm sure my fix is not the most elegant solution. I'm happy to make changes based on feedback. Likewise please let me know if there are docs/tests I should update (I couldn't find any).

Hope this helps.

[t47418_fix.diff](https://archive.blender.org/developer/F286136/t47418_fix.diff) Hi there, I've attached a git diff that addresses this. This is my first time coding in Blender and so I'm sure my fix is not the most elegant solution. I'm happy to make changes based on feedback. Likewise please let me know if there are docs/tests I should update (I couldn't find any). Hope this helps.
Author
Owner

Thanks for checking on this, some requested change.

  • clearing the file should be made into a function.
  • the while loops - while(cam != bmain->camera.last), why keep the last camera?
  • the next variables can be in the while loops scope (no need to have them above).
  • use tabs instead of spaces.
Thanks for checking on this, some requested change. - clearing the file should be made into a function. - the while loops - `while(cam != bmain->camera.last)`, why keep the last camera? - the next variables can be in the while loops scope (no need to have them above). - use tabs instead of spaces.

Thanks Campbell! I will address those changes soon.

Thanks Campbell! I will address those changes soon.

Added subscriber: @MikhailRachinskiy

Added subscriber: @MikhailRachinskiy

From the description I understand that this will not create completely empty blend file which would still contain user settings, space data, sculpt/texture brushes?

From the description I understand that this will not create completely empty blend file which would still contain user settings, space data, sculpt/texture brushes?

@MikhailRachinskiy User preferences are kept if using read_homefile, but not if using read_factory_settings. Either way, I believe custom sculpt brushes are removed. I'm not sure what you mean by space data, sorry (I'm still learning Blender).

@ideasman42 In the loop in which I delete all the cameras in the scene (and the loops for meshes etc, they're all the same) I'm trying to delete every camera, and the end result is that they are all deleted, but it's very clumsy. I end up freeing the last camera in the scene after the while loop has ended which is not intuitive. What I really want to do is iterate over every camera in the scene and delete it, preferably using "for (cam = bmain->camera.first; cam; cam = cam->id.next)" but that doesn't work because id.next is read from the camera, which then can't be deleted during the loop. I've added comments in the code for this to make it clearer, but if you know how to improve this I'm happy to change it.

Here is a patch with the changes discussed: t47418_revised.diff. Please let me know if there are other changes I should make. Thanks for the code review :-)

@MikhailRachinskiy User preferences are kept if using read_homefile, but not if using read_factory_settings. Either way, I believe custom sculpt brushes are removed. I'm not sure what you mean by space data, sorry (I'm still learning Blender). @ideasman42 In the loop in which I delete all the cameras in the scene (and the loops for meshes etc, they're all the same) I'm trying to delete every camera, and the end result is that they are all deleted, but it's very clumsy. I end up freeing the last camera in the scene after the while loop has ended which is not intuitive. What I really want to do is iterate over every camera in the scene and delete it, preferably using "for (cam = bmain->camera.first; cam; cam = cam->id.next)" but that doesn't work because id.next is read from the camera, which then can't be deleted during the loop. I've added comments in the code for this to make it clearer, but if you know how to improve this I'm happy to change it. Here is a patch with the changes discussed: [t47418_revised.diff](https://archive.blender.org/developer/F304797/t47418_revised.diff). Please let me know if there are other changes I should make. Thanks for the code review :-)
Author
Owner
@rdengate, theres no need to compare with the last item. see: https://developer.blender.org/diffusion/B/browse/master/source/blender/windowmanager/intern/wm.c;8dcdde52b17228601c95fed04f5bca03484d27ac$473

Aha, I see what you mean! Thanks, that's much better. Updated diff here: t47418_revised_02.diff

Aha, I see what you mean! Thanks, that's much better. Updated diff here: [t47418_revised_02.diff](https://archive.blender.org/developer/F304801/t47418_revised_02.diff)

@rdengate So it’s just using a “factory settings” blend file with all default screen layouts, default brushes etc. This is unfortunate, I was searching for a way to create a completely empty blend file. sigh

@rdengate So it’s just using a “factory settings” blend file with all default screen layouts, default brushes etc. This is unfortunate, I was searching for a way to create a completely empty blend file. *sigh*

@MikhailRachinskiy I believe it's using a factory settings blend file if using read_factory_settings, or using the user's startup.blend if using read_homefile. In both cases it's deleting data from the file after it's loaded (if empty is passed as an argument). What data did you need to delete exactly? I'm trying to understand what you're after. Since you need a completely empty file, why can't you create one and set it as the startup.blend, then use read_homefile?

@MikhailRachinskiy I believe it's using a factory settings blend file if using read_factory_settings, or using the user's startup.blend if using read_homefile. In both cases it's deleting data from the file after it's loaded (if empty is passed as an argument). What data did you need to delete exactly? I'm trying to understand what you're after. Since you need a completely empty file, why can't you create one and [set it ](http://blender.stackexchange.com/questions/186/how-to-change-blenders-default-startup-settings) as the startup.blend, then use read_homefile?

@rdengate wrote:
What data did you need to delete exactly?

  • All objects and object data: Objects, Meshes, Materials, Textures, Lamps
  • Default data: Brushes, Line styles, Worlds
  • Scene ID properties (if the are any)
  • Screens (except one)
  • Space data (with “Clean-up Space-data” operator)

I need this for an asset blend file.

Since you need a completely empty file, why can't you create one

That is what I am doing at the moment, very tedious, and even if I make special script for that, I would still need to track additional default data in new Blender versions.

Well my use case (or my requirements rather) quite specific, I guess factory settings blend file would do just fine for general purpose.

> @rdengate wrote: > What data did you need to delete exactly? * All objects and object data: Objects, Meshes, Materials, Textures, Lamps * Default data: Brushes, Line styles, Worlds * Scene ID properties (if the are any) * Screens (except one) * Space data (with “Clean-up Space-data” operator) I need this for an asset blend file. > Since you need a completely empty file, why can't you create one That is what I am doing at the moment, very tedious, and even if I make special script for that, I would still need to track additional default data in new Blender versions. Well my use case (or my requirements rather) quite specific, I guess factory settings blend file would do just fine for general purpose.

That makes sense. Thanks for the info. It does sound like a very specific use case.

That makes sense. Thanks for the info. It does sound like a very specific use case.
Author
Owner

@MikhailRachinskiy, its now possible to save individual data blocks - https://wiki.blender.org/index.php/Dev:Ref/Release_Notes/2.78/Add-ons#New_APIs

However think your suggestion is reasonable for an 'empty' file.

@MikhailRachinskiy, its now possible to save individual data blocks - https://wiki.blender.org/index.php/Dev:Ref/Release_Notes/2.78/Add-ons#New_APIs However think your suggestion is reasonable for an 'empty' file.

@ideasman42 its now possible to save individual data blocks

May I say that it is super duper cool and exactly what I was searching for! But why there is no option to save compressed?

As for the topic, I read the use case on stackexchange and I do not understand why do we need to read_homefile or read_factory_settings if we could just use bpy.ops.wm.open_mainfile(filepath=bpy.data.filepath) instead?
That way we are not limited to startup.blend and default factory settings blend files.

> @ideasman42 its now possible to save individual data blocks May I say that it is super duper cool and exactly what I was searching for! But why there is no option to save compressed? As for the topic, I read the use case on stackexchange and I do not understand why do we need to `read_homefile` or `read_factory_settings` if we could just use `bpy.ops.wm.open_mainfile(filepath=bpy.data.filepath)` instead? That way we are not limited to startup.blend and default factory settings blend files.

@ideasman42 Should empty=True for read_homefile and read_factory_settings delete more data than in my implementation, i.e. to get the file to the stage Mikhail is talking about (a completely empty file)?

@ideasman42 Should empty=True for read_homefile and read_factory_settings delete more data than in my implementation, i.e. to get the file to the stage Mikhail is talking about (a completely empty file)?
Author
Owner

@rdengate, yes, I think it should give something as close to an empty file as possible.

@MikhailRachinskiy, added support for writing compressed blend files 57d98a80

@rdengate, yes, I think it should give something as close to an empty file as possible. @MikhailRachinskiy, added support for writing compressed blend files 57d98a80

@rdengate @ideasman42, what’s the use case of this feature? For a given use case open_mainfile is a much better fit.

Thanks for “save compressed” option Campbell.

@rdengate @ideasman42, what’s the use case of this feature? For a given use case `open_mainfile` is a much better fit. Thanks for “save compressed” option Campbell.
Author
Owner

@MikhailRachinskiy, not sure how open_mainfile relates? - making an empty file is mainly useful when you want to create a file from scratch and save it without having to manually clear the data.

@MikhailRachinskiy, not sure how `open_mainfile` relates? - making an empty file is mainly useful when you want to create a file from scratch and save it without having to manually clear the data.

@ideasman42
not sure how open_mainfile relates?

In the description to this task this stackexchange question is specified as a use case for this feature, where open_mainfile is a much better solution.

empty file is mainly useful when you want to create a file from scratch and save it without having to manually clear the data.

Isn't libraries.write were created specifically for this purpose?

My point is: if we want to clear newly loaded data we can use open_mainfile, if we want to write to an empty blend file we can use libraries.write. Where is the use case for read_factory_settings(empty=True)?

> @ideasman42 > not sure how `open_mainfile` relates? In the description to this task [this stackexchange question ](http://blender.stackexchange.com/questions/46990/how-to-completely-remove-all-loaded-data-from-blender) is specified as a use case for this feature, where `open_mainfile` is a much better solution. > empty file is mainly useful when you want to create a file from scratch and save it without having to manually clear the data. Isn't `libraries.write` were created specifically for this purpose? My point is: if we want to clear newly loaded data we can use `open_mainfile`, if we want to write to an empty blend file we can use `libraries.write`. Where is the use case for `read_factory_settings(empty=True)`?

Perhaps I should suspend work on this until a clear use case emerges. Do you agree @ideasman42?

Perhaps I should suspend work on this until a clear use case emerges. Do you agree @ideasman42?
RG Dengate was unassigned by Campbell Barton 2017-03-03 15:07:11 +01:00

Added subscriber: @wongalvis

Added subscriber: @wongalvis

@ideasman42 What's the current status of this task? I'm interested in contributing to this hack.

@ideasman42 What's the current status of this task? I'm interested in contributing to this hack.
Author
Owner

@MikhailRachinskiy however you managed to solve your particular use-case, the ability to reset Blender to a blank state is quite reasonable.

We have automated tests for example, where it would be useful to be able to start with a blank Blender state for each test.


@wongalvis - nobody is working on this currently.

@MikhailRachinskiy however you managed to solve your particular use-case, the ability to reset Blender to a blank state is quite reasonable. We have automated tests for example, where it would be useful to be able to start with a blank Blender state for each test. ---- @wongalvis - nobody is working on this currently.

@ideasman42 as I understand this option performing data cleanup after Factory Settings file is loaded. Which means as soon as Factory Settings would receive new default data—this feature will become obsolete and need to be updated.

Isn't it better to leave this task to automated tests?

@ideasman42 as I understand this option performing data cleanup after Factory Settings file is loaded. Which means as soon as Factory Settings would receive new default data—this feature will become obsolete and need to be updated. Isn't it better to leave this task to automated tests?
Author
Owner

Which means as soon as Factory Settings would receive new default data—this feature will become obsolete and need to be updated.

Factory settings are changed very rarely, its possible this function will need updating when they do, this doesn't make it obsolete.

As for tests doing this, yes - they do this already, its quite awkward IMHO.

  ./tests/python/batch_import.py:59:
  ./tests/python/bl_mesh_modifiers.py:128:
  ./tests/python/bl_run_operators.py:320:

All duplicate similar logic, which is good enough reason IMHO for Blender to have the functionality built-in.

> Which means as soon as Factory Settings would receive new default data—this feature will become obsolete and need to be updated. Factory settings are changed very rarely, its possible this function will need updating when they do, this doesn't make it obsolete. As for tests doing this, yes - they do this already, its quite awkward IMHO. ``` ./tests/python/batch_import.py:59: ./tests/python/bl_mesh_modifiers.py:128: ./tests/python/bl_run_operators.py:320: ``` All duplicate similar logic, which is good enough reason IMHO for Blender to have the functionality built-in.

Then what are criteria for an empty file?
I think we all can agree that a file with objects, object data, materials and textures cannot be empty.

But what about brushes? Freestyle linestyles? Screens? Render layers? Worlds?
bf.png

Then what are criteria for an empty file? I think we all can agree that a file with objects, object data, materials and textures cannot be empty. But what about brushes? Freestyle linestyles? Screens? Render layers? Worlds? ![bf.png](https://archive.blender.org/developer/F525960/bf.png)
Author
Owner

@MikhailRachinskiy, right - all of this would be removed (or not created in the first place). Except a single empty scene.

@MikhailRachinskiy, right - all of this would be removed (or not created in the first place). Except a single empty scene.
Author
Owner

nobody picked this up and maybe wasn't that simple.
committed df7f6a3e2e for 2.79

nobody picked this up and maybe wasn't that simple. committed df7f6a3e2e for 2.79
Author
Owner

Changed status from 'Open' to: 'Resolved'

Changed status from 'Open' to: 'Resolved'
Campbell Barton self-assigned this 2017-03-29 11:31:00 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
4 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#47418
No description provided.