9

What is the best way to locate a function from the bpy module in Blender's source code? I've downloaded the source code and I'm aware you can also browse through it online.

As an example, I would like to inspect bpy.ops.render.render().

Garrett
  • 6,596
  • 6
  • 47
  • 75

2 Answers2

13

Get the idname like:

bpy.ops.render.render.idname()
# 'RENDER_OT_render'

(or simply take render.render and replace the . by _OT_)

Search that operator in the source code, and find it here:

blender\source\blender\editors\render\render_internal.c

/* contextual render, using current scene, view3d? */
void RENDER_OT_render(wmOperatorType *ot)
{
    PropertyRNA *prop;

    /* identifiers */
    ot->name = "Render";
    ot->description = "Render active scene";
    ot->idname = "RENDER_OT_render";

    /* api callbacks */
    ot->invoke = screen_render_invoke;
    ot->modal = screen_render_modal;
    ot->cancel = screen_render_cancel;
    ot->exec = screen_render_exec;

You'll likely wanna look for the exec function code (screen_render_exec) and see what it does.

CodeManX
  • 29,298
  • 3
  • 89
  • 128
  • How can I find classes that have no idname() like bpy.types.Scene in the code? – McLawrence Sep 05 '17 at 09:05
  • I would search the folder structure and by filename for those. Regarding the Scene type, check blenkernel, makesrna and makesdna: https://developer.blender.org/diffusion/B/browse/master/source/blender/blenkernel/BKE_scene.h / https://developer.blender.org/diffusion/B/browse/master/source/blender/makesdna/DNA_scene_types.h / https://developer.blender.org/diffusion/B/browse/master/source/blender/makesrna/intern/rna_scene.c / https://developer.blender.org/diffusion/B/browse/master/source/blender/makesrna/intern/rna_scene_api.c – CodeManX Sep 05 '17 at 09:42
  • Thanks, but I am searching for the python definition of the normal bpy.types.Scene() class – McLawrence Sep 05 '17 at 09:56
  • 1
    To my knowledge, it is only defined in the C code in rna_scene.c. Are you looking for the glue code that exposes the properties and methods to Python? – CodeManX Sep 05 '17 at 10:32
  • Yes, that's what I am looking for – McLawrence Sep 05 '17 at 10:35
  • 1
    I assume this is the most relevant part: intern/bpy.c:285 https://goo.gl/RUkrYc (shortened URL because SO breaks Phabricator deep links). The Scene type is not exposed explicitly anywhere as far as I can tell, because it is part of the DNA/RNA system and all types are exposed my generic code to make the properties and functions defined in the C code available in the bpy python interface. If you need more information, I suggest to contact the Blender developers via IRC. – CodeManX Sep 05 '17 at 11:03
1

Python offers a module for live inspection. If the function is a python function located in a .py file, you can use these functions.

For example,

import bpy
import inspect

#get sourcefile of a panel
inspect.getfile(bpy.types.VIEW3D_PT_transform_orientations)
#...blender-2.69-windows64\blender-2.69-windows64\2.69\scripts\startup\bl_ui\space_view3d.py

bpy.data.texts.new('source').write(
    inspect.getsource(bpy.types.VIEW3D_PT_transform_orientations)
    )

(for the difference between .getfile and .getsourcefile see this post on stackoverflow)

pink vertex
  • 9,896
  • 1
  • 25
  • 44
  • Why does inspect.getfile(bpy.ops.render.render) not work? I get a TypeError error saying it is not a module, class, method, function, traceback, frame, or code object. – Garrett Feb 09 '14 at 22:42
  • 1
    pink vertex's example shows inspect.getfile() using a bpy.types class. So the proper call for an operator is inspect.getfile(getattr(bpy.types, bpy.ops.render.render.idname())), which will raise a TypeError: <RNA_Types object at 0x0000000003A875A0> is a built-in class. You need search it in the C code (see my answer where to find it). – CodeManX Feb 09 '14 at 22:55