How can I set the value of a string property when a button is pressed?

Just to clarify I don't mean in the blender game engine I mean in "regular" scripting.


A button is a representation of an Operator in a layout. Example operator code:

import bpy

class SimpleOperator(bpy.types.Operator):
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    def execute(self, context):
        ob = context.object
        if ob is not None:
            ob.name = "Foo Bar"
        return {'FINISHED'}

def register():

def unregister():

if __name__ == "__main__":

You put your code to assign the new string value in the execute() method.

If you don't need any logic, but want to plainly assign to a context member, you may use the existing operators bpy.ops.wm.context_set_*:

import bpy

def draw_func(self, context):
    props = self.layout.operator("wm.context_set_string", text="Change object name")
    props.data_path = "object.name"
    props.value = "New Name"


This code adds a button to the Object panel in the Properties Editor to change the context object's name property (which is a string).


Blender uses operators to perform actions. See the api documentation and the Operator Simple template (Text editor -> Header Menu -> Templates -> Python)

A button is added in the draw function of a subclass of bpy.types.Panel via

  • 1
    $\begingroup$ How does that solve the problem? $\endgroup$ Oct 13 '14 at 18:06

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.