I'm attempting to learn Python, and figured the easiest way to learn would be by studying code snippets, and expanding from there. So to start with, I want to slightly modify the "ui_panel_simple" template so that instead of adding a primitive object at the 3D cursor, it will add a duplicate of the active object at the 3D cursor.
The full code is as follows:
import bpy class HelloWorldPanel(bpy.types.Panel): """Creates a Panel in the Object properties window""" bl_label = "Add object duplicate" bl_idname = "OBJECT_PT_hello" bl_space_type = 'PROPERTIES' bl_region_type = 'WINDOW' bl_context = "object" def draw(self, context): layout = self.layout obj = context.object row = layout.row() row.label(text="Add duplicate", icon='OBJECT_DATA') row = layout.row() row.label(text="Active object is: " + obj.name) row = layout.row() row.prop(obj, "name") row = layout.row() row.operator("mesh.primitive_cube_add") def register(): bpy.utils.register_class(HelloWorldPanel) def unregister(): bpy.utils.unregister_class(HelloWorldPanel) if __name__ == "__main__": register()
If I change
row.operator(bpy.ops.object.duplicate(), then I lose the newly added button in the custom panel. I can freely change 'cube" to any primitive object and all works as expected. What am I missing?