前言

在Blender中, 操作器(Operator)是它的核心. 用户通过各种操作器来创建和操作场景中的物体.

操作器对象继承自 class bpy.types.Operator(bpy_struct)

下面通过代码实例来学习它, 以下代码来源于https://docs.blender.org/api/2.79/bpy.types.Operator.html
#### 1 最简单的Operator
简单的信息打印
import bpy

class HelloWorldOperator(bpy.types.Operator):
bl_idname = "yy.hello_world"
bl_label = "Minimal Operator" def execute(self, context):
print("Hello World!")
return {'FINISHED'} bpy.utils.register_class(HelloWorldOperator) # test call to the newly defined operator
bpy.ops.yy.hello_world()
#### 2 Invoke

Operator.invoke用于在调用operator时从上下文初始化operator。

Operator.invoke is used to initialize the operator from the context at the moment the operator is called.

import bpy

class SimpleMouseOperator(bpy.types.Operator):
""" This operator shows the mouse location,
this string is used for the tooltip and API docs
"""
bl_idname = "wm.mouse_position"
bl_label = "Invoke Mouse Operator" x = bpy.props.IntProperty()
y = bpy.props.IntProperty() def execute(self, context):
# rather than printing, use the report function,
# this way the message appears in the header,
self.report({'INFO'}, "Mouse coords are %d %d" % (self.x, self.y))
return {'FINISHED'} def invoke(self, context, event):
self.x = event.mouse_x
self.y = event.mouse_y
return self.execute(context) bpy.utils.register_class(SimpleMouseOperator) # Test call to the newly defined operator.
# Here we call the operator and invoke it, meaning that the settings are taken
# from the mouse.
bpy.ops.wm.mouse_position('INVOKE_DEFAULT') # Another test call, this time call execute() directly with pre-defined settings.
bpy.ops.wm.mouse_position('EXEC_DEFAULT', x=20, y=66)
#### 3 调用文件选择器

Opens a file selector with an operator. The string properties ‘filepath’, ‘filename’, ‘directory’ and a ‘files’ collection are assigned

when present in the operator Notice the invoke function calls a window manager method and returns {'RUNNING_MODAL'},
this means the file selector stays open and the operator does not exit immediately after invoke finishes.

import bpy

class ExportSomeData(bpy.types.Operator):
"""Test exporter which just writes hello world"""
bl_idname = "export.some_data"
bl_label = "Export Some Data" filepath = bpy.props.StringProperty(subtype="FILE_PATH") @classmethod
def poll(cls, context):
return context.object is not None def execute(self, context):
file = open(self.filepath, 'r')
print(file.read())
file.close()
#file.write("Hello World " + context.object.name)
return {'FINISHED'} def invoke(self, context, event):
context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'} # Only needed if you want to add into a dynamic menu
def menu_func(self, context):
self.layout.operator_context = 'INVOKE_DEFAULT'
self.layout.operator(ExportSomeData.bl_idname, text="Text Export Operator") # Register and add to the file selector
bpy.utils.register_class(ExportSomeData)
bpy.types.INFO_MT_file_export.append(menu_func) # test call
bpy.ops.export.some_data('INVOKE_DEFAULT')
#### 4 对话框
import bpy

class DialogOperator(bpy.types.Operator):
bl_idname = "object.dialog_operator"
bl_label = "Simple Dialog Operator" my_float = bpy.props.FloatProperty(name="Some Floating Point")
my_bool = bpy.props.BoolProperty(name="Toggle Option")
my_string = bpy.props.StringProperty(name="String Value") def execute(self, context):
message = "Popup Values: %f, %d, '%s'" % \
(self.my_float, self.my_bool, self.my_string)
self.report({'INFO'}, message)
return {'FINISHED'} def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_props_dialog(self) bpy.utils.register_class(DialogOperator) # test call
bpy.ops.object.dialog_operator('INVOKE_DEFAULT')
#### 5 自定义绘图

By default operator properties use an automatic user interface layout. If you need more control you can create your own layout with a Operator.draw function.

import bpy

class CustomDrawOperator(bpy.types.Operator):
bl_idname = "object.custom_draw"
bl_label = "Simple Modal Operator" filepath = bpy.props.StringProperty(subtype="FILE_PATH") my_float = bpy.props.FloatProperty(name="Float")
my_bool = bpy.props.BoolProperty(name="Toggle Option")
my_string = bpy.props.StringProperty(name="String Value") def execute(self, context):
print("Test", self)
return {'FINISHED'} def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_props_dialog(self) def draw(self, context):
layout = self.layout
col = layout.column()
col.label(text="Custom Interface!") row = col.row()
row.prop(self, "my_float")
row.prop(self, "my_bool") col.prop(self, "my_string") bpy.utils.register_class(CustomDrawOperator) # test call
bpy.ops.object.custom_draw('INVOKE_DEFAULT')
#### 6 Modal执行

This operator defines a Operator.modal function that will keep being run to handle events until it returns {'FINISHED'} or {'CANCELLED'}.

import bpy

class ModalOperator(bpy.types.Operator):
bl_idname = "object.modal_operator"
bl_label = "Simple Modal Operator" def __init__(self):
print("Start") def __del__(self):
print("End") def execute(self, context):
context.object.location.x = self.value / 100.0
return {'FINISHED'} def modal(self, context, event):
if event.type == 'MOUSEMOVE': # Apply
self.value = event.mouse_x
self.execute(context)
elif event.type == 'LEFTMOUSE': # Confirm
return {'FINISHED'}
elif event.type in {'RIGHTMOUSE', 'ESC'}: # Cancel
context.object.location.x = self.init_loc_x
return {'CANCELLED'} return {'RUNNING_MODAL'} def invoke(self, context, event):
self.init_loc_x = context.object.location.x
self.value = event.mouse_x
self.execute(context) context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'} bpy.utils.register_class(ModalOperator) # test call
bpy.ops.object.modal_operator('INVOKE_DEFAULT')

最新文章

  1. 【JAVA并发编程实战】11、有界缓存的实现
  2. 搭建高可用mongodb集群(四)—— 分片
  3. Asp.Net Web API 2第十课——使用OWIN自承载Web API
  4. FFmpeg 官方 20160227 之后 追加 libmfx 无法在 xp 上运行的解决方法
  5. ubuntu1404_server搭建lamp
  6. openflashchart + flex
  7. 08_rlCoachKin自主编译,调试
  8. Xmind 快捷键
  9. spring与hibernate整合事务管理的理解
  10. html 框架 2017-03-11
  11. 使用exceljs时报错:no such file or directory
  12. 【网摘】C#.NET 在 MVC 中动态绑定下拉菜单的方法
  13. Linux使用wget、安装Python
  14. guxh的python笔记十一:异常处理
  15. 【365】拉格朗日乘子法与KKT条件说明
  16. [leetcode]449. Serialize and Deserialize BST序列化与反序列化BST
  17. 通过键盘控制改变物体transform值
  18. 将jar包添加到maven仓库
  19. 【微信小程序】使用setTimeout制作定时器的思路
  20. learning uboot auto switch to stanbdy system in qca4531 cpu

热门文章

  1. hibernate_04_hbm.xml介绍
  2. win7 64位装sql2000
  3. sql server 查询数据判断为空
  4. 51nod1085 背包问题【动态规划】
  5. [NOIP模拟赛]b
  6. 使用tomcat搭建Jenkins环境(centos7.3)
  7. 嵌入式linux实现NAT端口映射
  8. Spring Cloud-Ribbon ILoadBalancer负载均衡器核心源码(四)
  9. python进行excel操作
  10. CAN在汽车电子中的应用