Using an If/Else statement in a Python Operator for Custom Rig Controls

Hello! first time posting this but I need help, I was looking into the Blender’s Python API to figure out how panels and buttons are created because I wanted to create my own Rig control panel instead of relying on pre-built rig controls for ease of access whenever I create model rigs. To do that I needed to utilize “Operators” through two scripts I was writing: one to make a button executable and then signal the if/else statement in the operator script which button/option was pressed and allowing which pose bone can be selected. unfortunately though, the code isn’t working:


import bpy

class LayoutDemoPanel(bpy.types.Panel):
	"""Creates a Panel in the scene context of the properties editor"""
	bl_label = "Bone Selection Test"
	bl_category = "Rig"
	bl_space_type = 'VIEW_3D'
	bl_region_type = 'TOOLS'
	bl_context = 'posemode'

#   def unselectBones():
#   	for i in[active_pose].data.bones:
#[active_pose].data.bones[].select = False

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

		scene = context.scene

		# Big render button
		layout.label(text="Choose a Bone:")
		row = layout.row()
		row.scale_y = 3.0
		row.operator("rig.bone_picker", text="Top Bone").option = "Choice_1"

def register():

def unregister():

if __name__ == "__main__":

You may have noticed the row.operator() having the “Option” equals right? Following this interesting Operator tip: Custom operator with Arguments I thought using custom arguments can get the if/else options to work in the operator script:


import bpy

def main(context, choice):
    #Deselects all bones in active "Pose Mode"
	active_pose = #"Armature"
	for i in[active_pose].data.bones:[active_pose].data.bones[].select = False

    #Where depending which button is pressed, the if else statement is to choose an option of which bone.
	if choice is "Choice_1":[active_pose].data.bones["Bone.001"].select = True
	elif choice is "None":

class BonePicker(bpy.types.Operator):
	bl_idname = "rig.bone_picker"
	bl_label = "Animation Bone Rig Pick"
	option = bpy.props.StringProperty(default="None")

	def execute(self, context):
		main(context, option)
		return {'FINISHED'}

def register():

def unregister():

if __name__ == "__main__":

	# test call
	# bpy.ops.rigrig.bone_picker()

Instead I end up getting this error:

Does anybody happen to know how I can get the if/else statement within the operator to work by using custom operator arguments? Any info or tips would be appreciated!

P.S. Here is the Blend file if you need to test it out, there should be an armature with two bones.
Bone_Picker_Test.blend (531.1 KB)

According to your error message, your mistake seems to be on this line in the execute function of the BonePicker class:

main(context, option)

it should be

main(context, self.option)

I’ll try that out.

That part works but now the If/Else statement isn’t working right, I am using print(choice) to make sure the self.option is going through which in the python console says it is, however when I set the option/choice to “None” Text, I assumed that the IF/ELSE statement would go to the output of print(“Empty”):

	elif choice is "None":

Instead, nothing comes out of the python terminal.

the self.option/choice doesn’t go through the if/else statement, and I have no idea on how to resolve that. Is there a typo or mistake in how the if/else statements are handled?

Nevermind, figured it out:

	if choice == "Choice_1":[active_pose].data.bones["Bone.001"].select = True
	elif choice == "None":

Using double equals instead of “if”. The code works now. Thanks!