In order to navigate to an URL-address from Blender – to open the required address in the browser, we can use the system operator “wm.url_open”.
To demonstrate this let’s create a simple tab in the N-panel with a single button, by clicking on which the required page will be opened in the browser.
At first, let’s create a class to describe a simple tab in the 3D Viewport window N-panel and register it in the API.
1 2 3 4 5 6 7 8 9 10 11 12 |
from bpy.types import Panel from bpy.utils import register_class class TEST_PT_panel(Panel): bl_idname = 'TEST_PT_panel' bl_label = 'URL' bl_space_type = 'VIEW_3D' bl_region_type = 'UI' bl_category = 'URL' register_class(TEST_PT_panel) |
This class creates a panel named “URL” in the N-panel of the viewport window.
To add a button to it that calls the “wm.url_open” operator, let’s describe the call of the required operator in the “draw” function.
1 2 3 4 5 6 |
def draw(self, context): op = self.layout.operator( 'wm.url_open', text='Open URL', icon='URL' ) |
and pass a parameter with the required URL-address to it.
1 |
op.url = 'www.google.com' |
Full code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
from bpy.types import Panel from bpy.utils import register_class class TEST_PT_panel(Panel): bl_idname = 'TEST_PT_panel' bl_label = 'URL' bl_space_type = 'VIEW_3D' bl_region_type = 'UI' bl_category = 'URL' def draw(self, context): op = self.layout.operator( 'wm.url_open', text='Open URL', icon='URL' ) op.url = 'www.google.com' register_class(TEST_PT_panel) |
After executing this code, a new “URL” tab with the “Open URL” button will be created in the N-panel of the viewport. When you press it, the default browser will open the specified URL-address.