0
$\begingroup$

I have a bpy.types.Panel where I have two buttons. The first button is 'test render' and second is 'final render'. I'd basically want to call the same operator class for both these buttons just that they will perform slightly different instructions.

Image showing the panel with test render and final render settings buttons

I want to be able to access this as an argument in my bpy.types.Operator class. How would I do this?

Is there a way to pass the argument in the row operator? So that I can check that value in my operator class and based on that value set the values?

row.operator(TestOP.bl_idname, text="Test Render Settings", icon='PLAY')

So basically when I press the button for test render I should be able to check it in my operator and set the values accordingly.

Something like

class TestOP(bpy.types.Operator, rendertype: string):
    bl_label = "Test Operator"
    bl_idname = "op.test"
    
    def execute(self, context):
        if rendertype == "test":
            bpy.context.scene.cycles.samples = 28
        elif rendertype == "final":
            bpy.context.scene.cycles.samples = 512
        return {'FINISHED'}

I thought it would be possible to maybe pass some args in TestOP.bl_idname("testorfinal") but I'm not sure how I'm supposed to go about it.

Link to code

Thanks in advance

$\endgroup$
1
  • $\begingroup$ You may find the Blender scripting for artist series useful episode 10 $\endgroup$ Commented Jan 25 at 8:42

1 Answer 1

2
$\begingroup$

Yes, you can pass the operator's property value like this

class TestOP(bpy.types.Operator):
    bl_label = "Test Operator"
    bl_idname = "op.test"

    render_type: bpy.props.StringProperty(name="Render Type", default="test")

    def execute(self, context):
        if self.render_type == "test":
            bpy.context.scene.cycles.samples = 28
        elif self.render_type == "final":
            bpy.context.scene.cycles.samples = 512
        return {'FINISHED'}

Inside the panel

op = row.operator(TestOP.bl_idname, text="Test Render Settings", icon='PLAY')
op.render_type = "test" # or "final"

Complete code

import bpy
from bpy.types import Panel, Operator


class TestOP(Operator):
    bl_label = "Test Operator"
    bl_idname = "op.test"

    render_type: bpy.props.StringProperty(name="Render Type", default="test")

    def execute(self, context):
        if self.render_type == "test":
            bpy.context.scene.cycles.samples = 28
        elif self.render_type == "final":
            bpy.context.scene.cycles.samples = 512
        return {"FINISHED"}


class testorfinalrenderpanel(Panel):
    bl_label = "Test/Final Renderer"
    bl_idname = "PT_TestOrFinalRender"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_category = "Test"

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

        obj = context.object
        row = layout.row()
        row.label(text="Test Render", icon="RENDER_STILL")
        row = layout.row()
        op = row.operator(TestOP.bl_idname, text="Test Render Settings", icon="PLAY")
        op.render_type = "test" 

        obj = context.object
        row = layout.row()
        row.label(text="Final Render", icon="RESTRICT_RENDER_OFF")
        row = layout.row()
        op = row.operator(TestOP.bl_idname, text="Final Render Settings", icon="PLAY")
        op.render_type = "final"


classes = [
    TestOP,
    testorfinalrenderpanel,
]


def register():
    for cls in classes:
        bpy.utils.register_class(cls)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)


if __name__ == "__main__":
    register()
$\endgroup$
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.