How to create a checkbox in the Properties>Render tab?

sorry for the noob question, but i can't create a simple checkbox inside the render tab... Here is my code...

import bpy
from bpy import context
from math import degrees, floor
#from bpy.types import *
from bpy_extras.io_utils import ExportHelper
from bpy.props import BoolProperty
from bpy.types import (Panel,
Operator,
PropertyGroup,
)
class MySettings(PropertyGroup):

my_bool = BoolProperty(
name = "render scene",
description = "render a reference copy of the scene",
default = False
)

class BlenderCam_exportFile(bpy.types.Operator):
bl_idname = "myops.blendercam_export_file"
bl_label = "Export File"

def execute(self, context):
#------------------------------------
#    my code is here....
#------------------------------------
return {'FINISHED'}

class BlenderCamPanel(bpy.types.Panel):
"""Creates a Panel in the Render context of the properties editor"""
bl_label = "BlenderCam"
bl_idname = "blendercam_panel"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "render"

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

def draw(self, context):
layout = self.layout
scene = context.scene
rd = scene.render
#label
layout.label(text="Label")
#chechbox
layout.prop(rd, "my_bool", text="Bool Property")
#button
row = layout.row()
row.operator("myops.blendercam_export_file")

def register():
bpy.utils.register_class(BlenderCam_exportFile)
bpy.utils.register_class(BlenderCamPanel)
bpy.utils.register_class(MySettings)

def unregister():
bpy.utils.unregister_class(BlenderCam_exportFile)
bpy.utils.unregister_class(BlenderCamPanel)
bpy.utils.unregister_class(MySettings)

if __name__ == "__main__":
register()


The code runs fine and doesn't return any error, but while the label and the button are present, the checkbox doesn't show up... Any clue?

• Indent your code in the class declaration for MySettings, add a pointer in the register section bpy.types.Scene.my_props = bpy.props.PointerProperty(type=MySettings) as well as removing the pointer in the unregister section del bpy.types.Scene.my_props in you panel change to layout.prop(scene.my_props, "my_bool", text="Bool Property") and it should function but I'll let someone else answer for cleaner coding. – Ratt Dec 9 '18 at 20:27

I am no python expert however the reason why the bool wasn't showing it was because it was assigned to some property that didn't exists. If you launch blender from the terminal is easier to spot those problems.

code...
rd = scene.render
code...

output
/Text:58


--

Looking the documentation,

layout defines the structure of the panel in the UI and prop exposes the RNA item and places it into the layout.

The prop function takes data and property as arguments which in your case was data=rd property=“my_bool”. now the problem was arising because the variable rd was assigned to scene.render thus layout.propr(data=scene.render, property=“my_bool”)

looking the error property not found: RenderSettings.my_bool we can understand that the scene.render was in fact the wrong call as it was calling the RenderSettings where there was no property "my_bool".

hence creating the bpy.types.Scene.my_tool and assign it to PointerProperty(type=MySettings) (that points to the BoolProperty where my_bool exists) solved the problem when calling layout.propr(data=scene.my_tool, property=“my_bool”) because in fact now we know that my_tool has an attribute my_bool

--

I am not really familiar with the blender addon workflow so I look How can I add a checkbox in the tools UI? and made the changes to your code accordingly .

import bpy
from bpy import context
from math import degrees, floor
#from bpy.types import *
from bpy_extras.io_utils import ExportHelper
from bpy.props import (StringProperty,
BoolProperty,
IntProperty,
FloatProperty,
FloatVectorProperty,
EnumProperty,
PointerProperty,
)
from bpy.types import (Panel,
Operator,
PropertyGroup,
)

class MySettings(PropertyGroup):

my_bool = BoolProperty(
name = "render scene",
description = "render a reference copy of the scene",
default = False
)

class BlenderCam_exportFile(bpy.types.Operator):
bl_idname = "myops.blendercam_export_file"
bl_label = "Export File"

def execute(self, context):
#------------------------------------
#    my code is here....
#------------------------------------
return {'FINISHED'}

class BlenderCamPanel(bpy.types.Panel):
"""Creates a Panel in the Render context of the properties editor"""
bl_label = "BlenderCam"
bl_idname = "blendercam_panel"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "render"

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

def draw(self, context):
layout = self.layout
scene = context.scene

# --> new line here <--
rd = scene.my_tool

#label
layout.label(text="Label")
#chechbox
layout.prop(rd, "my_bool", text="Bool Property")
#button
row = layout.row()
row.operator("myops.blendercam_export_file")

def register():
# --> new line here <--
bpy.utils.register_module(__name__)
bpy.types.Scene.my_tool = PointerProperty(type=MySettings)

#bpy.utils.register_class(BlenderCam_exportFile)
#bpy.utils.register_class(BlenderCamPanel)
#bpy.utils.register_class(MySettings)

def unregister():
# --> new line here <--
bpy.utils.unregister_module(__name__)
del bpy.types.Scene.my_tool

#bpy.utils.unregister_class(BlenderCam_exportFile)
#bpy.utils.unregister_class(BlenderCamPanel)
#bpy.utils.unregister_class(MySettings)

if __name__ == "__main__":
register()


• Nice! It works now! However, i don't get why it works... what is the "rd" parameter? I mean, the scene.my_tool property, what is it and what is it used for? – Tareyes Dec 10 '18 at 10:29
• I update the answer with some explanation. hope it makes sense and hope I understood correctly ahah :) – Virgil Sisoe Dec 10 '18 at 18:38