0
$\begingroup$

Consider this a continuation of my previous question. I found the following post in which is the following code:

import bpy
import os

root_directory  = "/home/eriadar/city"
destination_directory = root_directory + "/data/plant_objects/"

for idx, obj in enumerate(bpy.data.objects):
    ctx = bpy.context.copy()
    ctx['selected_objects'] = obj
    filename = 'file_number' + str(idx) + '.blend'
    filepath = os.path.join(destination_directory, filename)
    bpy.data.libraries.write(filepath, set(ctx['selected_objects']), fake_user=True)

I use that code after first creating a single object, and get the following error:

#! Traceback (most recent call last):
#!   File "/usr/lib/python3.8/code.py", line 90, in runcode
#!     exec(code, self.locals)
#!   File "<blender_console>", line 1, in <module>
#! FileNotFoundError: [Errno 2] No such file or directory: 'plant_0.blend'
#! 

..which makes no sense. No, 'plant_0.blend' is not a file or directory, it doesn't exist yet! I'm trying to -create- it! :-)

Edit: Indications of solution? Perhaps the whole way I'm thinking about this is wrong?

Edit: I'm gradually coming to the realization, or suspicion/belief that blender stores everything it creates internally. This is very different behavior from what I was expecting. Ordinary operation (say for a word document) is for you to create a file then have to save it. Give it a name, etc. In blender, it seems that's done -automatically-. The challenge becomes to -delete- it. I'm trying to complete my understanding but.. If this is true, then each time a create a new object within a blend file, the -size- of that file should increase. I have now done that test, and it seems that it does not. Perhaps the different is so small that my computer approximates it as still being the same thing?

Edit: I won't go so far as to say I have the final answer, but I've learned enough about blender now that this question is mostly obsolete. I'm cleaning my blend files of sometimes huge amounts of junk that accumulated when I didn't know what blender was doing. And these files I've been trying to save.. they get saved internally automatically. getting -rid- of them is the challenge. I think I have at least partially documented in my other recent questions how I'm doing things. ..I suppose that technically, the correct answer to this question is simply that when specifying the blend file you want to save to, you have to select one that already exists. what happens when that blend file already contains a data-block of the same name.. ..don't know.

$\endgroup$
2
  • 1
    $\begingroup$ You may find this useful as a work around link $\endgroup$
    – Ratt
    Commented Apr 23, 2022 at 0:12
  • $\begingroup$ @Ratt -yes, I've been reading that page. It's not actually necessary. I'm gradually coming to the realization, or suspicion/belief that blender stores everything it creates internally. This is very different behavior from what I was expecting. Ordinary operation (say for a word document) is for you to create a file then have to save it. Give it a name, etc. In blender, it seems that's done -automatically-. The challenge becomes to -delete- it. I'm trying to complete my understanding but.. $\endgroup$
    – juggler
    Commented Apr 23, 2022 at 1:02

1 Answer 1

1
$\begingroup$

Not really a full answer - but too long for a comment:

When i had a similar problem, i roughly implemented the following workflow:

  • Decide of objects that you want to store in separate blend files.
  • For each of that objects, open Blender with no blend file, but a script doing:
    • for obj in bpy.data.objects: bpy.data.objects.remove(obj)
      to get rid of defaut cube, light and camera.
    • bpy.ops.wm.save_as_mainfile(filepath='pathToNewBlendFile.blend')
      to allow the use of relative paths (only needed when you link external files like textures).
    • bpy.ops.wm.append(directory='nameOftheBlendFileWithAllObjects', filename='objectName', link=False, autoselect=False, set_fake=True, use_recursive=True)
      to import the object. set_fake=True prevents automatic cleanup of the object when unused (important when just storing a material or action).
    • bpy.ops.wm.save_as_mainfile(filepath='pathToNewBlendFile.blend')
      to save a minimal Blend file with the object.
$\endgroup$
1
  • $\begingroup$ Thanks for the response! ..I should have come back here and edited this question once I figured out what I was going to do (which I haven't yet, I suppose, not completely). I'm cleaning up my blend files now, realizing that the fact that things are automatically stored internally means that there is no need for a separate elaborate saving operation. So your answer may become relevant again if and when I decide to become more sophisticated again. $\endgroup$
    – juggler
    Commented Apr 25, 2022 at 18:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .