Blender插件之操作器(Operator)实战
前言
在Blender中, 操作器(Operator)是它的核心. 用户通过各种操作器来创建和操作场景中的物体.
操作器对象继承自 class bpy.types.Operator(bpy_struct)
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()
Operator.invoke用于在调用operator时从上下文初始化operator。
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)
Opens a file selector with an operator. The string properties ‘filepath’, ‘filename’, ‘directory’ and a ‘files’ collection are assigned
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')
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')
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')
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')
Blender插件之操作器(Operator)实战的更多相关文章
- Blender插件编写指南
前言 Blender插件是Blender的利器, 用户可以使用各种插件扩充Blender的功能. Blender Python插件以bpy.props, bpy.types.Operator, bpy ...
- Blender插件之Panel
目标 [x] 总结Blender之Panel 总结 Blender之Panel需要从Blender界面组成开始理解. 直观上Blender的界面层次为 Editors ‣ Regions ‣ (Tab ...
- Dynamics CRM2013 6.1.1.1143版本号插件注冊器的一个bug
近期在做的项目客户用的是CRM2013sp1版本号,所以插件注冊器使用的也是与之相应的6.1.1.1143,悲剧的事情也因此而開始. 在插件中注冊step时,工具里有个run in user's co ...
- MYSQL的Java操作器——JDBC
MYSQL的Java操作器--JDBC 在学习了Mysql之后,我们就要把Mysql和我们之前所学习的Java所结合起来 而JDBC就是这样一种工具:帮助我们使用Java语言来操作Mysql数据库 J ...
- WordPress Tweet Blender插件跨站脚本漏洞
漏洞名称: WordPress Tweet Blender插件跨站脚本漏洞 CNNVD编号: CNNVD-201310-645 发布时间: 2013-10-30 更新时间: 2013-10-30 危害 ...
- MVC框架的插件与拦截器基础
自制MVC框架的插件与拦截器基础 上篇谈到我自己写的MVC框架,接下来讲讲插件及拦截器! 在处理一些通用的逻辑最好把它封装一个插件或者拦截器,以便日后可以直接拿过来直接使用.在我的框架中可以通过继承以 ...
- mongoDB的shell数组操作器
http://www.2cto.com/database/201304/205024.html mongoDB数组操作器 $push会向数组末尾加入一个元素,如果数组不存在,则会创建这个数组. 增 ...
- 自制MVC框架的插件与拦截器基础
上篇谈到我自己写的MVC框架,接下来讲讲插件及拦截器! 在处理一些通用的逻辑最好把它封装一个插件或者拦截器,以便日后可以直接拿过来直接使用.在我的框架中可以通过继承以下抽象类来实现插件或者拦截器. 1 ...
- Blender插件加载研究
目标 [x] 解析Blender插件代码加载原理, 为测试做准备 结论 采用方法3的方式, 可以在测试中保证重新加载子模块, 是想要的方式, 代码如下: _qk_locals = locals() d ...
随机推荐
- openMSP430之io_test
openMSP430: IO functionality test with interupt #include "omsp_system.h" volatile char shi ...
- vue中eventbus 多次触发的问题
相比于react,vue是一个更上手很快的框架.所说简单易用,但在做Vue项目的过程中,就会发现,坑还是有的.组件之间传递数据是一个常见的需求,父子组件传递数据在官网有介绍,除了父子组件,一般地,任意 ...
- kvm之 virt-install工具命令详解
一.virt-install是一个命令行工具,它能够为KVM.Xen或其它支持libvrit API的hypervisor创建虚拟机并完成GuestOS安装:此外,它能够基于串行控制台.VNC或SDL ...
- Hadoop 使用小命令(2)
一.查看一堆文件共有多少行 查看file1/file2目录下所有文件总共多少行 hadoop fs -text file1/file2/* | wc -l 二.正则表达式 hadoop fs -tex ...
- html第九节课
正则表达式和marquee 1.表单验证<form></form> (1).非空验证(去空格) (2).对比验证(跟一个值对比) (3).范围验证(根据一个范围进行判断) (4 ...
- vue: This relative module was not found
这是今天运行vue项目报的一个错误,特地在此记录一下. 错误信息如下: ERROR Failed to compile with 1 errors This relative module was n ...
- CentOS7.2安装nginx失败
错误提示: Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyu ...
- BZOJ1001 BJOI2006 狼抓兔子
Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个 ...
- Vue主要原理最简实现与逻辑梳理
Vue的主要原理中主要用到了定义的这么几个函数Dep,Watcher,observer.我们来使用这几个函数简单的实现一下vue构造函数数据绑定和相互依赖部分,梳理一下它们之间的关系.省略了编译部分和 ...
- 突如其来的“中断异常”,我(Java)该如何处理?
3.try-catch块 try语句块中代码执行时发生三种情况: try语句块中代码正常执行完毕,没有任何异常,那么catch语句块的代码将不会被执行. import java.util.*; pub ...