I want to make a button in my panel to add mesh that has been edited. I have edited the mesh with modifiers and some others things. When I press that button, it would like to add a mesh that I have edited.
1 Answer
$\begingroup$
$\endgroup$
3
To make a button that adds a mesh you have edited, you need to make an operator.
You need to type def main(context):
and type what you edit on the mesh.
Operator script:
class OperatorName(bpy.types.Operator):
"""Tooltip"""
bl_idname = "operator_id_name"
bl_label = "label_of_operator"
def execute(self, context):
main(context)
return {'FINISHED'}```
Here is an add-on script that I am working on, example for this question:
def main(context):
bpy.ops.mesh.primitive_monkey_add()
bpy.ops.object.modifier_add(type='SUBSURF')
bpy.context.object.modifiers["Subdivision"].levels = 4
class SmoothMonkey(bpy.types.Operator):
"""Tooltip"""
bl_idname = "mesh.smooth_monkey"
bl_label = "Add Smooth Monkey"
def execute(self, context):
main(context)
return {'FINISHED'}
def register():
bpy.utils.register_class(SmoothMonkey)
def unregister():
bpy.utils.unregister_class(SmoothMonkey)
if __name__ == "__main__":
register()
# test call
# bpy.ops.mesh.smooth_monkey()
To use this operator, you need to type
row = layout.row()
row.operator("mesh.smooth_monkey", icon= 'MONKEY', text= "Add smooth monkey")
after def draw(self, context):
your panel script
-
$\begingroup$ Then how can I add this script into my panel script? That means how can I use this operator in my panel to make a button? $\endgroup$ Commented Apr 4, 2021 at 12:16
-
$\begingroup$ Thanks, your answer after you edited is good $\endgroup$ Commented Apr 4, 2021 at 12:28
-
$\begingroup$ Hey, can you pls answer a question about this script? I want to know how you script after "def main(context):" My question here: blender.stackexchange.com/questions/218377/… $\endgroup$ Commented Apr 4, 2021 at 12:41