Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

January 17, 2015

Blender Console Prompt

Blender Console addon

Coming from applications that have a dedicated console/prompt for their user input I always felt that the Blender Interactive Python console was one abstraction away from what I want in a console. Yes it's great that it executes python, i totally love REPL, but what about shortcuts+switches? What about actual interaction where the prompt suggests options to continue a command.

Two types of commands


1) Parsed commands
2) Modal Interactive commands

Let's say I can't quite remember how to add a plane from python, or don't want to type:
bpy.ops.mesh.primitive_plane_add()
What if i maybe want to define my own syntax for adding objects, and use the Console more as a prompt than a Python interpreter. Pressing Enter will execute console.execute (ie, the regular python). Pressing Ctrl+Enter can send the current line to console.do_action ( a new operator for this addon). console.do_action can interpret / parse the line and do a number of predefined operations.

Maybe I want to be able to write
+plane|0 1 2|layer=2
# gets parsed into
# bpy.ops.mesh.primitive_plane_add(location=(0,1,2), layer=(False, True, False......)) 

+plane|3d|layer=1
# gets parsed into
# bpy.ops.mesh.primitive_plane_add(location=(current-3dcursor-location), layer=(True, False......)) 

Perhaps these are crude, non interactive, examples. Maybe i'd like a running modal operator to suggest a list of parameters to pick for the next element of my command.
+plane
# p=location (space separated, 3 floats)
# l=layer (single integer)
# r=radius
# enter  # to finalize

+circle # (or ci, or circ)
# p=location (space separated, 3 floats)
# l=layer (single integer)
# r=radius
# fill
# enter  # to finalize
Some golden oldies would be easy to add.
vtx # would just perform automatic intersection/extend/project from the tinyCAD addon
v2x # would place a vertex at the intersection
xall # would intersect all selected geometry
bix # add bisector edge in proximity of selected edges

# copy the current console line to the clipboard, without using a mouse. 
# exclamation at the end copies the preceding text to the clipboard.
some.superlong.line.that.i.wanted.to.copy.to.clipboard!
# pressing ctrl+enter would copy that line, without needing to highlight
This would abstract the python commands into something that I feel i can remember, or be happy about wanting to maintain.

Addon?!

yes, addon! https://github.com/zeffii/BlenderConsolePrompt Essentially, the addon core could be as simple as:
class ConsoleDoAction(bpy.types.Operator):
    bl_label = "ConsoleDoAction"
    bl_idname = "console.do_action"

    def execute(self, context):
        m = bpy.context.space_data.history[-1].body
        m = m.strip()
        if m == "cen":
            '''cursor to center'''
            context.scene.cursor_location = (0.0, 0.0, 0.0)

        elif m.endswith('!'):
            '''copy current line to clipboard'''
            m = m[:-1]
            context.window_manager.clipboard = m
            print('copied: "{0}"'.format(m))

        elif m == 'vtx':
            if hasattr(bpy.ops.view3d, 'autovtx'):
                bpy.ops.view3d.autovtx()

        return {'FINISHED'}

I haven't tried to implement the modal interactive bits yet, but that's the next step.

No comments:

Post a Comment

Please use Blender.StackExchange.com for python scripting questions unrelated to this post.