Making user inputs (properties?) in a panel: bools, ints, floats, etc

all i want to do is have a box in my panel where the user can type a number, and that number gets stored so i can use it. that’s all i need. it would be good to know how to do this with bools and stuff too, but a number input is the thing i need.

i have been trying to understand this for hours and have found very little information. the things i have found i either didnt understand, or gave me errors.

ive learned that i probably need to be making something called properties/props. but i have failed to make this work. ive been told these kind of work like global variables, which would be perfect for what im doing.

i would be extremely grateful for any help because this is rapidly driving me insane. thank you for reading.

Do you mean like the cutom properties in data blocks?
In the properties panel, if you go to the data blocks panel, for example the objects panel or the mesh panel there is menu titled “cutsom properties”. Here you can add boolean, floats, integers, strings and vectors:

thank you for replying. i dont think those are what im looking for but i appreciate the suggestion.
as an example, in the template: “ui_panel_simple.py”, there is an input for “start frame” and “end frame”. but these seem to work because they are things that already exist that are being referred to. i want to have my own custom thing i can refer to, for example “number of pages in book”. so the user can type in a number, and then ill use that number in something like a for loop to make more pages.

i hope that makes more sense. Im sorry, i dont know the correct terminology at all.

thank you for linking it to me, but im afraid i dont know how to apply that information, for example in a row. this is the first addon i have made and one of my first coding projects. would you be able to give me an example of how it would be used please?

import bpy

class BookPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Book Panel"
    bl_idname = "OBJECT_PT_book"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

    def draw(self, context):
        layout = self.layout
        layout.use_property_split = True
        
        obj = context.object

        col = layout.column()               
        col.prop(obj, "number_of_pages")


def register():
    bpy.utils.register_class(BookPanel)
    bpy.types.Object.number_of_pages = bpy.props.IntProperty(name="Number of Pages",
                                                            description="The number of pages in my book",     
                                                            min=0, 
                                                            max=250, 
                                                            default=5)

def unregister():
    bpy.utils.unregister_class(BookPanel)
    del bpy.types.Object.number_of_pages 

if __name__ == "__main__":
    register()
1 Like

Ah, sorry I didn’t notice that this was in the python sub forum.
I can not tell you how to do this because I have to look it up each time how to do it myself.
But as a non-programmer i can tell you that in my experience that creating the UI in Blender python is often trickier than the actual script itself.
It is badly documented and very unintuitive. It took me days to get it to work when I did it the first time, so just that you know, other people trip over the same seemingly simple thing as well.

The worst part about api reference is the lack of examples for doing a particular task. This does get better the more you work with it. 3ds max was particularly good with providing examples for just about anything in maxscript.

Blender on the other hand doesn’t do this, because maintaining examples through api changes requires a lot resources.

The link posted earlier does provide some examples (see the top of its page), but they don’t really explain the how, because it is assumed that the reader has a basic understanding to object-oriented programming (like eg. adding an attribute to a base type will propagate it to every child instance, etc.).

To create an integer property:

bpy.types.WindowManager.number = bpy.props.IntProperty()

To access and write the property from the python console:

>>> bpy.context.window_manager.number
0
>>> bpy.context.window_manager.number = 2
>>> bpy.context.window_manager.number
2

To access the property in a ui layout (assumes layout code):

layout = self.layout
layout.prop(context.window_manager, "number")

To store the property along with the blend-file, set the property on a so-called ID type:

# Do not use the window manager (does not save properties)
bpy.types.Scene.number = bpy.props.IntProperty()

# Or any classes of bpy.types.ID.__subclasses__() excluding window manager.

To access the property on a future session, you would need to re-initialize the property on the base object. This is what addons do when they register on startup.

1 Like

The Blender cloud has a really good tutorial series by Sybren Stüvel:

ahahahaha, im glad im not the only one who finds the UI harder than the actual script :'D
thats honestly pretty reassuring. thanks for trying to help me :slight_smile:

thank you very much for taking the time to write that. it looks extremely helpful. i will give it a try :slight_smile:

thank you very much. ive seen people use the window manager for the user input, but
i didnt understand it very well. this clears things up a lot. ill give it a try :slight_smile: