1
$\begingroup$

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,
                       AddonPreferences,
                       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?

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – Ratt
    Commented Dec 9, 2018 at 20:27

1 Answer 1

2
$\begingroup$

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
    rna_uiItemR: property not found: RenderSettings.my_bool
    /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,
                       AddonPreferences,
                       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()

enter image description here

$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$
    – Tareyes
    Commented Dec 10, 2018 at 10:29
  • $\begingroup$ I update the answer with some explanation. hope it makes sense and hope I understood correctly ahah :) $\endgroup$ Commented Dec 10, 2018 at 18:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .