1
$\begingroup$

I am trying to find the following classes in the source code:

  • SCREEN_OT_screenshot
  • EXPORT_MESH_OT_stl

However, I have no idea where to find them and it would be painful to search in every .py of Blender. Is there a way to find a class (especially those two listed above) from console or from the documentation?

$\endgroup$

1 Answer 1

3
$\begingroup$

It depends whether the operator is from an add-on or implemented in Blender itself. The former will be from a Python script, while the latter is implemented in C or C++ and is accessible through the Python API.

For operators from Python add-ons it's necessary to know the command for the operator, therefore enable the Developer Extras in Edit > Preferences > Interface > Display. Once enabled, you can right click on buttons and choose Copy Python Command from the context menu.

Developer Extras

Copy Python Command

When the command is known, the inspect module can be used to find the source file and the source code. Replace bpy.ops.export_mesh.stl with the operator you would like to inspect.

import bpy
import inspect


cls = getattr(bpy.types, bpy.ops.export_mesh.stl.idname())
print(inspect.getfile(cls))
print(inspect.getsource(cls))

Alternatively, if the class name is already known, it can be used directly.

import bpy
import inspect


cls = getattr(bpy.types, 'EXPORT_MESH_OT_stl')
print(inspect.getfile(cls))
print(inspect.getsource(cls))

The source code of operators, that are implemented in C or C++, can be found by opening an IDE (e.g. Visual Studio Code) or advanced text editor of your choice (e.g. Notepad++) and searching the cloned repository of Blender for the definition of the class name. This approach works for any programming language, including Python scripts.

Alternatively you can use the:

  • Diffusion for Blender or Blender Add-ons and search the class name through the Pattern Search
  • GitHub mirror and search the repository using repo:blender/blender followed by the class name.

The SCREEN_OT_screenshot class can be found in /source/blender/editors/screen/screendump.c. The EXPORT_MESH_OT_stl class, which is actually named ExportSTL, can be found in /release/scripts/addons/io_mesh_stl/__init__.py.

$\endgroup$
1
  • $\begingroup$ @UneconscienceUneSource If you want access to the class you need the idname(), hence the correct function call would be cls = getattr(bpy.types, bpy.ops.mesh.subdivide.idname()). However, this also won't work as the operator is not implemented in Python. You will get an error that looks like AttributeError: 'RNA_Types' object has no attribute 'MESH_OT_subdivide' which means you will have to look at Blender's source code. $\endgroup$
    – Robert Gützkow
    Jul 22, 2021 at 9:26

You must log in to answer this question.

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