3
$\begingroup$

So I have the labels set using the answer here

How to add text in blender using python

import bpy

tech_list = ["TechA", "TechB", "TechC", "TechD"]
for tech in tech_list:
    bpy.data.curves.new(type="FONT", name=tech).body = tech
    label = bpy.data.objects.new(name=("label"+str(tech_list.index(tech))),\ 
                                 object_data=bpy.data.curves[tech])
    bpy.context.scene.collection.objects.link(label)

But there is no way I can get these objects rotated 90*, the bpy.ops.rotate only works in edit mode on meshes and the labels are curves, I tried to convert to meshes but I get into infinite loop of trial and failure.

Would you be able to point me in the right direction?

$\endgroup$

1 Answer 1

2
$\begingroup$

The easiest way seems to use object.rotation_euler for this.

Note that Python always sets rotation in Radians. To convert degrees to radians you can use

Math Module

import math
degrees = 90  # example
radians = math.radians(degrees)   
degrees = math.degrees(radians)  
print(radians, degrees)      # return: 1.5707963267948966 90.0 

Formula

degrees = 180  # example
pi = 3.141592653589793
radians = degrees * (pi / 180)
degrees = radians * (180 / pi)
print(radians, degrees)      # return: 3.141592653589793 180.0

Script

import bpy
degrees = 90
pi = 3.141592653589793
radians = degrees * (pi / 180)

tech_list = ["TechA", "TechB", "TechC", "TechD"]
for tech in tech_list:
    bpy.data.curves.new(type="FONT", name=tech).body = tech
    label = bpy.data.objects.new(name=("label" + str(tech_list.index(tech))) , object_data=bpy.data.curves[tech])
    bpy.context.scene.collection.objects.link(label)
    label.rotation_euler[0] = radians   # index 0 = Rotation X = +90°
$\endgroup$
4
  • $\begingroup$ works OK, and how would you move it? I thought one had to change the text to a mesh $\endgroup$
    – frank
    Dec 26, 2021 at 10:44
  • 1
    $\begingroup$ No need to convert as long as you do not want to manipulate the mesh of the text object. And no matter if text or mesh object, you get access via bpy.data.objects[ID]. To move eg bpy.data.objects['label2'].location[0] = -5.0 $\endgroup$
    – relaxed
    Dec 26, 2021 at 11:05
  • $\begingroup$ thank you so much for your replies, there is just one thing I do not really get: if you can manipulate all data through the bpy.data interface, what is the use of the bpy.ops then? $\endgroup$
    – frank
    Dec 26, 2021 at 14:56
  • 1
    $\begingroup$ For this I recommend an oldie but goldie post: blender.stackexchange.com/questions/2848/why-avoid-bpy-ops $\endgroup$
    – relaxed
    Dec 26, 2021 at 16:43

You must log in to answer this question.

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