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
<p>Which is: this line <strong>scene.collection.objects.link(font_obj)</strong></p>
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
w1=[]
font_obj=[]
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)
else:
scene = bpy.context.scene
scene.collection.objects.link(font_obj)
scene.collection.objects.link(font_obj2)
def recalculate_text(scene):
font_curve.body = 'Variable1 : ' + str(w1)
font_curve2.body = 'Variable2 : ' + str(scene.frame_current+3)
def register():
bpy.app.handlers.frame_change_post.append(recalculate_text)
def unregister():
bpy.app.handlers.frame_change_post.remove(recalculate_text)
register()
font_obj = []Setting to a list, and then check ifNonewill never be True. instead change tofont_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 asNoneHowever they are Both boolean False) to a collection. Might find this of interest https://blender.stackexchange.com/questions/161103/handler-script-updates-in-viewport-but-not-in-render – batFINGER Jan 31 '20 at 13:51line 29, in recalculate_text NameError: name 'font_curve' is not definedbut it's already defined when the object is created? Also line 29 is a blank line. – Rick T Jan 31 '20 at 15:23font_curve = font_obj.dataYour logic is totally snarfed here. A lot of the code above IMO should be in the handler code not outside. This will makefont_curvelocal to the handler method. Unless (for example)font_curveis defined as aglobal(which you really don't want to do) the handler doesn't know who the outsider is. Check out the link posted, especially howtextis passed to one function to return a handler method with thattextas a local. If you are new to python recommend searching on "python NameError" or similar at stackoverflow – batFINGER Jan 31 '20 at 15:33