help with checkbox propertie

i defined a checkbox like this at scene level

bpy.types.Scene.MyCkeckbox1 = BoolProperty(
name=“Boolean”,
description=“True or False?”)
bpy.context.scene[‘MyCkeckbox1’] = False

now i can set it up in the panel class i have
but then i need to reset this flag in another function
where it does not work?

scene = bpy.context.scene
MyCkeckbox1=False

bpy.context.scene[‘MyCkeckbox1’] = False

#############

The first line MyCkeckbox1=False does not give any error but does not reset the Flag!
the second line should work but it’s giving an error!

can someone help to make this work in this other function

Thanks happy 2.5

The property value is an attribute of the scene instance.

yes but how do you acces it from inside a function?

i tried to ways and not working in this script not certain why !
cause i’m certain i did this before and it work ok!
or may be it’s a new bug !

i’ll re do some testing again and see what happen here!

with the long one

bpy.context.scene[‘MyCkeckbox1’] = False

get this error

File “lightsetupricky3.py”, line 463, in eraserobjects1
RuntimeError: Writing to ID classes in this context is not allowed: Scene, Scen
datablock, error setting Scene.MyCkeckbox1

this is same line then i have in the main which seems ok there
but no inside this function!

Thanks

First, is MyCkeckbox1 really spelled correctly? Notice the k instead of an h.

Now, I think, if you want to access your property, you should do this :

bpy.context.scene.MyCkeckbox1 = False

And when you do this for the first time:

bpy.context.scene['MyCkeckbox1'] = False

I don’t think you are initializing it to false. I think you should do this when you define your property instead, if you want it to initialize to false :

bpy.types.Scene.MyCkeckbox1 = BoolProperty(
name=“Boolean”,
description=“True or False?”,
default=False)

The first line MyCkeckbox1=False does not give any error but does not reset the Flag!
In the case where the name MyCkeckbox1 would not exist, you would anyway create a new Bool variable. This is why it does not complain.

1- corrected the k for h
2- tried with bpy.context.scene.MyCheckbox1 = True

get same error ID class ect.

one line before that error i can print the value on console
but as soon as this line is executed get the error!

when would this line bpy.context.scene[‘MyCheckbox1’]
be usefull ?
is it only to get the value of the propertie like printing it ?

i got this example from another script and i see now it’s wrong !

thanks happy 2.5

ok did a new little script to see what’ happenign with checkbox!

here it is with a in tool pro panel

###########


 
 
import bpy
from bpy.props import *
from mathutils import *
from math import *
 
 
# Ckeck Box Properties
 
 
bpy.types.Scene.MyCheckbox1 = BoolProperty(
  name="Boolean", 
  description="True or False?")
#bpy.context.scene['MyCheckbox1'] = False
 
bpy.context.scene.MyCheckbox1 = False
 
 # From code snippet   PDF  
 
#bpy.types.Scene.MyBool = BoolProperty(
#name="Boolean",
#description="True or False?")
#bpy.context.scene.['MyBool'] = True
 
 
 
print ()
print (' checkbox  main  **************   =  ',bpy.context.scene['MyCheckbox1'])
print ()
 
bpy.context.scene.MyCheckbox1 = True
 
print (' checkbox  main **************   =  ',bpy.context.scene['MyCheckbox1'])
 
print ()
bpy.context.scene.MyCheckbox1 = False
print (' checkbox  main   **************   =  ',bpy.context.scene['MyCheckbox1'])
print ()
 
 
class Panellightsetup1(bpy.types.Panel):      # This is the panel
 '''Panel'''
 
 bl_label = "Light setup Panel"       # Header Panel's  name
 bl_space_type = "VIEW_3D"        # in 3D view
#   bl_region_type = "TOOLS"        # in tool shelf
 bl_region_type = "TOOL_PROPS"  
 bl_show_header=True  
 
 
 
 #   http://www.blender.org/documentation/250PythonDoc/bpy.types.Panel.html#bpy.types.Panel
 
 def draw(self, context):
 
  global last_menu1         # Access global Var to find the last operation
 
  layout = self.layout
  scene = context.scene
  
  col = layout.column()
  col.label(' Testing Checkbox', 'LAMP_DATA')
 
  col.prop(scene, "MyCheckbox1", text = "Erase all Lamps in Scene ")
 
  if scene.MyCheckbox1:       # 
 
   col.label(' In if test  Checkbox = True', 'LAMP_DATA')
   print ()
   print (' checkbox if test  draw **************   =  ',bpy.context.scene['MyCheckbox1'])
   print (' Check Button =',scene.MyCheckbox1)
 
   str1=str(bpy.context.scene['MyCheckbox1'])
   print ('string=',str1)
   
#   scene.MyCheckbox1=False        # not working
   bpy.context.scene.MyCheckbox1 = False
   
   
   print (' endif test  **************   =  ',bpy.context.scene['MyCheckbox1'])
   
#   col.label(' Value Checkbox =', str1)
#  elif scene.MyCheckbox1=False:
  elif scene.MyCheckbox1==0:
 
   col.label(' In if test  Checkbox = False', 'LAMP_DATA')
   print (' checkbox if test  draw **************   =  ',bpy.context.scene['MyCheckbox1'])
 
 
 
 
 
 
 
 

###########

can you explain why i cannot change the value inside panel?
i an do it outside of it in main part !

happy 2.5

I opened in Blender the Text Editor and copy paste there the above code (tip: you can horizontally scroll the text editor with middle mouse!) and clicked on Run Script. The checkbox showed in the Tool Shelf and when I clicked it to check I could see the error in the python terminal (the terminal external to blender in windows):
RuntimeError: Writing to ID classes in this context is not allowed: Scene, Scene datablock, error setting Scene.MyCheckbox1

It says you can’t assign a value to the bool property in a panel. You are in the draw function that draws the panel, perhaps changing that value would force to enter again the draw function creating a forever loop or something like that so they placed restrictions. Whatever it be is not permitted.

But I don’t understand what you are trying. If you want to answer to the user checking the checkbox doing some task and then unchecking the checkbox to the user be able to check again, then you need a button and not a checkbox. A checkbox is the user the only one that must check or uncheck and then the program acts accordingly.

Bao2 is correct, you are not allowed to change properties from within a draw function.

You can change properties from within pretty much any other function, though, as far as i know.

Also, if you decide to use your script as an add-on, you will find that you cannot create scene properties as you are doing above. See the last post in this thread for a description of that problem and a solution: http://blenderartists.org/forum/showthread.php?t=206742&p=1773170&viewfull=1#post1773170

Ah!

When we will we have “real” events?

but you can change other properties like float or integer ect. i think if i remember well.

i mean it’s the idea of Scene properties to be available anywhere with greater scope then global variables
but your saying that in the case of checkbox it is not allowed to change it inside panel of even a function!
cause i did try it also in a function and it does not work also!

my idea was to check the check box in GUI then execute a function for calculations ect…
then uncheck the checkbox at the end of the function so in GUI it is back to false

like doing the function only ounce instead of several times!

Hum but looks like it cannot be done that way!

now is there a way to use an operator with checkbox may be and how ?

or i can use a normal button with operator to do that except it does not look like a checkbox!
but can add label word like execute or run in button to ececute only ounce the calculations or changes on objects in scene !

Thanks

I think that this was possible in earlyer version (2.55 and before) but now it seems to be a restriction intentionnaly placed there.

Why I say that? I had a very simple script that I published on the upload tracker that did something similar and a developer said :

“… it looks curious how you mingle functionality and layout stuff in one function.”

This script is stucked in the state of evaluation to go to contrib tracker since before christmas. since then, 2.56 has been released and now the script gives me the same error as you said. And I met it in another more recent script.

So it seems that it is no more possible to access a property inside a draw function. I think this has been intentionally put there for avoiding contributer to put fuctionnality into a draw function. For better design. If it’s not the case, than it may be a bug that the dev should know.

@Everyone
Now that this is said, we need to know (and this will be useful for RickyBlender as well as everyone) how to execute code for every redraw of a window. This is why some functionality code is put into a draw function(for it to occur at every redraw).

Is there a better way to make code exectuted at every redraw than to put the code inside a panel draw function(or a header draw function cause it’s the same)?

You can do that with a global bool variable and then check the status of the bool property and the bool global this way: if bool property is true or bool global is true then do something (just now you must be doing: if bool property is true then do something)

so i need to add another var but global and use that one to make the test instead of the properties bool!

i’ll make a test today on this and see if this can work!

did a test and it does not work !

best way is with an operator that one will be done ounce!
i did one with a label operator like “execute”

but is there a way to make a checkbox use an operator ?

happy 2.5

Im wondering how i can set this up. I need 2 checkbox and when one is clicked, i want the other to uncheck, is this even possible?

Rombout : I’m far to be expert, but perhaps you can do that on one checkbox (and the other of course), that will call a function (you must define that function of course)

value = BoolProperty(name=“Value”, update=bool_property_update)

show us some script

have not played with that in a long time
but might not be possible to set a propertie inside draw function!

did you try it ?

happy bl

Maybe something like explained here : https://www.blender.org/api/blender_python_api_2_66_release/bpy.props.html#update-example ?

show us what you got as script with draw function

then can do some testing !

happy bl

You cannot set values in a draw function. What warnotte posted is correct. You want to use an update function. In the update function for the first property, set the value for the second property.


<b>def</b> update_func(self, context):
    <b>self.testprop2 = not self.testprop</b>

bpy.types.Scene.testprop = bpy.props.BoolProperty(update=update_func)
bpy.types.Scene.testprop2 = bpy.props.BoolProperty()

@Bsavery : Thanks, honestly I wasn’t sure because I didn’t do blender scripting for long times :slight_smile: