#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    14
    Rep Power
    0

    Exclamation how to program in python??


    Hai to all,
    i want to know, how we can load a .3ds or any other 3D objects using python in our program
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    Bimingham,AL
    Posts
    68
    Rep Power
    11
    Can you explain in detail please ?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    14
    Rep Power
    0

    Exclamation


    Originally Posted by wx_uab
    Can you explain in detail please ?
    Hello,
    I have attached the python program and the bunny.3ds file with this. The program is giving error as follows

    Traceback (most recent call last):
    File "E:\Python23\shownormals.py", line 105, in ?
    load("bunny.3ds")
    NameError: name 'load' is not defined






    #shownormals.py
    def showVertices(mesh):
    """Draw markers at the vertex positions of a triangle mesh.
    """

    # Vertices are stored in the local coordinate system of the geometry.
    # But as we need the coordinates relative to the world coordinate
    # system we have to obtain the transformation L that transforms from
    # local to world coordinates.
    L = mesh.worldtransform

    # Iterate over the vertices and draw a marker at the global vertex
    # position L*v.
    for v in mesh.verts:
    drawMarker(L*v, size=0.07, color=(0,0,1))


    def showNormals(mesh):
    """Draw the normals as lines.

    A normal is drawn at every vertex position. The normals may either be
    specified as "varying normal N" or as "user normal N"/"uniform int Nfaces[3]".
    The actual drawing code is in showVaryingNormals() and showUserNormals().
    """

    # Check if there are normals available in this mesh
    desc = mesh.findVariable("N")
    if desc==None:
    print 'Mesh "%s" has no normals set.'%mesh.name
    return

    # Unpack the variable descriptor
    name, storage_class, type, mult = desc

    # Are the normals stored as "varying normal N"?
    if storage_class==VARYING and type==NORMAL and mult==1:
    showVaryingNormals(mesh)

    # Or are they stored as "user normal N"?
    elif storage_class==USER and type==NORMAL and mult==1:
    # Check if the normal faces are available
    desc = mesh.findVariable("Nfaces")
    if desc==None:
    print 'Mesh "%s" has no normal faces set.'%mesh.name
    else:
    name, storage_class, type, mult = desc
    if storage_class==UNIFORM and type==INT and mult==3:
    # "N" and "Nfaces" is available and of the right type...
    showUserNormals(mesh)
    else:
    print 'Mesh "%s": Invalid declaration of "Nfaces"'%mesh.name

    else:
    print 'Mesh "%s": Invalid declaration of variable "N".'%mesh.name


    def showVaryingNormals(mesh):
    """Draw normals given as "varying normal N".
    """
    # Get the world transform to transform the local vertices and normals
    # into the world coordinate system
    L = mesh.worldtransform
    L0 = L*vec3(0)
    # Obtain the slot carrying the normals
    N = mesh.geom.slot("N")
    # Iterate over the faces and draw the vertices and normals
    for i,j,k in mesh.faces:
    a = L*mesh.verts[i]
    b = L*mesh.verts[j]
    c = L*mesh.verts[k]
    na = L*N[i]-L0
    nb = L*N[j]-L0
    nc = L*N[k]-L0
    drawLine(a, a+na)
    drawLine(b, b+nb)
    drawLine(c, c+nc)


    def showUserNormals(mesh):
    """Draw normals given as "user normal N"/"uniform int Nfaces[3]".
    """
    # Get the world transform to transform the local vertices and normals
    # into the world coordinate system
    L = mesh.worldtransform
    L0 = L*vec3(0)
    # Obtain the slot carrying the normals and the normal faces
    N = mesh.geom.slot("N")
    Nfaces = mesh.geom.slot("Nfaces")
    # Iterate overt the faces and normal faces...
    for face, Nface in zip(mesh.faces, Nfaces):
    i,j,k = face
    ni,nj,nk = Nface
    a = L*mesh.verts[i]
    b = L*mesh.verts[j]
    c = L*mesh.verts[k]
    na = L*N[ni]-L0
    nb = L*N[nj]-L0
    nc = L*N[nk]-L0
    drawLine(a, a+na)
    drawLine(b, b+nb)
    drawLine(c, c+nc)

    ######################################################################

    # Load a scene file
    load("bunny.3ds")

    # Show its contents
    listWorld()

    # Draw the vertices & normals of every triangle mesh in the scene
    for obj in scene.walkWorld():
    # Check if the object is a triangle mesh
    if isinstance(obj.geom, TriMeshGeom):
    # Uncomment the following line to convert the normals from
    # "user normal" to "varying normal"
    # obj.geom = convMeshAttr(obj.geom, "N")
    print 'Creating vertex markers for "%s"...'%obj.name
    showVertices(obj)
    print 'Creating normal lines for "%s"...'%obj.name
    showNormals(obj)
    Attached Files
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    I feel like I'm just repeating the traceback here, but your code has the line:

    Code:
    # Load a scene file
    load("bunny.3ds")
    and you haven't defined a load() function, so Python can't run that line...



    file() or open() read from files.

    There is no implicit way of reading and parsing a 3D object, but you might be using a third party module...?
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Two modules used for 3D programming in Python are PyOpenGL and VPython. Which one would suit you best I'm not sure but here are links to to the two projects .

    http://pyopengl.sourceforge.net/
    http://vpython.org/

    To a lesser extent there is also Pygame however this is more for writing interactive games and interfaces etc. and I haven't seen it used much for 3D stuff.

    http://www.pygame.org/

    You should also read the sticky thread at the top of this from regarding how to ask a question; particularly the use of CODE tags when posting [Python] code in the forum.

    Hope this helps,

    Mark .
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    Bimingham,AL
    Posts
    68
    Rep Power
    11
    If you are planning to write a full fledged 3d game , I would veto against using pyopengl( as it is very slow compared to opengl) . You would be better off writing the grapichs stuff in opengl ( in C++ ) and " swig"ing the code to python and use python for all other stuff.

IMN logo majestic logo threadwatch logo seochat tools logo