0
$\begingroup$

AI Icon Warning, Potential AI generated content⚠️

This post is suspected to have been generated with the help of an artificial intelligence, chat bot, or other language model tools without proper attribution.

There is reason to believe this post was not written by a human due to its structure, and grammatical construction, and has been marked by our community.
Current AI chat bots are a language models, that means they are like a parrot, they can mimic human language well, but they are built to write seemingly logical, seemingly factual grammatically valid text, rather than actually be correct, like a human would.
The veracity of the following content cannot be ascertained. See the Concerns regarding AI generated content.

I'm trying to create a piece of Blender Text that is linked via a driver to the value of a custom property. The following keeps throwing an error:

import bpy

# Check if there is an active object
if bpy.context.active_object is None:
    print("Please select an object first.")
else:
    selected_object = bpy.context.active_object
    
    # Check if the selected object has the specified custom property
    custom_property_name = "0V Description"
    if custom_property_name not in selected_object:
        print(f"The selected object does not have the custom property '{custom_property_name}'.")
    else:
        # Create a new text object
        bpy.ops.object.text_add()
        text_object = bpy.context.active_object
        text_object.name = "Custom_Text"
        
        # Create a custom property on the text object for the driver
        text_object["_RNA_UI"] = {"Custom_Driver_Property": {"description": "Custom Driver Property", "default": 0.0, "min": 0.0, "max": 1.0}}
        text_object["Custom_Driver_Property"] = 0.0
        
        # Set the custom property to the value of the custom property of the selected object
        text_object["Custom_Driver_Property"] = selected_object[custom_property_name]
        
        # Access the driver of the custom property and set its expression
        driver = text_object.driver_add("['Custom_Driver_Property']")
        driver.expression = f'\"{selected_object[custom_property_name]}\"'
$\endgroup$
5
  • 1
    $\begingroup$ Hello. what's the error ? $\endgroup$
    – Gorgious
    Feb 20 at 16:09
  • $\begingroup$ Blender vanilla text objects contents can't be driven because it doesn't support animation. You'll have to use special callbacks for that $\endgroup$
    – Gorgious
    Feb 21 at 9:21
  • $\begingroup$ Thanks Gorgeous, I will have a look at 'special callbacks'. I'm a big fan of yours in the OSArch discussion group! $\endgroup$
    – Bedson
    Feb 21 at 9:23
  • $\begingroup$ Hehe cheers. You can look into property group and / or StringProperty (docs.blender.org/api/current/…) which you can add an update callback to and tweak the text in there $\endgroup$
    – Gorgious
    Feb 21 at 10:32
  • $\begingroup$ Yeah, I'll take a look at the StringProperty. My latest attempt went mad and nearly crashed Blender. It added a LOT of Text objects rather than just one. $\endgroup$
    – Bedson
    Feb 21 at 10:34

1 Answer 1

2
$\begingroup$

Here's a proposal based on your explanation :

  • Define a custom prop on bpy.types.Object.
  • Add a callback to its update method, that will update a children text object's body and create one if it doesn't exist yet.
  • Since this is a API defined property you can't modify it using the UI anymore so define a new panel to display the prop.

enter image description here

import bpy


def update_text(self, context):
    obj = self
    for text_obj in obj.children:
        if text_obj.type == "FONT":
            text = text_obj.data
            break
    else:  # No text children yet
        text = bpy.data.curves.new(name=obj.name + "_text", type="FONT")
        text_obj = bpy.data.objects.new(object_data=text, name=obj.name + "_text")
        text_obj.parent = obj
        text_obj.matrix_world = obj.matrix_world
        obj.users_collection[0].objects.link(text_obj)
        text_obj.show_in_front = True

    text.body = self.my_string_property


class CustomPanel(bpy.types.Panel):
    bl_label = "Custom Panel"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

    def draw(self, context):
        self.layout.prop(context.object, "my_string_property")


bpy.utils.register_class(CustomPanel)
bpy.types.Object.my_string_property = bpy.props.StringProperty(name="My String Property", update=update_text)
$\endgroup$
3
  • $\begingroup$ That is amazing! Now I need to work out how to get it to show in a DXF made with the PRJ addon. Great work Gorgious! $\endgroup$
    – Bedson
    Feb 21 at 12:16
  • $\begingroup$ Cheers. It will be converted to a mesh object on export so it won't export to an Autocad Text object but will show up as a bunch of polylines. In my experience it will lead to artifacts when zoomed out if you plan on printing it. Depends on your end goal $\endgroup$
    – Gorgious
    Feb 21 at 12:30
  • $\begingroup$ What is your process for creating technical drawings? (cheeky question - apologies) I've been trying all sorts of addons (TechSec, Measurable etc) and methods - from exporting to DXF to using Blender's compositor (one camera with a different position per Frame). $\endgroup$
    – Bedson
    Feb 21 at 12:37

You must log in to answer this question.

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