9
$\begingroup$

I want to make something like this:

enter image description here

I want some options to appear in a submenu.

Here is my script which creates only single menu:

bl_info = {
        "name": "Draguu",
        "category": "3D View",
        "author": "Pratik Solanki"
        } 

import bpy


class buttons(bpy.types.Menu):
    bl_label = 'my materials'
    bl_idname = 'view3d.mymenu'

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


        layout.operator('grass.button')
        layout.operator('fall.button')
        layout.operator('skin.button')
        layout.operator('rock.button')
        layout.operator('sky.button', icon = 'WORLD')
        layout.operator('setrender.button')
        layout.operator('setsub.button')
        layout.operator('resub.button')
        layout.operator('mod.button')



#grass button




class hello2(bpy.types.Operator):
    bl_idname = 'grass.button'
    bl_label = 'grass mat'


#on button press event.....................................................

    def execute(self, context):

# Material Properties....................................................
        mato = bpy.data.materials.new('grass')

        if mato!= None:

            mato.preview_render_type = 'HAIR'
            mato.diffuse_color = (0.2,0.8,0.04)
            mato.specular_intensity = 0.1
            mato.specular_hardness = 20
            mato.ambient = 0
            mato.use_transparency = 0
            mato.alpha = 0.01
            mato.strand.root_size = 1.5
            mato.strand.tip_size = 0.25

# adding blend texture  ......................................................


            gtex = bpy.data.textures.new('grass blend', type = 'BLEND')
            gtex.use_color_ramp = 1

            ramp = gtex.color_ramp

            ramp.elements.new(0.05)
            ramp.elements.new(0.18)
            ramp.elements.new(0.4)
            ramp.elements.new(0.69)



            ramp.elements[0].color = (0,0,0,0.2)
            ramp.elements[1].color = (0.02,0.007,0.001,1)
            ramp.elements[2].color = (0.01,0.05,0,1)
            ramp.elements[3].color = (0.02,0.1,0,1)
            ramp.elements[4].color = (0.07,0.3,0,0.8)
            ramp.elements[5].color = (0.2,0.8,0,0.5)
            ramp.elements[5].position = (0.89)


            gtex.progression = 'LINEAR'


 # adding material slot to assign texture ....................................

            mtex = mato.texture_slots.add()
            mtex.texture = gtex
            mtex.texture_coords = 'STRAND'
            mtex.use_map_color_diffuse = True
            mtex.use_map_alpha = True
            mtex.alpha_factor = 0

            ing = 'FLAT'
# adding cloud material for variation 

            #defining texture type and assigning to variable
            ctex = bpy.data.textures.new('grass variation', type = 'CLOUDS')
            mtex2 = mato.texture_slots.add()  # adding texture slot
            mtex2.texture = ctex              # assigning to slot

            ctex.use_color_ramp = 1
            ctex.color_ramp.elements[0].position = 0.28
            ctex.color_ramp.elements[0].color = (0,0.06,0,1)
            ctex.color_ramp.elements[1].color = (0,0.3,0,1)
            ctex.noise_scale = 0.02
            mtex2.texture_coords = 'UV'
            mtex2.blend_type = 'OVERLAY'
            mtex2.diffuse_color_factor = 0.5





# applying material to an object ..............................

            bpy.context.object.data.materials.append(mato)

#closing on press event   ....................................    

        return{"FINISHED"}
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------    


# fall off button ............................................

class hello3(bpy.types.Operator):
    bl_idname = 'fall.button'
    bl_label = 'add falloff'

    @classmethod
    def poll(self, context):
        if context.object and context.object.type == 'MESH':
            return len(context.object.data.materials)

    def execute(self, context):


        fall = context.object.active_material 



        if fall!= None:




            ftex = bpy.data.textures.new('falltex', type = 'BLEND')
            fslot = fall.texture_slots.add()
            ramp = ftex.color_ramp
            fslot.texture = ftex

            ftex.progression = 'SPHERICAL'

            ftex.use_color_ramp = 1
            ftex.color_ramp.elements[1].position = 0.51
            fslot.texture_coords = 'NORMAL'
            fslot.mapping_x = 'NONE'
            fslot.mapping_y = 'NONE'
            fslot.diffuse_color_factor = 0.6
            fslot.blend_type = 'ADD'
            return{"FINISHED"}

#_______________________________________________________________________________-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------               




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

    wm = bpy.context.window_manager
    kc = wm.keyconfigs.addon
    if kc:
        km = kc.keymaps.new(name='3D View', space_type='VIEW_3D')
        kmi = km.keymap_items.new('wm.call_menu', 'D', 'PRESS', ctrl = True, shift = True)
        kmi.properties.name = "view3d.mymenu"

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

    wm = bpy.context.window_manager
    kc = wm.keyconfigs.addon
    if kc:
        km = kc.keymaps['3D View']
        for kmi in km.keymap_items:
            if kmi.idname == 'wm.call_menu':
                if kmi.properties.name == "view3d.mymenu":
                    km.keymap_items.remove(kmi)
                    break

if __name__ == "__main__":
    register()

Can anybody tell me how to make these buttons appear in a submenu?

$\endgroup$
1
  • 4
    $\begingroup$ Please don't links to the script if it can be helped, best include a small example in the question. $\endgroup$
    – ideasman42
    Dec 3, 2013 at 6:27

1 Answer 1

15
$\begingroup$

All you need to do is to define 2 menu's, then reference one from another with layout.menu

import bpy

class ButtonsSub(bpy.types.Menu):
    bl_label = 'my materials'
    bl_idname = 'view3d.mymenu_submenu'

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

        layout.label("This is a submenu")
        layout.operator("render.render")


class ButtonsMain(bpy.types.Menu):
    bl_label = 'my materials'
    bl_idname = 'view3d.mymenu'

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

        layout.label("This is a main menu")
        layout.menu(ButtonsSub.bl_idname)
        layout.operator("render.render")

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

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

if __name__ == "__main__":
    register()

    # call menu now! (you wouldn't normally do this, just to test)
    bpy.ops.wm.call_menu(name=ButtonsMain.bl_idname)
$\endgroup$

You must log in to answer this question.

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