hhr
(hhr)
1
I want to clear system console.
So i did execute a below command.
But i receive error message such as below.
RuntimeError: Operator bpy.ops.console.clear.poll() failed, context is incorrect
Why not excute this command “bpy.ops.console.clear(scrollback=True, history=False)”
My Script
--------------------i
import bpy, random
bpy.ops.console.clear(scrollback=True, history=False)
print(“rand:” , int(round(random.random() * 5)))
Please answer to me.
Thank you.
first up its import not mport, next up the bpy.ops.console.clear should be on the next line…
hhr
(hhr)
3
Oh, It’s my mistake.
I did fix a wrong code.
Please answer to me again.
the console is in a different window, so you are unable to use the bpy.ops.console command.
CoDEmanX
(CoDEmanX)
5
you need a context override
http://www.blender.org/documentation/blender_python_api_2_70a_release/bpy.ops.html#overriding-context
import bpy
from bpy.props import IntProperty, FloatProperty
def insert_newlines(string, every=200):
return '
'.join(string[i:i+every] for i in range(0, len(string), every))
class ModalOperator(bpy.types.Operator):
"""Move an object with the mouse, example"""
bl_idname = "object.modal_operator"
bl_label = "Simple Modal Operator"
first_mouse_x = IntProperty()
first_value = FloatProperty()
prompt = "Name = "
def modal(self, context, event):
#print(event.type)
ctx = context.copy()
ctx['area'] = self.area
history = self.area.spaces[0].history[-1]
if event.type in ('LEFTMOUSE', 'RET', 'NUMPAD_ENTER'):
self.area.spaces.active.prompt = ">>> "
self.area.spaces.active.scrollback[-1].body = self.prompt + history.body
#self.report({'ERROR'}, "You typed:
" + insert_newlines(history.body))
bpy.ops.console.clear_line(ctx)
return {'FINISHED'}
elif event.value == 'PRESS':
if event.type == 'BACK_SPACE':
if event.ctrl:
bpy.ops.console.delete(ctx, type="PREVIOUS_WORD")
else:
bpy.ops.console.delete(ctx, type="PREVIOUS_CHARACTER")
elif event.type == 'DEL':
if event.ctrl:
bpy.ops.console.delete(ctx, type="NEXT_WORD")
else:
bpy.ops.console.delete(ctx, type="NEXT_CHARACTER")
elif event.type == 'LEFT_ARROW':
if event.ctrl:
bpy.ops.console.move(ctx, type="PREVIOUS_WORD")
else:
bpy.ops.console.move(ctx, type="PREVIOUS_CHARACTER")
elif event.type == 'RIGHT_ARROW':
if event.ctrl:
bpy.ops.console.move(ctx, type="NEXT_WORD")
else:
bpy.ops.console.move(ctx, type="NEXT_CHARACTER")
elif event.type == 'HOME':
bpy.ops.console.move(ctx, type="LINE_BEGIN")
elif event.type == 'END':
bpy.ops.console.move(ctx, type="LINE_END")
elif event.unicode:
bpy.ops.console.insert(ctx, text=event.unicode)
#self.area.spaces[0].history[-1].body += event.unicode
#self.area.spaces[0].history[-1].current_character += 1
return {'RUNNING_MODAL'}
def invoke(self, context, event):
for area in bpy.context.screen.areas:
if area.type == 'CONSOLE':
break
else:
self.report({'WARNING'}, "No PyConsole on screen")
return {'CANCELLED'}
self.area = area
ctx = context.copy()
ctx['area'] = area
bpy.ops.console.clear_line(ctx)
region = area.regions[-1]
context.window.cursor_warp(region.x + 120, region.y + 12)
area.spaces.active.prompt = self.prompt
bpy.ops.console.scrollback_append(ctx, text="Please type your text:")
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
def register():
bpy.utils.register_class(ModalOperator)
def unregister():
bpy.utils.unregister_class(ModalOperator)
if __name__ == "__main__":
register()
# test call
bpy.ops.object.modal_operator('INVOKE_DEFAULT')