0
$\begingroup$

I want to do something, save the blend file, then open it again. Then repeat a number of times.

The first time, everything works. The second time, I get this error

    for obj in bpy.context.selected_objects:
AttributeError: 'Context' object has no attribute 'selected_objects'

Is there a way to either put Blender into the correct mode without using Context, or a way to name an object without referring to Context?

import bpy

filename = Path to file

for q in range(4):

    bpy.ops.mesh.primitive_cube_add(size=0.5, enter_editmode=False, align='WORLD', location=(0, 0, q), scale=(1, 1, 1)) # do some stuff

    for obj in bpy.context.selected_objects: # name the object
        obj.name = "monica"

    bpy.ops.wm.save_as_mainfile(filepath=filename) # save the file
    bpy.ops.wm.open_mainfile(filepath=filename) # open the file

Edit, to better explain what I am trying to do: The line adding four objects named "monica" is just a place holder for what I am really doing. I wanted to reduce a much larger project to the minimum non-working example, and was sloppy with that bit. The problem with Context is in the previous line.

I sort of got around the context problem with

selected_objects = [o for o in bpy.data.objects if o.select_get()] 
    selected_objects[0].name = 'foo'

But this workaround isn’t enough as I am using Context for other reasons, such as 3D cursor location, and so on.

Why am I re-opening after save? My project includes many many objects and slows to a glacial speed very early on. I wanted to add a number of objects, free up some memory by saving the Blend file and then starting again.

I have used something like this for each type of data block (in the right order of dependency) and this might be enough.

for block in bpy.data.meshes:
        if block.users == 0:
            bpy.data.meshes.remove(block)

I do not know whether I am achieving anything more by the crude save and open method but I wanted to try.

I do not get the Context problem if I save a progressive sequence of files, adding to the number of objects in each. But I am having problems with quit.

Blender crashes if I try to use this:

bpy.ops.wm.quit_blender()
$\endgroup$
4
  • $\begingroup$ Context is lost when opening a file. Test by adding print(context.copy()) What is the result desired re opening same file after saving? And frankly what are you trying to do here, will never end up with 4 objects named "monica" in same file???? $\endgroup$
    – batFINGER
    May 18, 2021 at 10:59
  • $\begingroup$ You are right. I have edited my question to better explain what I am trying to do. $\endgroup$ May 18, 2021 at 11:35
  • $\begingroup$ Perhaps the issue is code we don't see here, which if unnecessarily operator-centric will have bad performance blender.stackexchange.com/questions/7358/…. Can add an object via the api new_data = bpy.data.objects.new(name, data) or copy via ob_copy = ob.copy() IMO constantly saving as above is undesirable. Weigh up whether this is an XY Problem that may be better evaluated by looking at why it is getting slow. $\endgroup$
    – batFINGER
    May 18, 2021 at 12:41
  • $\begingroup$ I think you are right. Now I have tried to ask the right question here: blender.stackexchange.com/questions/223074/… $\endgroup$ May 19, 2021 at 17:19

0

You must log in to answer this question.

Browse other questions tagged .