Blender 简单插件,Python实现

Python —— Blender 简单插件

建立一个方块

bpy.ops.mesh.primitive_cube_add(location=(0,0,3))

ops表示操作 data表示操作 context表是上下文、运行的环境 type表示所有行为类型 ……

bpy 中 data 与 context

分别赋值给 D 和 C

  • data
    • for d in D.objects:print(d.name) 输出场景中所有问题
  • context
    • for c in C.selected_objects:print(c.name) 输出所需选文件
    • for c in C.selected_objects:c.scale=(2,2,2) 选择物体放大二倍

脚本中编写 需要依赖 Blender Console

插件

  • 插件的身份 定义字典 表示 放在开头
bl_info={
    'name':'Hi Addons', #插件名称
    'category':'3D View', #插件类别
    'blender':(2,80,0), #插件所兼容的版本
    'author':'Angie', #作者名
    'version':(0,0,1), #插件版本号
    'location':'Addons location', #用于说明插件在哪 其位置
    'description':'Addon’s power' #插件功能说明
}
import bpy #引入bpy包

class hiadd(bpy.types.Panel):
    bl_idname = 'hiadd'
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_label = 'Hi Addons'
    
    def draw(self,context):
        self.layout.label(text = '我是一个莫得感情的插件')
        
def register():
    bpy.utils.register_class(hiadd)
    
def unregister():
    bpy.utils.unregister_class(hiadd)
    
if __name__ == '__main__':
    register()
编写UI

编写是时 可参考 blender 文件夹下 version(2.82)/scripts/bl_ui 中的 py 文件

  • 面板的四种组件
    • label 标签
    • operator 操作 所有操作命令的工具通常为按钮 显示再菜单项 与 bpy.ops 是同一工具
    • prop 属性 对应界面的文本编辑框、数值条
    • 已经各种容器
def (self,context):
layout = self.layout
layout.label(text='哥是插件')
layout.operator('mesh.primitive_cube_add')
latout.prop(context,object,'name',text='Object Name')
"""读取了 context 的 object 操作符 或读取被最后选中物体的参数 如: name scale location"""
  • 面板的三种容器 其中 row colum 都有 align 参数 为 Ture 时会时其内部组件连在一起
    • row 行
    • colum 列
    • box 盒子
#需上述代码
layout.lable(text='新行容器 不相连')
row = layout.row() #初始化一个行容器
row.operator('mesh.primitive_cube_add')
row.operator('mesh.primitive_cube_add')
row.operator('mesh.primitive_cube_add')

layout.lable(text='新行容器 相连、分隔与分割')
row = layout.row('align=True') #初始化一个行容器 改 align
row.operator('mesh.primitive_cube_add')
row.separator() #分隔符,表现为横线
row.operator('mesh.primitive_cube_add')

split = row.split() #分割行容器
col = split.column(align=Ture) #在分割后的行容器中创建列容器
col.operator('mesh.primitive_cube_add')
col.operator('mesh.primitive_cube_add')

split = row.split() #分割行容器
col = split.column(align=Ture) #在分割后的行容器中创建列容器
col.operator('mesh.primitive_cube_add')
col.separator()
col.operator('mesh.primitive_cube_add')

Icon Viewer插件 提供图标

编写菜单
import bpy

def addmenu(self,context):
    self.layout.label.(text='我是一个菜单')
    
if __name__ == '__main__':
    bpy.types.VEIW3D_MT_view.append(addmenu)
bl_info = {
    'name' : 'Fancy objects',
    'author' : 'Angie',
    'version' : (0,0,1),
    'blender' : (2,80,0),
    'location' : 'View3D > Tool',
    'warning' : '',
    'wiki_url' : '',
    'catgory' : 'Fancy '
}

import bpy

class Fancy_Panel(bpy.types.Panel):
    bl_label = 'Fancy_objects'
    bl_idname = 'Fancy_odj'
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'Fancys'
    
    def draw(self,context):
        layout = self.layout
        
        row = layout.row() #init a line create a line
        row.label(text='Make Cube Fancy')
        row = layout.row()
        row.operator('mesh.primitive_cube_add',icon='CUBE')
# operator 创造按钮 相当于 () 中的操作 ops

class PanelA(bpy.types.Panel):
    bl_label = 'PanelA'
    bl_idname = 'PanelA'
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'PanelA is'
    bl_parent_id = 'Fancy_odj'
    bl_options = {'DEFAULT_CLOSED'}
    
    def draw(self,context):
        layout = self.layout
        obj = context.objecct()
        
        row = layout.row() #init a line create a line
        row.label(text='Select an option to scale your object')
        row = layout.row()
        row.operator('transform.resize')
        layout.scale_y = 1.4 #设置按键宽度
        
        col = layout.column() #建立列
        col.prop(odj,'scale')

        
class PanelB(bpy.types.Panel):
    bl_label = 'Specials'
    bl_idname = 'PanelB'
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'PanelV is'
    bl_parent_id = 'Fancy_odj' #in a class
    bl_options = {'DEFAULT_CLOSED'}
    
    def draw(self,context):
        layout = self.layout
        
        row = layout.row() #init a line create a line
        row.label(text='A PanelB')
        row = layout.row()

def register():
    bpy.utils.register_class(Fancy_Panel)
    
def unregister():
    bpy.utils.unregister_class(Fancy_Panel)
    
if __name__ = '__main__':
    register()
import bpy
import random

class Fancy_Panel(bpy.types.Panel):
    bl_label = 'Fancy_objects'
    bl_idname = 'Fancy_odj'
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'Fancys'
    
    def draw(self,context):
        layout = self.layout
        object = context.object
        
        row = layout.row() #init a line create a line
        row.label(text='Add Object')
        row = layout.row()
        row.operator('mesh.primitive_cube_add',icon='CUBE')
        row.operator('mesh.primitive_ico_sphere_add',icon='SPHERE')
#        row.separator()
#        row.operator(object,name,text='Object Name')

class cuberandom(bpy.types.Operator):
    bl_idname = 'Cube_random'
    bl_label = 'Cuble Rand'
    
    def fun_randcube(self,context):
        for i in context.selected_objects:
            i.location.z=random.random()*10
        self.report({'INFO','RandCube is OK !'})
        return {'INISHED'}
        
def register():
    bpy.utils.register_class(Fancy_Panel)
    bpy.utils.register_class(cuberandom)
    
def unregister():
    bpy.utils.unregister_class(Fancy_Panel)
    bpy.utils.unregister_class(cuberandom)
    
if __name__ == '__main__':
    register()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值