If else function

I have two objects "ObjectA" and "ObjectB". Object A when selected extrudes up in the z direction. Object B when selected extrudes down in the z direction.

Currently I have two different buttons each having their own script. One for A the other for B.

I would like to have one button. So this is where I need a script and it gets tricky. If Object A is selected, it will run the script, else it does nothing. If object B is selected it runs the script, else it does nothing.

I hope I am descriptive enough..

import bpy

ObjectA=bpy.data.objects['Cube']
ObjectB=bpy.data.objects['Cone']

class ObjectAOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.objecta_operator"
bl_label = "Simple ObjectA Operator"

@classmethod
def poll(cls, context):
return context.active_object is not None

def execute(self, context):
context.active_object.location[2] += 1
return {'FINISHED'}

class ObjectBOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.objectb_operator"
bl_label = "Simple ObjectB Operator"

@classmethod
def poll(cls, context):
return context.active_object is not None

def execute(self, context):
context.active_object.location[2] -= 1
return {'FINISHED'}

class HelloWorldPanel(bpy.types.Panel):
"""Creates a Panel in the Object properties window"""
bl_label = "Hello World Panel"
bl_idname = "OBJECT_PT_hello"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "object"

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

obj = context.object

row = layout.row()
row.label(text="Hello world!", icon='WORLD_DATA')

row = layout.row()
row.label(text="Active object is: " + obj.name)
row = layout.row()
row.prop(obj, "name")

row = layout.row()
if obj == ObjectA:
row.operator("object.objecta_operator")
if obj == ObjectB:
row.operator("object.objectb_operator")

def register():
bpy.utils.register_class(ObjectAOperator)
bpy.utils.register_class(ObjectBOperator)
bpy.utils.register_class(HelloWorldPanel)

def unregister():
bpy.utils.unregister_class(HelloWorldPanel)
bpy.utils.unregister_class(ObjectAOperator)
bpy.utils.unregister_class(ObjectBOperator)

if __name__ == "__main__":
register()


Try this simple example whether it satisfies your requirements.

• You can simplify this massively by moving the if statement into the operator. – timodriaan Jun 9 '19 at 11:19
• @timodriaan how exactly? could please provide an example? – Andrew Patynko Jun 13 '19 at 6:47
• Delete one of both operators and copy the if/else from draw() into the other operator's execute method. Then replace the call to the operators inside the if/else with the actual code (context.active_object.location[2] += 1 e.g.) – timodriaan Jun 13 '19 at 19:04
• @timodriaan Yes, I agree that in this simple example it makes sense to have less code. But initially I separated into two operator because in the main question it was mentioned "two different buttons each having their own script". And I thought having two operators simplifies understanding of the logic. – Andrew Patynko Jun 14 '19 at 7:10