5
$\begingroup$

When you press a button or change a setting, this is an operation which you can also do in Python, how do you find the Python equivalent to accessing these user interface elements.

  • Settings (text, numbers, colors... etc)
  • Buttons (tools you access via key bindings, menu items etc)
  • Menus (accessed via headers, other menus or key-bindings)
$\endgroup$
  • 1
    $\begingroup$ Enable Python Tooltips, and hover over the setting/button/thing and there will be a data path which points to the function; You can also press Ctrl + C to copy it. $\endgroup$ – someonewithpc Nov 11 '14 at 18:56
  • $\begingroup$ Ya, you posted an hour ago. @someonewithpc, Didn't think about that. Answer removed $\endgroup$ – BlendingJake Nov 11 '14 at 20:15
  • $\begingroup$ @BlendingJake Sorry for the inconvenience.... $\endgroup$ – someonewithpc Nov 11 '14 at 20:32
7
$\begingroup$

Edit Source

If you wanted to find out the python code for some of the user interface and panels you could right-click on parts of the UI (such as buttons or properties within the panel you wanted to know about) and choose 'Edit Source'.

For example, if I wanted to know about the render panel:

enter image description here

I could right-click on the 'Render' button and choose 'Edit Source':

enter image description here

A notification will then appear in the Info header at the top of the screen (in a default layout) informing you that a text file had been created for viewing in the text editor:

enter image description here

You can check this file and view the python code that makes up the panel:

enter image description here

In this example you can see information which might be useful such as: The panel class name which can be used to append new UI elements into existing parts of the UI (RENDER_PT_render), the operator names and categories (render.render for example) and general UI code for creating rows (row = layout.row(align=True)).

Autocomplete

Sometimes, if you are searching for the python equivalent of a property, it might be useful to use the autocomplete feature (CTRL+Space) of the 'Python Console' to explore the api.

Autocomplete will show you all the possible options branching off the current one. For example, using autocomplete on bpy.data.objects['Cube'] would result in showing me all the available python properties for that object:

enter image description here

Online API documentation

You can also browse the online 'Python API Reference' (that link always redirects to the latest version) which can be found via the help menu:

enter image description here

This documentation includes several pages serving as an introduction to the API as well as a full reference to all the python commands and properties available to use in blender. While this doesn't provide the python command to an exact tool/panel instantly, a simple generic search of "panel" using the search box would provide results relating to existing panels in blender and examples of how to create a new one.

Hotkey Preferences

If you wanted to find out the python name of a menu that appears when you press a hotkey, you can search for that hotkey in the preferences. For example, if I wanted to know the python name of the menu that appears when I press Ctrl+H in edit mode (the hook menu), I can search using the shortcut (when search type is 'key binding') in File> User Preferences> Input:

enter image description here

This provides several results, one of which is a 'Call Menu' item in the 'Mesh' category which reveals the name of the menu being called:

enter image description here

Here we can see the menu being called with that shortcut is VIEW3D_MT_hook, which you can then call in your own scripts.

$\endgroup$
6
$\begingroup$

One way to get a whole lot of info about what is going on behind the scenes when it comes to Pyhon is to open the sneaky window hidden up at the top of the main Blender window. Click and drag the top header bar down to reveal it.

enter image description here

Upon adjusting the Lamp Strength in the Node Editor, an entry in the top window will appear that shows the Python command with the value entered which matches the value from the node which was recently adjusted as shown in the next image.

enter image description here

$\endgroup$
6
$\begingroup$

You can use Python Tooltips.

To enable them, go to the 3D Header, press file and the User Preferences; You can also use the shortcut Ctrl + Alt + U. Then go to the Interface tab, and check Python Tooltips.

Now when you hover your mouse over any button/field, it will show you, in blue text, the datapath to the function/field associated with that button/field. For example, the Render button's tooltip reads Python: bpy.ops.render.render(), so it's datapath is bpy.ops.render.render()

To copy this automagically, you can press Ctrl + C, or RMB and select Copy Data Path.

Python Tooltips

$\endgroup$
1
$\begingroup$

Others have given a partial answer, the full answer is usually a combination of each method. Unfortunately finding the python equivalent is not a one method answer.

Python tooltips are helpful but can be truncated so you may need to find extra information or be familiar with what is cut out. From 2.72 you don't need to keep tooltips enabled, hold down Alt and move the mouse over an item to see the tooltip when they are turned off. This is an area that the auto complete in the python console can be helpful. Sometimes you may need to search through the api docs to find the truncated sections.

Many properties are easily visible in tooltips and right click->copy data path will give you the most unique part of data path, you then add that to current object or material nodes etc. There is also an API navigator addon that can be useful to find data paths.

Buttons are used to run an operator and most will show up in the info window once you expand it, a right click->Edit source is usually a good second option. There are areas that edit source fails, such as most of the properties sidebars (for non-addon panel items).

For menus, they are a collection of operators and python tooltips are normally sufficient. You can't edit source for each menu item but as long as you haven't collapsed the menus you can edit source for the menu in the header and follow the list to find the one you want.

$\endgroup$

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.