0
$\begingroup$

I am new to python and scripting for blender, I'm using 2.8. I would love go have a text box field in a UI panel that the user can enter a value and its stored on a variable. My full plan is to have multiple boxes for when you have to make repetitive scaling you can just enter the numbers once then apply them to any object you want.

Slightly related to that, how do you have the value of a variable displayed as text in the U

$\endgroup$
1

1 Answer 1

2
$\begingroup$

Quick and dirty:

bpy.context.scene['MyProperty'] = 5 # or 'yay' or whatever

You do not have to declare anything when using this method.But this is adding to the struct "scene", more practical:

bpy.types.Scene.MyProperty = 5 # registers a new variable to the scene
print(bpy.context.scene.MyProperty) # prints 5

Cleanest way I know of:

bpy.types.Scene.MyProperty = bpy.props.FloatProperty()

If you want to use it as part of an addon, you may want to check if MyProperty registered already, otherwise you risk overwriting settings from the previous session.
More information: https://docs.blender.org/api/current/bpy.props.html
To display your value, you can use any field with information, e.g.

def draw(self, context):
    self.layout.label(text=str(bpy.context.scene.MyProperty)) 

More info about panels here: https://docs.blender.org/api/current/bpy.types.Panel.html

$\endgroup$
2
  • $\begingroup$ How would I increment that variable? Like var = var + 1 $\endgroup$ Commented Jul 30, 2020 at 14:30
  • $\begingroup$ bpy.context.scene.MyProperty += 1 $\endgroup$ Commented Jul 30, 2020 at 15:14

Not the answer you're looking for? Browse other questions tagged .