2
$\begingroup$

I'm trying to add "Normals" section in custom panel

enter image description here

import bpy


class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

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

        overlay = bpy.context.space_data.overlay

        row = layout.row()
        row.prop(overlay, "normals_length")

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


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


if __name__ == "__main__":
    register()

Please help me! What am I doing wrong?

$\endgroup$

2 Answers 2

4
$\begingroup$

I think the problem is trying to add settings for the 3D view outside the 3D view. You can place it in the Sidebar (Properties Panel N).

import bpy

class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'

    def draw(self, context):
        layout = self.layout
        overlay = context.space_data.overlay

        row = layout.row()
        row.prop(overlay, "normals_length")

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


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


if __name__ == "__main__":
    register()
$\endgroup$
0
2
$\begingroup$

This is how you can access this property from anywhere in the UI, provided you have an open 3D viewport editor in the same screen. The bpy.types.View3DOverlay object is a member of bpy.types.SpaceView3D.

You can access the property using this method :

https://blender.stackexchange.com/a/162466/86891

In an actual script :

import bpy


class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Scene properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

    def draw(self, context):
        area = next(a for a in context.screen.areas if a.type == "VIEW_3D", None)
        if area is None:
            return
        space = area.spaces.active
        overlay = space.overlay
        self.layout.prop(overlay, "normals_length")

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


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


if __name__ == "__main__":
    register()
$\endgroup$

You must log in to answer this question.

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