If you are only going to re-executing this script when 3D View is open you might add the shortcut to 3D View (global). I propose a different shortcut as alt+p is for showing the 'clear parent' menu, but it's up to you.
At the end of this list..
# something to test with..
x = random.randint(-6, 6)
y = random.randint(-6, 6)
z = random.randint(-6, 6)
bpy.ops.mesh.primitive_cube_add(radius=1, location=(x, y, z))
bl_idname = "view3d.global_script_runner"
bl_label = "Global Script Runner"
#def poll(cls, context):
# return context.active_object is not None
def execute(self, context):
print('i get called')
textblock_name = 'generic_executor.py'
textblock_as_string = bpy.data.texts[textblock_name].as_string()
if __name__ == "__main__":
To wrap this into a reusable add-on requires more time than I have, but it's not the purpose of this answer. The benefit of having this as an addon would be that you could create a panel for 3dview in which you can type or select a textblock to be executed when the shortcut is triggered, rather than this hard coded suggestion, where it only executes a textblock named
Here's something you could use as a starting point, it creates a panel in 3dview (right side panel) and allows you to pick a textblock. Whenever you trigger the operator
view3d.global_script_runner , it would look at the name of the selected file and execute it.