2
$\begingroup$

I am looking to place a button in it's own category inside the N panel on the UI. I read this as a reference: How to create a button in Custom panel to take you to the specified url

This operator is needed:
row.operator('wm.url_open', text='open url').url='http://www.blender.org'

I am following PANEL3 from this page example: (the panel in UI, VIEW): https://b3d.interplanety.org/en/creating-panels-for-placing-blender-add-ons-user-interface-ui/

My code looks like (taken from ui_panel_simple.py template).

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"
    bl_context = "object"

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

        obj = context.object

        row = layout.row()
        row.label(text="Hello world!", icon='WORLD_DATA')

        row = layout.row()
        row.label(text="Active object is: " + obj.name)
        row = layout.row()
        row.prop(obj, "name")

        row = layout.row()
        row.operator("mesh.primitive_cube_add")


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


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


if __name__ == "__main__":
    register()

But I get an error on:
def register(): bpy.utils.register_class(HelloWorldPanel)

I would like a button that I can click to a URL from the N panel in Blender 2.81.

$\endgroup$
2
$\begingroup$

Not sure what the issue is re registering... remove bl_context since it is not required outside 'PROPERTIES' area. Please in future paste the error message from the system console into your question.

Anyway here is a suggested edit. Please take note of the class names. and registration code. If another class that needs registering, eg panel of operator, simply pop in the class and add to classes tuple.

Adds a Quick Links panel to 3d view UI "N" panel under the category "Links".

import bpy

links = {"Blender" : 'http://www.blender.org',
         "BSE" : "https://blender.stackexchange.com",
         }


class VIEW3D_PT_links(bpy.types.Panel):
    """Quick Links"""
    bl_label = "Quick Links"
    bl_idname = "VIEW3D_PT_links"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_category = 'Links'


    def draw(self, context):
        layout = self.layout
        col = layout.column()
        for name, url in links.items():
            op = col.operator('wm.url_open', text=name)
            op.url = url

classes = (
        VIEW3D_PT_links,
        )

def register():
    for cls in classes:
        bpy.utils.register_class(cls)


def unregister():
    for cls in reversed(classes):
        bpy.utils.unregister_class(cls)


if __name__ == "__main__":
    register()

edit: added links dictionary to add more links simply.

$\endgroup$
2
  • $\begingroup$ Can't I give you some of the coins I have? This code is gold! Thank you sir! works perfectly! $\endgroup$ Jan 6 '20 at 10:36
  • $\begingroup$ What if these 2 buttons were to be included in a custom property of an object's panel? $\endgroup$ Jan 6 '20 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.