4
$\begingroup$

I have run into an issue where I need to edit all text curves to change their content, but I also need to be able to use other curves that are not text within the scene.

If I try and use the standard for x in y method and cycle through all curves I get the error I expected.

import bpy

curves = bpy.data.curves

for c in curves:
  print(c.data.body)
  c.data.body = 'this has worked'

'Curve' object has no attribute 'data'

This is caused by non-text curve objects in the scene, which of course have no text attributes. What are my options to work around this limitation of having text objects bundled in with all curve objects?

Adding text object definitions manually is not an option as this needs to be extremely flexible.

$\endgroup$
4
  • 2
    $\begingroup$ The error above isn't caused by the curve not being a TextCurve. The curve is the data property of a curve object. There is no data attribute of a curve. ie should be print(c.body) Then you will need to check for TextCurve. $\endgroup$
    – batFINGER
    Apr 14, 2018 at 3:22
  • $\begingroup$ @batFINGER How you usually address a single text curve's text content is with bpy.data.curves['CurveName'].data.body which is why it was written the way it is. Thanks for the clarification. $\endgroup$
    – J Sargent
    Apr 14, 2018 at 15:29
  • $\begingroup$ Change to bpy.data.objects['TextName"].data.body and I'd agree. Only objects have the data prop that can be a curve, in bpy.data.curves, a mesh in bpy.data.meshes etc. $\endgroup$
    – batFINGER
    Apr 14, 2018 at 16:24
  • $\begingroup$ @batFINGER I think I see where my confusion first arose, thanks $\endgroup$
    – J Sargent
    Apr 14, 2018 at 16:33

1 Answer 1

5
$\begingroup$

Edited, thanks.

import bpy

curves = bpy.data.curves

for c in curves:
    if isinstance (c, bpy.types.TextCurve):
        print(c.body)
        c.body = 'this works in better style'
$\endgroup$
3
  • $\begingroup$ Good idea, I'll wait to see if there is a better method but this looks like it would work well if not. Thanks! $\endgroup$
    – J Sargent
    Apr 13, 2018 at 23:29
  • 4
    $\begingroup$ I don't know about Blender's internals, but for Python it's typically better to use isinstance(obj, class_or_classes) instead of type(obj) is cls so that subclasses are supported as well. $\endgroup$ Apr 14, 2018 at 2:07
  • 1
    $\begingroup$ Could use hasattr(c, "font") or hasattr(c, "body") $\endgroup$
    – batFINGER
    Apr 14, 2018 at 3:17

You must log in to answer this question.

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