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()
Blender插件学习--Operator and Panel
最新推荐文章于 2024-04-19 01:37:31 发布