1
$\begingroup$

I am working on making a custom menu that would allow access to things like Cycle Render, Blender Render, changing file formats for render outputs, etc.

I understand how to make menus using operators like

layout.operator("rigidbody.objects_add", text="Add as Active"). type = 'ACTIVE'

Is the process for creating menus for bpy.context.scene similar or am I missing some basic understanding of python and how Blender works?

UPDATE....

In the info panel I am trying to execute this.

 bpy.context.scene.render.engine = 'CYCLES' 

I am using this template from cgcookie.

EXAMPLE

import bpy

# Creates a menu for global 3D View
class customMenu(bpy.types.Menu):
    bl_label = "Sub"
    bl_idname = "view3D.sub"

    # Set the menu operators and draw functions
    def draw(self, context):
        layout = self.layout

        layout.operator("rigidbody.objects_add", text="A Add Active").type = 'ACTIVE'
        layout.operator("rigidbody.objects_add", text="B Add Passive").type = 'PASSIVE'
      # here is were I would like to add a menu item to change to cycles.

def register():
    bpy.utils.register_class(customMenu)
    bpy.ops.wm.call_menu(name=customMenu.bl_idname)

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

if __name__ == "__main__":
    register()
$\endgroup$
  • $\begingroup$ care to show a mock-up it's difficult to be confident about what the question is, or the desired result. $\endgroup$ – zeffii Jun 15 '15 at 12:48
2
$\begingroup$

You want to have a look at the ops.wm.context_... family of operators, which are 'generic' operators that can be used to define a value (or cycle through a set of values) of any compatible property accessible through context. This avoid the need to define a custom operator.

E.g. for an enum property like context.scene.render.engine:

import bpy

# Creates a menu for global 3D View
class customMenu(bpy.types.Menu):
    bl_label = "Sub"
    bl_idname = "view3D.sub"

    # Set the menu operators and draw functions
    def draw(self, context):
        layout = self.layout

        layout.operator("rigidbody.objects_add", text="A Add Active").type = 'ACTIVE'
        layout.operator("rigidbody.objects_add", text="B Add Passive").type = 'PASSIVE'
        op = layout.operator("wm.context_set_enum", text="Set Cycles Renderer")
        op.data_path = "scene.render.engine"
        op.value = 'CYCLES'


def register():
    bpy.utils.register_class(customMenu)
    bpy.ops.wm.call_menu(name=customMenu.bl_idname)

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

if __name__ == "__main__":
    register()

Note that you can also use layout.prop() and the like, just like in a regular panel - but beware, most of our widgets are not really designed nor tested in menus… Afaik in 'official' UI we only do that for some boolean properties (giving small toggle boxes in the menus). Seems to work rather well with enums, though, producing a sub-menu:

import bpy

# Creates a menu for global 3D View
class customMenu(bpy.types.Menu):
    bl_label = "Sub"
    bl_idname = "view3D.sub"

    # Set the menu operators and draw functions
    def draw(self, context):
        layout = self.layout

        layout.operator("rigidbody.objects_add", text="A Add Active").type = 'ACTIVE'
        layout.operator("rigidbody.objects_add", text="B Add Passive").type = 'PASSIVE'
        layout.prop(context.scene.render, "engine")

def register():
    bpy.utils.register_class(customMenu)
    bpy.ops.wm.call_menu(name=customMenu.bl_idname)

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

if __name__ == "__main__":
    register()
$\endgroup$
  • $\begingroup$ In the info panel I am trying to execute this. "bpy.context.scene.render.engine = 'CYCLES' import bpy class customMenu(bpy.types.Menu): bl_label = "Sub" bl_idname = "view3D.sub" def draw(self, context): layout = self.layout layout.operator("rigidbody.objects_add", text="A Add Active").type = 'ACTIVE' layout.operator("rigidbody.objects_add", text="B Add Passive").type = 'PASSIVE' layout.operator("rigidbody.objects_remove", text="C Remove from Rigid") # add this layout.context("scene.render").engine = 'CYCLES' $\endgroup$ – Mark Jun 16 '15 at 13:18
  • 1
    $\begingroup$ @Mark Please edit your question rather than adding precisions in comments, this is not even readable. Also, assuming you are doing this in draw code, you will not be allowed to change anything (any data) in such part of the code… $\endgroup$ – mont29 Jun 16 '15 at 17:08

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.