0
$\begingroup$

enter image description here

I need to create a couple of text input fields for my Blender addon. When the script starts, the first text should be replaced by the second one. So I need to create 2 text input fields and to have access to the data entered in these fields with Python API.

Does someone know how to do it? Tried to google something, but didn't succeed.

import bpy
from bpy.props import StringProperty

mystr: StringProperty(name="Enter something:")


class HelloWorldPanel(bpy.types.Panel):
    bl_idname = "OBJECT_PT_hello_world"
    bl_label = "Hello! World."
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

def draw(self, context):
    self.layout.label(text="He that endureth to the end shall be saved.")
    self.layout.prop("mystr")
    
def register():
    bpy.utils.register_class(HelloWorldPanel)

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

if __name__ == "__main__":
    register()
$\endgroup$
4
  • 1
    $\begingroup$ Having a hard time to understand what you mean by "when the script starts, the text should be replaced by...". Can you elaborate please? In the meantime you can have a look into How to create a custom UI, there is even a nice template. BTW: the bare minimum here is to show some effort towards your goal, at least some research. I recommend always add the current state of your code to the question: blender.stackexchange.com/help/how-to-ask $\endgroup$
    – brockmann
    Commented Nov 24, 2020 at 18:24
  • $\begingroup$ OK, I've added my code. I declared StringProperty, but I have no idea how to "insert" it into my panel. $\endgroup$
    – Mapper720
    Commented Nov 26, 2020 at 7:07
  • $\begingroup$ Recommend read the post I have linked fully: blender.stackexchange.com/questions/57306/… ... and let us know what you do not understand. All explained in there, so... $\endgroup$
    – brockmann
    Commented Nov 26, 2020 at 8:02
  • 1
    $\begingroup$ Related blender.stackexchange.com/questions/127462/… IMO Major issue above is: akin to python's getattr(..) ==> layout.prop(obj, propertyname) expects the object and the property name. Either annotate your own class (as answered below by @brockmann), or define it on the class, eg for a scene property bpy.types.Scene.foo = StringProperty(...) to make it a property of any instance of a blender scene. layout.prop(context.scene, "foo") $\endgroup$
    – batFINGER
    Commented Nov 26, 2020 at 10:01

1 Answer 1

2
$\begingroup$

You would have to register a Panel and as much StringProperties as you like. In order to display your properties on the panel, call layout.prop().

Following demo is based on How to create a custom UI?

import bpy

from bpy.props import (StringProperty,
                       PointerProperty,
                       )
                       
from bpy.types import (Panel,
                       PropertyGroup,
                       )


# ------------------------------------------------------------------------
#    Scene Properties
# ------------------------------------------------------------------------

class MyProperties(PropertyGroup):

    foo: StringProperty(
        name="Foo",
        description=":",
        default="",
        maxlen=1024,
        )

    bar: StringProperty(
        name="Bar",
        description=":",
        default="",
        maxlen=1024,
        )


# ------------------------------------------------------------------------
#    Panel in Object Mode
# ------------------------------------------------------------------------

class OBJECT_PT_CustomPanel(Panel):
    bl_label = "My Panel"
    bl_idname = "OBJECT_PT_custom_panel"
    bl_space_type = "VIEW_3D"   
    bl_region_type = "UI"
    bl_category = "Tools"
    bl_context = "objectmode"   

    @classmethod
    def poll(self,context):
        return context.object is not None

    def draw(self, context):
        layout = self.layout
        scene = context.scene
        mytool = scene.my_tool

        layout.prop(mytool, "foo")
        layout.prop(mytool, "bar")
        layout.separator()

# ------------------------------------------------------------------------
#    Registration
# ------------------------------------------------------------------------

classes = (
    MyProperties,
    OBJECT_PT_CustomPanel
)

def register():
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)

    bpy.types.Scene.my_tool = PointerProperty(type=MyProperties)

def unregister():
    from bpy.utils import unregister_class
    for cls in reversed(classes):
        unregister_class(cls)
    del bpy.types.Scene.my_tool


if __name__ == "__main__":
    register()
$\endgroup$
2
  • $\begingroup$ Thanks, it seems I've figured it out. $\endgroup$
    – Mapper720
    Commented Nov 27, 2020 at 16:15
  • $\begingroup$ TypeError: PointerProperty(...) expected an RNA type, failed with: RuntimeError: , missing bl_rna attribute from 'RNAMetaPropGroup' instance (may not be registered) $\endgroup$
    – Phil
    Commented Jun 5, 2022 at 10:20

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