Blender插件学习--Operator and Panel

import bpy

def showMat(self,context):
    mats = bpy.data.materials
    for mat in mats:
        print(f"{self.myStr} : {mat.name}")

class myTextOperator(bpy.types.Operator):
   #bl_idname有格式要求,必须小写,下划线或者数字,不允许有大写字母
    bl_idname = "hyq.hello"
    bl_label = "Hello"
    
    # Property
    #自定义属性,使用冒号声明,非等号=
    myfloat:bpy.props.FloatProperty(name="float",default=1.0)
    myStr:bpy.props.StringProperty(name="string",default="Hello Operator")
    select_name:bpy.props.StringProperty(name="select_name",default ="")
    
    @classmethod
    def poll(cls,context):
		#注意,这里的返回true false会影响下面的panel的显示,即是否能激活这个UI界面
    	if bpy.area.ui_type == "VIEW_3D":
    		print("3d 视图")
    		return True
    	else:
    		print("非3d视图")
    		return False
    def invoke(self,context,event):
    	# 通过选择文件获取到文件路径
    	#context.window_manager.fileselect_add(self)
    	#event 监测键盘操作或者鼠标点击事件 ,当发生对应的事件时执行对应的程序
        if event.type == "LEFTMOUSE":
            self.report({"INFO"},self.select_name)
        return{"FINISHED"}  #self.execute(context)	#这里可以return{“FINISHED”}结束这个invoke方法,也可以返回调用另一个方法,比如执行self.execute(context)
    
    def execute(self,context):
        print(f"now myfloat is {str(self.myfloat)}")
        self.myfloat +=1
        print(f"next myfloat is {str(self.myfloat)}")
        
        #call
        showMat(self,context)	#可以调用这个类外面的方法
        self.report({"INFO"},self.myStr)
        
        return{"FINISHED"}



class myPlane(bpy.types.Panel):
    """Creates a Panel in the scene context of the properties editor"""
    bl_label = "Layout Demo"
    bl_idname = "SCENE_PT_layout"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"
    
    
    def draw(self,context):
        layout = self.layout
        scene = context.scene
        # Big render button
        layout.label(text="Big Button:")
        row = layout.row()
        row.scale_y = 3.0
        row.operator("hyq.hello")
def register():
    bpy.utils.register_class(myTextOperator)
    bpy.utils.register_class(myPlane)
    

def unregister():
    bpy.unils.unregister_class(myTextOperator)
    bpy.unils.unregister_class(myPlane)
    
if __name__ == "__main__":
    register()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值