Trying to create and check if text objects exist.

My logic is if a specific text object doesn't exist create it, if it does exist update the value using python in Blender 2.81. At the moment nothing gets created. The error is:

Traceback (most recent call last): File "/Text", line 21, in TypeError: CollectionObjects.link(): error with argument 1, "object" - Function.object expected a Object type, not list Error: Python script failed, check the message in the system console

Which is: this line scene.collection.objects.link(font_obj)

This code will be placed in another loop hence I'm using an if / else statement to prevent the text objects from being created multiple times. Trying to alter the working answer code from this question

What I have:

import bpy

if font_obj is None: 
    scene = bpy.context.scene

    font_curve = bpy.data.curves.new(type="FONT",name="Font Curve")
    font_curve.body = 'Test Variable: ' + str(w1) 
    font_obj = bpy.data.objects.new("Font Object w1 var", font_curve)
    font_obj.location = (0, 1, 0)

    font_curve2 = bpy.data.curves.new(type="FONT",name="Font Curve2")
    font_curve2.body = 'Current Frame plus: ' + str(scene.frame_current) 
    font_obj2 = bpy.data.objects.new("Font Object w2 var", font_curve2)
    font_obj2.location = (0, -1, 0)
    font_obj2.scale = (.5, .5, .5)

    scene = bpy.context.scene

    def recalculate_text(scene):
        font_curve.body = 'Variable1 : ' + str(w1) 
        font_curve2.body = 'Variable2 : ' + str(scene.frame_current+3) 

def register():

def unregister():

  • $\begingroup$ Does your code not work? Are you getting errors? Please describe the problem you are facing. $\endgroup$ Jan 31, 2020 at 12:40
  • $\begingroup$ @Ray Mairlot I updated the question to include the link to the working code along with the error code. $\endgroup$
    – Rick T
    Jan 31, 2020 at 12:47
  • $\begingroup$ Change font_obj = [] Setting to a list, and then check if None will never be True. instead change to font_obj = scene.objects.get("Font Object w1 var") (and defining scene prior) This is your error in trying to link an empty list (which isn't same as None However they are Both boolean False) to a collection. Might find this of interest blender.stackexchange.com/questions/161103/… $\endgroup$
    – batFINGER
    Jan 31, 2020 at 13:51
  • $\begingroup$ @batFINGER Thanks! I made those changes it creates the object but I get an error line 29, in recalculate_text NameError: name 'font_curve' is not defined but it's already defined when the object is created? Also line 29 is a blank line. $\endgroup$
    – Rick T
    Jan 31, 2020 at 15:23
  • $\begingroup$ Since you have the object font_curve = font_obj.data Your logic is totally snarfed here. A lot of the code above IMO should be in the handler code not outside. This will make font_curve local to the handler method. Unless (for example) font_curve is defined as a global (which you really don't want to do) the handler doesn't know who the outsider is. Check out the link posted, especially how text is passed to one function to return a handler method with that text as a local. If you are new to python recommend searching on "python NameError" or similar at stackoverflow $\endgroup$
    – batFINGER
    Jan 31, 2020 at 15:33


You must log in to answer this question.

Browse other questions tagged .