0
$\begingroup$

I have an addon I wrote some years ago, around 2.80 days, it's been working fine since with some small tweaks along the way with version updates.

But with version >=4.0 a new bug has shown up...

The addon first remembers the current scene and view_layer, then at the top of its modal it checks if the user has changed the Scene, and forcefully changes it back. Then it checks if the user has changed view_layer and forcefully changes it back to the original one.

def __init__(self):
    self.context = bpy.context
    self.scene_orig = self.context.scene
    self.view_layer_orig = self.context.view_layer

def modal(self, context, event):

    # check for scene change
    scene_check = bpy.context.scene
    if scene_check is not self.scene_orig:
        print("orig scene: ", self.scene_orig)
        print("curr scene: ", scene_check)
        bpy.context.window.scene = self.scene_orig

    # check for view layer change
    view_layer_check = bpy.context.view_layer
    if view_layer_check is not self.view_layer_orig:
        print("orig view_layer: ", self.view_layer_orig)
        print("curr view_layer: ", view_layer_check)
        bpy.context.window.view_layer = self.view_layer_orig

Running the addon does not print the scene change, but it ALWAYS print the view_layer change! And the view_layer is always equal (I think) so it should not print!

OUTPUT:

orig view_layer:  <bpy_struct, ViewLayer("View Layer") at 0x7f133278f888>
curr view_layer:  <bpy_struct, ViewLayer("View Layer") at 0x7f133278f888>
orig view_layer:  <bpy_struct, ViewLayer("View Layer") at 0x7f133278f888>
curr view_layer:  <bpy_struct, ViewLayer("View Layer") at 0x7f133278f888>

This repeats for every modal event...

Now here's the really weird bit... If I zoom in or zoom out (WHEELUPMOUSE or WHEELDOWNMOUSE) Blender crashes after several quick scrolls. This only happens with zooming, not with any other event.

If I comment out

    if view_layer_check is not self.view_layer_orig:
        print("orig view_layer: ", self.view_layer_orig)
        print("curr view_layer: ", view_layer_check)
        bpy.context.window.view_layer = self.view_layer_orig

Then it does not crash.

I can only assume Blender is trying to overwrite the view_layer with every modal event, bad, but the compare always returns the same view_layer, so it should not run 😤😤 If I make a new View Layer and change to it then the code correctly sets the original view_layer back...

Any ideas why:
A: The view_layer compare is returning false even though they get print the same? Should I not be using is not here? I thought this was correct, and has worked for years.
B: Why Blender would crash ONLY when zooming? (not really important, just curious)

$\endgroup$
5
  • $\begingroup$ Hello, in python is not is not the same things as !=. Have you tried using the inequality operator ? stackoverflow.com/questions/2209755/python-operation-vs-is-not $\endgroup$
    – Gorgious
    Commented Jun 27 at 12:56
  • $\begingroup$ In particular, is(not) is great to check identity, but python wrappers in Blender are short-lived and temporary, so e.g. armature.bones['Bone'] is armature.bones['Bone'] will result in False. Meanwhile == and != will compare the identities in this case. $\endgroup$ Commented Jun 27 at 13:34
  • $\begingroup$ You're also using the bpy.context not the context passed into the modal, which as also not recommended. I don't know if that's the problem 100% but it's not generally advised to do that. $\endgroup$
    – Jakemoyo
    Commented Jun 27 at 14:32
  • $\begingroup$ @Gorgious @Markus von Broady Don't know why I didn't even try it, but that worked! Thank you!! Using != fixed it! Not sure why it used to work but here we are, thanks again! $\endgroup$
    – Psyonic
    Commented Jun 28 at 10:34
  • $\begingroup$ @Jakemoyo Thanks man! I was using the passed context but was trying things to bug hunt... $\endgroup$
    – Psyonic
    Commented Jun 28 at 10:35

0

You must log in to answer this question.

Browse other questions tagged .