0

I have a simple panel that you can see in the viewport which is a shorter version of UI Panel Simple. Seeing from other panels, by using Edit Source, they all define a context where the property is added, but how do I decide which one to use to get it working? Using Blender 2.92.

import bpy
from bpy.props import BoolProperty

class HelloWorldPanel(bpy.types.Panel): bl_label = "Hello World Panel" bl_idname = "OBJECT_PT_hello" bl_space_type = 'VIEW_3D' bl_region_type = 'UI' bl_category = "Tool"

def draw(self, context):
    layout = self.layout
    row = layout.row()
    row.label(text="Hello world!", icon='WORLD_DATA')


class QuickRename_PT_Panel(bpy.types.Panel): bl_space_type = 'VIEW_3D' bl_region_type = 'UI' bl_label = "Quick Rename" bl_category = "Tool" bl_parent_id = "OBJECT_PT_hello"

testBool: BoolProperty(
    name = 'Test',
    description = 'Test description',
    default = True,)


def draw(self, context):
    layout = self.layout
    layout.use_property_split = True
    layout.use_property_decorate = False

    tool_settings = context.tool_settings

    row = layout.row()
    row.prop(tool_settings, 'testBool')



def register(): bpy.utils.register_class(HelloWorldPanel) bpy.utils.register_class(QuickRename_PT_Panel)

def unregister(): bpy.utils.unregister_class(QuickRename_PT_Panel) bpy.utils.unregister_class(HelloWorldPanel)

if name == "main": register()

I want to add the testBool property to the Hello World Panel. enter image description here

But I think the

tool_settings = context.tool_settings

is wrong.

Lala_Ghost
  • 457
  • 4
  • 17
  • 2
    We do not declare properties in panel classes. Either use a property group or register your property directly to the scene: https://blender.stackexchange.com/questions/57306/how-to-create-a-custom-ui – brockmann Apr 09 '21 at 14:12
  • 1
    or AFAIK also not on any non ID types (those in bpy.data collections like Objects, and bones) eg bpy.types.ToolSettings.test_bool = bpy.props.BoolProperty() then in python console type in C.tool_settings.test_bool If it is not True or False ' can't do it. – batFINGER Apr 12 '21 at 06:16
  • 1
    Yes, thats why I added the answer with the link to How to create a custom UI? and working code – Lala_Ghost Apr 12 '21 at 07:03

1 Answers1

0

Okay by looking at How to create a custom UI? I could figure it out

import bpy
from bpy.props import BoolProperty, PointerProperty
from bpy.types import PropertyGroup

class HelloWorldPanel(bpy.types.Panel): bl_label = "Hello World Panel" bl_idname = "OBJECT_PT_hello" bl_space_type = 'VIEW_3D' bl_region_type = 'UI' bl_category = 'Tool'

def draw(self, context):
    layout = self.layout
    row = layout.row()
    row.label(text="Hello world!", icon='WORLD_DATA')


class QuickRenameProperties(PropertyGroup): test_bool: BoolProperty( name = 'Test', description = 'Test description', default = True)

class QuickRename_PT_Panel(bpy.types.Panel): bl_space_type = 'VIEW_3D' bl_region_type = 'UI' bl_label = "Quick Rename" bl_category = "Tool" bl_parent_id = "OBJECT_PT_hello"

def draw(self, context):
    layout = self.layout
    layout.use_property_split = True
    layout.use_property_decorate = False
    scene = context.scene
    mytool = scene.my_tool

    row = layout.row()
    row.prop(mytool, 'test_bool')


def register(): bpy.utils.register_class(HelloWorldPanel) bpy.utils.register_class(QuickRename_PT_Panel) bpy.utils.register_class(QuickRenameProperties) bpy.types.Scene.my_tool = PointerProperty(type = QuickRenameProperties)

def unregister(): bpy.utils.unregister_class(QuickRename_PT_Panel) bpy.utils.unregister_class(HelloWorldPanel) del bpy.types.Scene.my_tool

if name == "main": register()

brockmann
  • 12,613
  • 4
  • 50
  • 93
Lala_Ghost
  • 457
  • 4
  • 17