Tuesday, October 2, 2012

Python Infrastructure in Blender 2.5 and above

I have started to study the Python infrastructure upon which Blender functions. To begin with, I added few lines of python code in some package initializer files under 2.63\scripts\
import os
type, package, init = __file__.split(os.path.sep)[-3:]
print ("loading package (type {0}) {1} \n\t {2}".format(type, package, __file__))

Upon starting Blender, following was the result (printed in the system console)
loading package (type modules)  bpy  
D:\BLENDER\RC\blender-2.64-RC2-windows64\2.63\scripts\modules\bpy\__init__.py
loading package (type startup)  bl_operators  
D:\BLENDER\RC\blender-2.64-RC2-windows64\2.63\scripts\startup\bl_operators\__init__.py 
loading package (type modules)  bpy_extras  
D:\BLENDER\RC\blender-2.64-RC2-windows64\2.63\scripts\modules\bpy_extras\__init__.py 
loading package (type startup)  bl_ui  
D:\BLENDER\RC\blender-2.64-RC2-windows64\2.63\scripts\startup\bl_ui\__init__.py
After the trivial experiment out of the way, I went ahead with doing something fun: prepending and appending  custom draw functions to all the registered panel types in Blender. The code below was added inside the register function defined in bl_ui startup package. I also tried to get my head around the layout engine.
    def drawBefore(self, context):
        layout = self.layout
        row = layout.row()
        box = row.box()
        box.label(text="The class of this panel is "+self.bl_rna.name, icon='CONSOLE') 
  def drawAfter(self, context):
        contextType = self.bl_context.capitalize()
        DOCS = 'http://www.blender.org/documentation/blender_python_api_2_63_17'
        URL = "{0}/bpy.types.{1}.html".format(DOCS, contextType)
        box = self.layout.split(percentage=1).box()
        if contextType:
            aStr = "Learn more about {0} context".format(contextType)
            box.label(text=aStr, icon='TRIA_DOWN')
            row = box.split(percentage=.4)
            row.operator("wm.url_open", text=contextType, icon="HELP").url = URL
        else:
            box.label(text="NO bl_context", icon="SPEAKER")
 
    module = 'bpy.types'
    for rType in dir(eval(module)):
        if rType.find('_PT_') == -1:
            continue
        panel = eval("{0}.{1}".format(module, rType))
        panel.prepend(drawBefore)
        panel.append(drawAfter)
And upon launching Blender, this is what I see..


Meanwhile, I found a glaring bug and reported on the bugtracker

No comments: