I sometimes notice inexplicable panda behavior that I don’t understand.
For example, create the following code:
from panda3d.core import NodePath, TextNode
from direct.showbase.ShowBase import ShowBase
class MyApp(ShowBase):
def __init__(self):
ShowBase.__init__(self)
font = loader.loadFont('arial.ttf')
label = TextNode('')
label.set_font(font)
label.set_text("Bags")
label.set_text_color((1, 0, 0, 1))
label.setAlign(TextNode.ACenter)
node_label = NodePath(label.generate())
node_label.reparentTo(aspect2d, 1)
app = MyApp()
app.run()
And you will see that the text is offset from the center. To verify this add node_label.showBounds ()
from panda3d.core import NodePath, TextNode
from direct.showbase.ShowBase import ShowBase
class MyApp(ShowBase):
def __init__(self):
ShowBase.__init__(self)
font = loader.loadFont('arial.ttf')
label = TextNode('')
label.set_font(font)
label.set_text("Bags")
label.set_text_color((1, 0, 0, 1))
label.setAlign(TextNode.ACenter)
node_label = NodePath(label.generate())
node_label.showBounds()
node_label.reparentTo(aspect2d, 1)
app = MyApp()
app.run()
Now we can clearly see the center of the sphere and the text, however, where does the offset on the render aspect2d node come from? To solve this problem, I make such a fix.
from panda3d.core import NodePath, TextNode
from direct.showbase.ShowBase import ShowBase
class MyApp(ShowBase):
def __init__(self):
ShowBase.__init__(self)
font = loader.loadFont('arial.ttf')
label = TextNode('')
label.set_font(font)
label.set_text("Bags")
label.set_text_color((1, 0, 0, 1))
label.setAlign(TextNode.ACenter)
node_label = NodePath(label.generate())
node_label.showBounds()
size = node_label.getBounds()
center = size.getCenter()
node_label.set_pos(-center)
node_label.reparentTo(aspect2d, 1)
app = MyApp()
app.run()
Accordingly, the question arises, why does the panda move the text from the center? After all, it is more logical by default that the text should be centered. But it is strange that no one pays attention to it, it seems that this is one of the problems for me only.