插件信息声明
在最开始写上
bl_info = {
"name": "rrBrush",
"description": "",
"author": "",
"version": (1, 0, 0),
"blender": (2, 80, 0),
"location": "View3D", # 在哪界面可以找到插件
"doc_url":"",
"tracker_url": "https://",
"category": "View 3D" ,#插件分类
"warning":"",
}
类/执行打包 定义
新建一个类,类型是Operator
类名的命名方式一般是 大写所属类_大写类型缩写_小写名称
配置
基本
Tooltip
提示性语言bl_idname
是这个类所属的分类,可以是已有的也可以如图自己写一个- 假如这里是mesh.monkey_grid,那么调用方法就是
bpy.ops.mesh.monkey_grid()
,看上去是一个类,实际上就像是一个函数。
- 假如这里是mesh.monkey_grid,那么调用方法就是
bl_label
是这个类的显示名称bl_options
可选参数,比如 = {‘REGISTER’,’UNDO’}- REGISTER 启动默认注册此py
- UNDO 在undo面板调节参数
Panel UI
类的类型要选择bpy.types.Panel
,也可以是其他的UI Type
https://docs.blender.org/api/current/bpy.types.Panel.html
Panel 类型下,需要一个bl_space_type = 'VIEW_3D' #所属页面 bl_region_type = 'UI' #所属区域 # bl_context = "objectmode" bl_category = "Kitfox - Normal" #panel卡片名称 bl_label = "Normal Brush" #标签名称
def draw(self,content)
这是窗口绘制函数,在这里自动排列绘制GUI
如果什么都不干,就写个pass
如果要调用一个operator作为按钮
self.layout.operator('输入所需要的idname',
text = '重新修改显示名称',
icon = 'icon名称')
在addon中打开 iconview就可以在python命令窗口查看icon
并且这个operator是可以传递构造函数的
编组
把原先的self.layout
替换为self.layout.column
可以编组
prop函数,用来获取某个分类下的class
label函数,用来创建一条文本
self.layout.label(text = 'this is a lablel')
变量
还可以为变量设置
- min
- max
- soft_max
值类型(显示名称,描述解释,默认值)
在函数中使用的时候用
- self.count_x
- self.count_y
- self.size
- …
调用传参的时候也是写,例子
bpy.ops.mesh.monkey_grid(count_x = 3,count_y = 2,size = 0.5)
bpy.ops.mesh.monkey_grid() #使用默认值
函数
execute
默认执行的内容- 运行正确,则返回
{‘FINISHED’}
否则return{‘CANCELED’}
- 如果要使用context,不要用bpy.context,使用这个传入的
- 运行正确,则返回
invoke
Operator.invoke用于在调用operator时从上下文初始化operator。modal
保持运行来传递事件,直到returns {‘FINISHED’} or {‘CANCELLED’}.poll
决定了这个命令在哪个视窗可见- 比如说只能在3d view可见:
@classmethod def poll(cls,context): return context.area.type == 'VIEW_3D'
- 比如说只能在3d view可见:
注册
注册calss
在class
外面写两个函数
def register():
bpy.utils.register_calss(类名)
def unregister():
bpy.utils.unregister_calss(类名)
或者当class
多的时候用列表
ObjectList = [
类名,
]
def register():
for obj in ObjectList
bpy.utils.register_calss(obj)
def unregister():
for obj in ObjectList
bpy.utils.unregister_calss(obj)
注册之后,其他Blender窗口也可以用,但是关闭了Blender就没了
可以让它在Blender启动时候自动注册
注册一个函数 def
bpy.types.VIEW_3D_MT_mesh_add.append(mesh_add_menu_draw)
bpy.types.VIEW_3D_MT_mesh_add.remove(mesh_add_menu_draw)
这就是在VIEW_3D_MT_mesh_add
中加入一个按钮功能,这个功能被
def mesh_add_menu_draw(self,context):
self.layout.operator('某个idname')
定义了。
最终会显示在
注册一个custom properties
为物体/场景提供额外的变量属性
bpy.types.Scene.aaaa = bpy.props.StringProperty(
name = 'OBJ Floder', #显示的名称,不是value
subtype = 'DIR_PATH', #创建一个路径选择按钮,选择之后会自动更新name
)
del bpy.types.Scene.aaaa
这个注册也是可以在panel中唤出的
如果注册的是物体属性,就是这样唤出(为避免物体不存在)
这里的context.object 就是bpy.context.active_object
入口
如果不是安装插件,只是text窗口的脚本,要在最后写上
if __name__ == '__main__':
register()
这样启动脚本的时候就进入注册入口
如果是安装好的插件,那么直接开启插件,就是相当于执行register入口。关闭就是unregister
UI
如果想知道某个UI是怎么写的,在设置中system,开启develop extra ,就可以右击它查看源码,都是Python写的