This is a follow up of How could a single Python script run when Blender is started?.
Simply adding a method to a newly created file in Blender\scripts\startup didn't work:
Warning! 'C:\Program Files\Blender Foundation\Blender\2.68\scripts\startup\global.py' has no register function, this is now a requirement for registerable scripts.
I used snippets from:
- Cannot-display-a-render-preview-of-a-scene-by-using-python-script-need-advices
- Adding a custom shortcut for sculpt mode
The script current looks like (Uses Ctrl-R to toggle between rendered and textured mode in Cycles) and adds a dump method I really need):
import bpy
class U(bpy.types.Operator):
bl_idname = "object.util"
bl_label = "Toggle Render Mode"
def execute(self, context):
# bpy.ops.sculpt.sculptmode_toggle()
for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
if area.spaces[0].viewport_shade == 'RENDERED':
area.spaces[0].viewport_shade = 'TEXTURED'
else:
area.spaces[0].viewport_shade = 'RENDERED'
return {'FINISHED'}
def dump( obj, ctx="", level=0):
for attr in dir(obj):
print( "%s.%s = %s" % (ctx, attr, getattr(obj, attr)))
def menu_func(self, context):
self.layout.operator(U.bl_idname)
addon_kmaps = []
def register():
bpy.utils.register_class(U)
wm = bpy.context.window_manager
km = wm.keyconfigs.addon.keymaps.new(name='Object Mode', space_type='EMPTY')
kmi = km.keymap_items.new(U.bl_idname, 'R', 'PRESS', ctrl=True, shift=False)
addon_kmaps.append(km)
def unregister():
bpy.utils.unregister_class(U)
bpy.types.VIEW3D_MT_object.remove(menu_func)
wm = bpy.context.window_manager
for km in addon_kmaps:
wm.keyconfigs.addon.kmaps.remove(km)
del addon_kmaps[:]
if __name__ == "__main__":
register()
The toggle button works, but how can the dump() method be invoked from the python console?
I noobishly tried U.dump(obj) and bpy.U.dump(obj)
EDIT:
I finally modified the existing scripts/modules/console_python.py and added (starts add line 100):
# weak! - but highly convenient
namespace["C"] = bpy.context
namespace["D"] = bpy.data
namespace['dump'] = bpy.types.OBJECT_OT_util.dump # my one

global.pysinceglobalis a Python keyword and it would make importing and working with this module from another script impossible. – ideasman42 Sep 07 '13 at 21:37