1
$\begingroup$

When I add an primitive via Python by using the following code:

import bpy

bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))

the Adjust Last Operation panel does not appear. Do I need to run any other code additionally?

enter image description here

$\endgroup$
0

1 Answer 1

1
$\begingroup$

I don't think you can because this panel is context sensitive, and it only appears when you do something in the 3D viewport, but you are programmatically adding it which will not trigger this panel directly. However, you can create your own operator that will trigger your own Adjust Last Operation panel, but you will have to execute the operator while in the context of the 3D Viewport. Copy the sample My Object Operator operator script and execute it. Then press F3 and search for it and press Enter. Notice I have added 2 properties in this panel which you can extend and can be adjusted. You can also bring this operator back if you haven't done anything else with Edit > Adjust Last Operation or press shortcut F9.

enter image description here

import bpy
from bpy.props import *
import math


class MyOperator(bpy.types.Operator):
    bl_idname = "object.my_operator"
    bl_label = "My Object Operator"
    bl_options = {'REGISTER', 'UNDO'}

    scale_x : FloatProperty (
        name = "This is the X-Scale",
        description = "the x-scale",
        default = 1.0,
        min = 0.1,
        max = 5.0
    )

    rotation_z : IntProperty (
        name = "This is the Z-Rotation",
        description = "the z-rotation",
        default = 45,
        min = 0,
        max = 360
    )

    def execute(self, context):
        result = bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
        o = bpy.context.active_object;
        o.rotation_euler[2] = math.radians(self.rotation_z)
        o.scale[0] = self.scale_x

        return {'FINISHED'}

def register():
    bpy.utils.register_class(MyOperator)

def unregister():
    bpy.utils.unregister_class(MyOperator)


if __name__ == "__main__":
    register()
$\endgroup$
2
  • $\begingroup$ But changing the amount of segments is not possible like this, isn't it? $\endgroup$
    – Lala_Ghost
    Aug 10, 2023 at 14:14
  • $\begingroup$ yes it is possible. the execute function will update whenever you adjust one of the settings in the panel, so there you can update your mesh with different vertices, see this example $\endgroup$ Aug 11, 2023 at 4:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .