Global Vars

Below is a code snippet, nothing special. Just creates a panel in the 3dView.
The intreset here is in the Global variable “myObjs”.
If you run this, all seems well, and at first works as expected.
But…
Do something in the 3dView (object mode). Add a few objects and move them around.
All is still well.
Now, while watching the dos box (or whatever you call it on your os) hit Undo <ctrl>Z a few time.
The object held in myObjs changes. It starts iterating through all the objects in the scene.
This would be funny if I hadn’t discovered it half way through a project :wink:
Is this a bug in the blender/python implementation, or some odd behavior with python and Global vars that I don’t know about?
aljo

import bpy

myObjs = []
myObjs.append(bpy.data.objects[‘Cube’])

class MyPanelClass(bpy.types.Panel):
bl_space_type = ‘VIEW_3D’
bl_region_type = ‘UI’
bl_label = “My Panel”

@classmethod
def poll(self, context):
    return True

def draw(self, context):
    global myObjs
    layout = self.layout
    col = layout.column()
    col.label(text="Some Text")
    print(myObjs[0])

not sure what i’m supposed to be seeing here.
have you tried it with a current build from graphicall.org? :yes:

The last line of the draw function is “print(myObjs[0])”
It prints the the contents myObjs[0] to the dos box.
Each time you his <ctrl>Z (undo) it changes to something else even though it was assigned the Cube object.

@aljo
Please use


#hier your code between code and /code in square brackets

(at least for bigger source :slight_smile: )

It is even worth, Blender crashes with this code (differently tried)…


import bpy

myObjs = []
myObjs.append(bpy.data.objects['Cube'])

class MyPanelClass(bpy.types.Panel):
  bl_space_type = 'VIEW_3D'
  bl_region_type = 'UI'
  bl_label = "My Panel"

  @classmethod
  def poll(self, context):
    return True

  def draw(self, context):
    global myObjs
    layout = self.layout
    col = layout.column()
    col.label(text="Some Text")
    print("Test", myObjs)