I am currently in the process of writing a project in python for Blender to simulate crowds automatically. However I would need for there to be a custom UI to allow the user to choose their settings such as min to max speed, how much they want locations to randomised by etc. But from tutorials I have watched, and links I have followed all seem to have the buttons and sliders only do what is already predefined in blender and does not allow me to make it execute a function I have wrote. I managed to find a workaround for creating a custom shape by adding it to the operator menu, but is there a way for it to save the value of a slider to a variable rather than doing a predefined Blender function.

import bpy
import math
import mathutils

class TargetMakerPanel(bpy.types.Panel) :
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    bl_context = "objectmode"
    bl_label = "CrowD"
        def draw(self, context) :
        layout = self.layout
        scene = context.scene
        TheCol = self.layout.column(align = True)
        TheCol.operator("mesh.make_target", text = "Add Target")
        layout.label(text=" Random Speed:")
        row = layout.row()
        row.prop(scene, "frame_start")
        row.prop(scene, "frame_end")

class MakeTarget(bpy.types.Operator) :
    bl_idname = "mesh.make_target"
    bl_label = "Add Target"
    bl_options = {"UNDO"}

    def invoke(self, context, event) :
        Vertices = \
            mathutils.Vector((-0.5, -0.5,0)),
            mathutils.Vector((0.5, -0.5, 0)),
            mathutils.Vector((0.5, 0.5, 0)),
            mathutils.Vector((-0.5, 0.5, 0)),

        NewMesh = bpy.data.meshes.new("Target")
        NewMesh.from_pydata \
            [[0, 1, 2], [0, 1, 3], [1, 2, 3], [2, 0, 3]]
        NewObj = bpy.data.objects.new("Target", NewMesh)
        return {"FINISHED"}
    #end invoke

def register() :

def unregister() :

if __name__ == "__main__" :

In my code I would ideally like it so that the row that slider that says "Start Frame" would allow me to recognise that value and assign it to a variable called MinSpeed.... Rather than it automatically changing the frame as blender had already defined it as...


You can use custom properties in Blender by defining it like so (for example):

bpy.types.Scene.your_property = bpy.props.IntProperty(
    name = "Name your int prop", # as you want it to appear on the slider
    default = 1, # optional
    description = "Short description of your prop, to show in the popup"


bpy.types.Scene.your_property = bpy.props.StringProperty(
    name = "Name your prop",
    default = "default value for your prop (optional)",
    description = "Short description of your prop, to show in the popup"

You can read this for documentation about properties: http://www.blender.org/documentation/blender_python_api_2_68_2/bpy.props.html

Later, you can use your property in the code, by calling it like so (for example, and assuming you're in a function with context available, like your invoke function of your MakeTarget class) :

if context.scene.your_property == 2:

And, to use it in Blender's UI (TargetMakerPanel class):

row = layout.row()
row.prop(context.scene, "your_property")

Blender will automatically make the UI that correspond the property type. (eg. a slider for an IntProperty, input box for a string, etc.)

You can see more examples here : http://wiki.blender.org/index.php/Dev:2.5/Py/Scripts/Cookbook/Code_snippets/Interface#Using_scene_properties_to_store_information

Hope this helps!


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.