0

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

Mapper720
  • 15
  • 2
  • 7
  • 1
    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: https://blender.stackexchange.com/help/how-to-ask – brockmann Nov 24 '20 at 18:24
  • OK, I've added my code. I declared StringProperty, but I have no idea how to "insert" it into my panel. – Mapper720 Nov 26 '20 at 07:07
  • Recommend read the post I have linked fully: https://blender.stackexchange.com/questions/57306/how-to-create-a-custom-ui/57332#57332 ... and let us know what you do not understand. All explained in there, so... – brockmann Nov 26 '20 at 08:02
  • 1
    Related https://blender.stackexchange.com/questions/127462/how-do-i-register-scene-properties-with-python-in-2-8 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") – batFINGER Nov 26 '20 at 10:01

1 Answers1

2

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

brockmann
  • 12,613
  • 4
  • 50
  • 93
  • Thanks, it seems I've figured it out. – Mapper720 Nov 27 '20 at 16:15
  • TypeError: PointerProperty(...) expected an RNA type, failed with: RuntimeError: , missing bl_rna attribute from 'RNAMetaPropGroup' instance (may not be registered) – Phil Jun 05 '22 at 10:20