Python addon errors while in blender code runs

Hey

I’m losing my mind here slowly.

The addon is a bit “complex” as it imports paths/modules/reloads them etc etc so I’ll skip trying to make a usable version of it for public for now untill its last resort…

The issue I’m having is that when I run this code:

        selection_names = bpy.context.selected_objects
        print(selection_names)

I get

AttributeError: 'Context' object has no attribute 'selected_objects'

The same line works inside blender.

Also when I run this code in addon

        for obj in bpy.context.scene.objects:
            print(obj, obj.name)

It works just fine…

Whhh what am I doing wrong here?

Regards
Dariusz

edit1. I tried registering the class using bpy.utils.register_class(extra) sadly no luck :confused:

You can’t access bpy.context from within an operator class, if that’s what you’re trying to do (since the context is given to the object when it is created). Use the context provided by the (self, context) in your execute or invoke function.

Hello @Josephbburg
Yeh I have just arrived at that conclussion! Uhh that was fun evening… but I’m not sure how to pass context up stream atm… kinda lost with this somehow whh need to re-read docs another 10x to get it :- ) thank you for help.

Nope still stuck, as far as I can tell def execute(self,context) gives me a valid context, now since I run pySide widget with functioins, I wante to store it and use latter. But as far as I can tell once function run off context becomes invalid and its a None from that point. Unless I messed up my singeltons again sigh…

How to get proper context during function call outside execute() ?

TIA

Do you have a gitlab or something with your code? It’s hard to help without a lot more information.

The word “singleton” makes me suspect that you’re doing OOP when it really isn’t necesary or well-advised.

If your operator is causing the context to become invalid, you should probably write it as a modal operator instead.

@Josephbburg Hey, no git yet, I think I might make one.

So far I managed to get a valid context that exists during the duration of my pyside widget, however even when I try to use it > self.blenderContext.selected_objects I still get AttributeError: 'Context' object has no attribute 'selected_objects' which means that the bpy.context.selected_objects and context from execute(self,context) are equally “broken”… lost again :- )

I just ran a test, tried the same function call inside execute() and then inside my widget function, the execute works but my widget function does not. This is bananas. Why on earth is this context so protected to the point where its unusable with 3rd party gui libraries? I checked both contextes and they both are “the same object”


EXECUTE CONTEXT <bpy_struct, Context at 0x0000020F884E4478>
[bpy.data.objects['Cube']] None

END GUI SETUP

GUI CONTEXT <bpy_struct, Context at 0x0000020F884E4478>
line 40, in hideSelected
    print("hahah", self.blenderContext.selected_objects)
AttributeError: 'Context' object has no attribute 'selected_objects'

For selected objects in 2.8 I use this:

sel_objs = bpy.context.view_layer.objects.selected

Can you try this?

1 Like

Yep this works, how did you figure that out? I’m struggling when I get google results as there seem to be a lot of api changes and I dunno which one is the valid/correct one.

Unfortunately there is a lot of “miss-information” out there in Googleland… I tend to trust the API docs most, make sure you read the docs for the latest version of Blender.

Edit:

I think the point is that the context changes so much from different parts of Blender, but view_layer is always view_layer, so you are forcing it to look there for the objects.

Hey

yep true that, but how did you know to look in to view_layer for selected objects?

I want to do stuff like select by code/deselect by code/ show/hide/ lock trasform/ apply matterials/get matterials list/import matterialls/etc/etc/etc…

I’m bit in dark, can you ping me any of the classes that I should be looking at?

I still think that if the context is changing, there’s something wrong in the structure of the code.

@Dariusz is this a modal operator or not? If you’re using a widget to interact with it, you need to make it modal.

You can find just about anything in Python by:

for attr in dir(object):
    print(attr)

But also the API docs have all of this if you are willing to go many pages deep in hyperlinks. The manual pages are laid out in a sort of Object-Oriented way, just like the class hierarchy in the API itself.

It’s very difficult to pinpoint a really good resource, other than asking here. That is how I learnt to write an add-on. People here are very helpful, but sometimes you have to ask the question several ways before others realise what you want to do, so give as much information as you can and are as much of your code as you can, that makes it easier to understand the context of your question.

The API is not easy to follow and there are not many worked examples, I leant programming 30 odd years ago and languages were much simpler in structure then. Principles don’t change though, the problem we have now is some many people publish stuff on the internet that is never fact checked, it can be very confusing…

Context depends on so many things, it can be changed in code, or it can be controlled by code, so you only look in the place where you know the answer will be, it is a complex subject and takes a long time to fully understand.

Example:

You can change the context to a specific editor type like this:

bpy.context.area.type = "VIEW_3D"

This tells Blender to take the context of the 3D View, you could then switch that to say the Node Editor in the same script, even switching it every iteration in a for loop.

Sorry this isn’t a “look here” answer, but its the best I can do…

Hey

No worries and thank you for help ! It already helped me a lot with what you posted.

That context feels to me like openGl context somehow… is that like old legacy programming or something? I also noticed that some contextes are somewhat protected from using within addonn… So many hiccups and online tutorials are like 50% hit n miss ^^ Fun times :- )

bpy.context is a Python object, it’s not legacy programming. It’s not related to OpenGL. Blender used to have OpenGL drawing functions, but they’ve been removed or replaced… IIRC that’s why Retopoflow still hasn’t been ported to 2.8.

I forgot to say that you shouldn’t try to modify the context, and I think Python will throw an error if you do.

post as much of your code as you can

Is the best advice. And your code is GPL2, anyways, right? So you have no reason not to post it for us to look at! :smiley:

1 Like