how to attach text to a render2d subnode?

Im experimenting with a 2d layout, i want solids, sprites and text to be managed in the render2d for making a gui.

Everything is working fine with the help of some sprite related script that i found in this forum, but when i try to include text inside my setup (self.root or inside a sprite) the text is not showed at all. only if i parent it to render2d or aspect2d is shown.

someting is escaping me…

Could anybody plz explain me why this is not working?

import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import TextureStage,CardMaker,NodePath,TextNode
from random import *
#import functions

##############################################################################
    
class Gui(DirectObject):
  def __init__(self, parent=render):
    #record vars
    self.parent=parent
    self.res=(800,600)#self.parent.parent.screen.res
    #setup sprite interface
    self.root = self.setupScreen(self.res)
    #create a 3d cube in the 2d root
    #self.model=functions.loadModel(self.root,"name","data/models/cube/cube.egg",(1,1,32),(100+16,0,100),(1,0,1,1))
    #create a sprite in the 2d root
    self.sprite=Sprite(self,self.root,"axe",(100+32,100,32,32))
  #===========================================================================
  def setupScreen(self,res):
    parent=render2d
    aspect_ratio = parent.getScale()[0]
    print aspect_ratio
    screenOrigin = parent.attachNewNode('screen_origin')
    screenNode = screenOrigin.attachNewNode('screen_node')
    screenOrigin.setPos(-1.0/aspect_ratio, 0.0, 1.0)
    screenOrigin.setScale(2.0, 1.0, -2.0)
    screenNode.setPos(0, 0, 0)
    screenNode.setScale(1.0/(aspect_ratio*res[0]), 1.0, 1.0/res[1])
    screenNode.setTexScale(TextureStage.getDefault(), 1.0, -1.0)
    return screenNode

############################################################################## 
  
class Sprite():
  def __init__(self,parent,parentNode,name,rect,transparent=1):
    #init vars
    self.parent=parent
    filename="data/tiles/"+name+".png"
    #create card
    cm = CardMaker('spritesMaker')
    cm.setFrame(-0.5, 0.5, -0.5, 0.5)
    #create sprite
    sprite = cm.generate()   
    spriteNP = NodePath(sprite)
    spriteNP.setScale(rect[2], 1.0, rect[3])
    spriteNP.setPos(spriteNP.getSx()/2+rect[0], 0, spriteNP.getSz()/2+rect[1])
    spriteNP.reparentTo(parentNode)
    #texturize
    tex = loader.loadTexture(filename)
    spriteNP.setTexture(tex)
    spriteNP.setTransparency(transparent)
    #add to gui general sprite list
    self.model=spriteNP
    
    #WHY?
    
    #add text to render2d (why this works?)
    text = TextNode('textnode')
    text.setText("This is a Text.")
    textNodePath = render2d.attachNewNode(text)#.generate()
    textNodePath.setScale(.07)
    print textNodePath
    
    #add text to sprite (why this doesnt work?)
    text = TextNode('textnode')
    text.setText("This is a Text.")
    textNodePath = self.model.attachNewNode(text)#.generate()
    textNodePath.setScale(.07)
    print textNodePath
    
    
    
#
if __name__ == '__main__':
  gui=Gui()
  run()

Thanks!

It’s just too small to see. Scale 0.07 is an appropriate scale for render2d (whose range is -1 … 1), but very very tiny under render (whose range is usually closer to -1000 … 1000 or so).

Try removing the setScale() and see if you can find it then.

David

i thought about that and i tryed diferent scales (like 1, 10, 100, 1000, no scaling) but still doesnt show…

Well, maybe it’s facing away from the camera. You can also do textNodePath.setTwoSided(True) to make it visible from both sides. If all else fails, you can do textNodePath.showBounds() to draw a green sphere around where it is supposed to be.

David

Nothing. everything works fine attached to the render2d root but attached to the spritenode is not shown (exist aldough, cause is traced in the console).
Thanks anyway.

I guess i’ll have to manage the text inside the render2d with their own calculations for positioning.
so, is there a way to know the size of the text in screen coordinates once attached to the render2d?

So i did it, and is working quite good :slight_smile:

I just added this Gui example as a downloable snippet on this thread

discourse.panda3d.org/viewtopic.php?t=7360

if somoeone wants to try it.

It’s simple but works quite well in many resolutions, you can toggle fullscreen on/of, includes text and sprites and some key functionality.

Cheers!