1
$\begingroup$

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.

$\endgroup$
0
2
$\begingroup$

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

import bpy

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    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():
    bpy.utils.register_class(SimpleOperator)

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


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"

bpy.types.OBJECT_PT_context_object.append(draw_func)

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).

$\endgroup$
0
$\begingroup$

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

layout.operator(bl_idname)
$\endgroup$
1
  • 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.