2
$\begingroup$

I tried to create a menu panel for drawing the mesh, so I applied:

import bpy

class CustomPanel(bpy.types.Panel):
    """A Custom Panel in the Viewport Toolbar"""
    bl_label = "Generate"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'

    def draw(self, context):
        layout = self.layout

        row = layout.row()
        row.label(text="Generate objects:")

        split = layout.split()
        col = split.column(align=True)

        layout.label(text="Primitives:")
        layout.operator("mesh.primitive_plane_add", text="Plane", icon='MESH_PLANE')
        layout.operator("mesh.primitive_cube_add", icon='MESH_CUBE', text="Cube")
        layout.operator("mesh.primitive_circle_add", icon='MESH_CIRCLE', text="Circle")
        layout.operator("mesh.primitive_uv_sphere_add", icon='MESH_UVSPHERE', text="UV Sphere")                
        layout.operator("mesh.primitive_ico_sphere_add", text="Ico Sphere", icon='MESH_ICOSPHERE')
        layout.operator("mesh.primitive_cylinder_add", text="Cylinder", icon='MESH_CYLINDER')
        layout.operator("mesh.primitive_cone_add", text="Cone", icon='MESH_CONE')
        layout.operator("mesh.primitive_torus_add", text="Torus", icon='MESH_TORUS')


        #
        col.operator("mesh.primitive_plane_add", text="Plane"), 
        col.operator("mesh.primitive_cube_add", text="Cube")


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

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

if __name__ == "__main__":
    register()

but when I try to ecris as a drop-down list, like this: How to make compare with Menus I haven't succeeded, someone help me how writing in menu panel?

$\endgroup$
3
$\begingroup$

the easy way by reusing existing UI code

enter image description here

code:

import bpy

class CustomPanel(bpy.types.Panel):
    """A Custom Panel in the Viewport Toolbar"""
    bl_label = "Generate"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'

    def draw(self, context):
        obj = context.active_object
        mode_string = context.mode
        edit_object = context.edit_object

        layout = self.layout
        col = layout.column()
        col.label(text="Generate objects:")

        if mode_string == 'OBJECT':
            layout.menu("INFO_MT_add", text="Add")
        elif mode_string == 'EDIT_MESH':
            layout.menu("INFO_MT_mesh_add", text="Add")

def register():
    bpy.utils.register_module(__name__)


def unregister():
    bpy.utils.unregister_module(__name__)


if __name__ == "__main__":
    register()

code (the explicit way)

Gives something like this:

enter image description here

import bpy

def execute_operator(self, context):
    eval('bpy.ops.' + self.primitive + '()')

class MyShortAddonProperties(bpy.types.PropertyGroup):
    mode_options = [
        ("mesh.primitive_plane_add", "Plane", '', 'MESH_PLANE', 0),
        ("mesh.primitive_cube_add", "Cube", '', 'MESH_CUBE', 1),
        ("mesh.primitive_circle_add", "Circle", '', 'MESH_CIRCLE', 2),
        ("mesh.primitive_uv_sphere_add", "UV Sphere", '', 'MESH_UVSPHERE', 3),
        ("mesh.primitive_ico_sphere_add", "Ico Sphere", '', 'MESH_ICOSPHERE', 4),
        ("mesh.primitive_cylinder_add", "Cylinder", '', 'MESH_CYLINDER', 5),
        ("mesh.primitive_cone_add", "Cone", '', 'MESH_CONE', 6),
        ("mesh.primitive_torus_add", "Torus", '', 'MESH_TORUS', 7)
    ]

    primitive = bpy.props.EnumProperty(
        items=mode_options,
        description="offers....",
        default="mesh.primitive_plane_add",
        update=execute_operator
    )


class CustomPanel(bpy.types.Panel):
    """A Custom Panel in the Viewport Toolbar"""
    bl_label = "Generate"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'

    def draw(self, context):
        layout = self.layout
        col = layout.column()
        col.label(text="Generate objects:")
        col.prop(context.scene.my_short_addon, "primitive")


def register():
    bpy.utils.register_module(__name__)
    bpy.types.Scene.my_short_addon = bpy.props.PointerProperty(type=MyShortAddonProperties)


def unregister():
    bpy.utils.unregister_module(__name__)
    del bpy.types.Scene.my_short_addon


if __name__ == "__main__":
    register()

You were close.. Do I need to explain what's going on here or do you think you can figure it out?

$\endgroup$
  • $\begingroup$ Sorry, thank for this script so, when i click for example in mesh cube, Box appears as the following picture: [IMG]i64.tinypic.com/2vx3e3s.png[/IMG] how to add this, knowing that I created a script that tied between number of cubes and dimensions $\endgroup$ – saded Dec 11 '15 at 17:22
  • $\begingroup$ Now you are asking a separate question. Ask another new question. Personally I would simply hit F6 directly after adding the primitive, that's the Operator Properties. I'm not sure if it's possible to display those inside your addon's panel.. -- that wasn't part of your original question. $\endgroup$ – zeffii Dec 11 '15 at 17:34
  • $\begingroup$ ok no problem, thank you again, I expected someone who can do that. $\endgroup$ – saded Dec 11 '15 at 17:37
  • $\begingroup$ it might be possible but you should ask a new separate question if you want an answer $\endgroup$ – zeffii Dec 11 '15 at 17:39
  • $\begingroup$ something like "is it possible to display the Operator properties of an Active Add Primitive operator in a Custom Panel" $\endgroup$ – zeffii Dec 11 '15 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.