problem regarding drawing a line.

Hello, Im a beginner of panda3D, I want to draw a line on the screen, but everytime I got nothing on the screen, here is my code:

import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from random import randint, random
from pandac.PandaModules import LineSegs
from direct.showbase.ShowBase import ShowBase

class MyApp(DirectObject):

def __init__(self):
    d=self.Draw()
    d.drawLine((0,0,0), (550,550,550))

class Draw(LineSegs):

    def __init__(self):
        LineSegs.__init__(self)

    def drawLine(self,startPoint,endPoint,color=None,thickness=None):            
        if color is None: color = (100,100,100,100)
        if thickness is None: thickness = 1                    
        self.setColor(*color)
        self.setThickness(thickness)
        self.moveTo(startPoint)
        self.drawTo(endPoint)

run()

I totally have no idea where I do wrong, is there anyone can kindly give me some guide, thanks a lot!

first of all, welcome to panda 3d.
when you paste code, feel free to use the [ code] tags. otherwise indentation will be lost(which is critical for most codes)

in panda all colors range between 0 and 1 (since panda supports more than just 8 bit colors-scales everything is managed as float values)

for your problem: you create a line, but you did not attach it to your scene-graph. so it would not show up.

[Motion path & line classes)
in this thread you can find a small class which draws lines (amongst others)

EDIT: just noticed i created lines a tad different in that thread. but you still need to wrap your line in a nodepath and reparent it to render (or some other node in the scenegraph)

:smiley:

I modified the code, but still got nothing. Am I doing wrong?

import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from random import randint, random
from pandac.PandaModules import LineSegs
from direct.showbase.ShowBase import ShowBase 

class MyApp(DirectObject):
 
    def __init__(self):
        d=self.Draw()
        d.drawLine((0,0,0), (550,550,550))
        node = d.create()
        self.np = NodePath(node)
        self.np.reparentTo(render)
    class Draw(LineSegs):

        def __init__(self):
            LineSegs.__init__(self)

        def drawLine(self,startPoint,endPoint,color=None,thickness=None):            
            if color is None: color = (0.5,1,1,1)
            if thickness is None: thickness = 1                    
            self.setColor(*color)
            self.setThickness(thickness)
            self.moveTo(startPoint)
            self.drawTo(endPoint)
 
run()

have you tried to zoom out the camera? drawing a line from 0 to some point in space (which may be behind the camera) is not garantueed to pop up a line within the camera’s field of view.

yes, I did, and it’s an empty grey area, and I can’t see anything. Is there some basic mistake of my code? Thnaks.

You never instantiate a MyApp object, so most of that code never gets run.

David

I add the two line of code:
app = MyApp()
app.run()

but it say MyApp() has no attribute ‘run’

Is this the way to instantiate it?

I finally work it out, thank you guys! Thanks a lot.