Can't retrieve .data or .context at Blender startup in python

I’m trying to write a addon that will perform an if statement when Blender boots. The problem I’m having is that if I ask for something like bpy.data.scenes[0].objects I get an error like this:
Screenshot from 9-28-2022 12-40-49
It appears that, for whatever reason, Blender locks all access to information when booting. Is there a way to get around this?

Try bpy.context.scene.objects instead.

Same issue. Except then it gives _RestrictContext instead of _RestrictData.

Can you show your import statements? I suspect that you might have stated them incorrectly.

Related:

1 Like

This is the current one I’m using.
if bpy.context.blend_data.filepath == "C:\\Program Files\\Blender Foundation\\Blender\\2.79\\scripts\\presets\\BT_Templates\\2D_Template_01.blend":
I also used bpy.data.filepath which produced the same result.

you can’t use context or data during startup because they don’t fully exist yet (blender is still in the process of filling the data structures). during registration, set a timer for .01 seconds or something very small, and when it triggers- perform your startup tasks that need context.
I’m surprised that with a username like “BlenderTimer” you didn’t already think of this solution :slight_smile:

2 Likes

Bro, I agree! Every time I’m having issues getting something to work in a script it’s either insanely complicated to fix or insanely easy! :rofl: (this time it appears to be easy)

I’ll check that tomorrow and see if it works.

Soo… This didn’t work.
I get an error like this:
Screenshot from 9-29-2022 9-51-38
I literally copied and pasted from the api example, so I don’t know what the deal is.

I couldn’t say without seeing your full code… are you trying to run this in headless mode or from the command line? some weird thing like that by chance?

Doesn’t matter. I’ve tested it multiple ways (even from the interactive console) they all give the same error. :unamused:

Ok, finally got this method working.
Thanks!