I'm trying to change the prompt string to the left of the Python Console.

Setting bpy.context.space_data.prompt, either directly or via an operator, will change it only in the scrollback for that in which one did so, after which it reverts to the default ">>> ". Perhaps bpy.ops.console.execute() is resetting it.

Is there a way to make it stick?


I made it work by wrapping bpy.console.execute(), then setting it afterward:

import bpy
class CONSOLE_OT_execute_wrapper(bpy.types.Operator):
    """Wraps command execution to maintain the current prompt."""
    bl_label="Execute Wrapper"
    interactive=bpy.props.BoolProperty(name="Interactive",description="Whatever the \"interactive\" property does for bpy.ops.console.execute.",default=False)
    prompt=bpy.props.StringProperty(name="Prompt",description="String that shows to the left of the current command.")
    def poll(cls,C):
        return C.area.type=="CONSOLE"
    def execute(self,C):
        return {'FINISHED'}
def register():
    for kmi in [kmi for kc in bpy.context.window_manager.keyconfigs for km in kc.keymaps for kmi in km.keymap_items if kmi.idname=="console.execute"]:kmi.idname+="_wrapper"#Wrap
def unregister():
    for kmi in [kmi for kc in bpy.context.window_manager.keyconfigs for km in kc.keymaps for kmi in km.keymap_items if kmi.idname=="console.execute_wrapper"]:kmi.idname=kmi.idname[:kmi.idname.rindex("_")]#Unwrap
if __name__=="__main__":register()

After running the above in the text editor, for now you can then set the prompt in User Preferences->Input, where you expand "Console", find each "Execute Wrapper" and set their "prompt" property. I'll put the option somewhere appropriate in the addon.

  • 2
    $\begingroup$ Are you able to expand your answer by showing how you did that? $\endgroup$ Mar 29 '17 at 15:30
  • $\begingroup$ Python by itself uses sys.ps1 and sys.ps2 for the interactive prompts; might want to check if blenderpython respects that. docs.python.org/3/library/sys.html?highlight=sys.ps1#sys.ps1 $\endgroup$
    – StarWeaver
    Mar 30 '17 at 6:38
  • $\begingroup$ Added. It's part of a console shellification module I'll soon upload. $\endgroup$
    – Blitzen
    Apr 1 '17 at 1:27
  • $\begingroup$ @StarWeaver: Apparently not: AttributeError: module 'sys' has no attribute 'ps1' Good thinking, in any case. $\endgroup$
    – Blitzen
    Apr 3 '17 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.