Raydium 3D Game Engine

Official forum for everything about Raydium, ManiaDrive, MeMak, ...
It is currently Thu Mar 28, 2024 10:11 pm

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Blender .tri import
PostPosted: Sat Oct 18, 2008 2:11 pm 
Offline

Joined: Sun Oct 09, 2005 10:46 pm
Posts: 759
Hello,

It's a try to support raydium multitexturing import with blender.
This is a new import script.
It have been quite all rewritten, and indent are Tabs and no more mixed between tabs and space (code was quite unreadable with blender font).

It is possible to specify several texture on a mesh using several uvtexture in mesh properties:
Image
Uv texture name are not important, during import they are defined to the name of the image file.

Blender did not show the multitextuing effect in the 3D view. But you can swap texture visibility using
Image

The code is a bit slow (for cocorobrix.tri at least).

Please try it and don't hesitate to report bugs or enhancement.

It allows to render texture and ambiant occlusion (after this one has been baked to a new texture).
I'll detail the steps further because this is wip.

Code:
#!BPY

"""
Name: 'Raydium Import (.tri format)'
blender: 2.31
Group: 'Import'
Tooltip: 'Import a .tri'
"""
#import rpdb2;
#rpdb2.start_embedded_debugger("test",True)

try:
   import nt
   os=nt
   os.sep='\\'
except:
   import posix
   os=posix
   os.sep='/'

import sys
import Blender
from Blender import Mesh

# This script will include .tri files (Raydium mesh files)
# Do not supports multitexturing yet.
# Python puxxor :)

def fileExists(f):
   try:
      file = open(f)
   except IOError:
      exists = 0
   else:
      exists = 1
   return exists

class source:
   def __init__(self,filename):
      self.filename=filename
      self.file=open(filename,"r")

   def readFaces(self):
      
      lines = self.file.readlines();
      #p,f = os.path.split(self.filename)
      #p,f = Blender.sys.splitext(self.filename)
      p = Blender.sys.dirname(self.filename)
      #f = Blender.sys.basename(self.filename)
      os.chdir(p)
      print "CWD is now %s" % (p)
      scene=Blender.Scene.GetCurrent()
      mesh = Blender.Mesh.New()
      
      words = lines[0].split()
      if len(words) <>1 :
         print "Not a raydium .tri file (first line must be 0 or 1)"
         return -1
      version = float(words[0])
      if version <0 or version >1:
         print "Not a supported (0 or 1) file format"
         return -1

      n = 0
      vi = 0
      fi = 0
      ntot = len(lines)-1
      naff = ntot /20
      ncour = 0
      
      lfilename =[]
      ltexname =["tex01","tex02","tex03"]
      
      for line in lines[1:]:
         ncour=ncour+1
         if (ncour >= naff):
            print (vi*100/ntot)+1
            ncour=0
         if (n==3):
            n=0
            vi = vi+3
            fi = fi+1         
         if (n==0):
            mesh.verts.extend(0,0,0)
            mesh.verts.extend(0,0,0)
            mesh.verts.extend(0,0,0)
            mesh.faces.extend(vi,vi+1,vi+2)
            face=mesh.faces[fi]
                     
         words = line.split()
         
         vx = float(words[0])
         vy = float(words[1])
         vz = float(words[2])
         
         if version==1:
            bu = float(words[6])
            bv = float(words[7])
            texname=words[8]
         if version==0:
            bu = float(words[3])
            bv = float(words[4])
            texname=words[5]
            
         mesh.verts[vi+n].co[0] = vx
         mesh.verts[vi+n].co[1] = vy
         mesh.verts[vi+n].co[2] = vz
         
         if texname[0:4] == "rgb(":
            temp=texname[4:-1].split(")")
            texname=temp[0]
            textured = 0
            couls=texname.split(",")
            r= float(couls[0])*255
            g= float(couls[1])*255
            b= float(couls[2])*255
            try:
               mesh.activeColorLayer = "rgb"
            except:
               mesh.addColorLayer("rgb")
            mesh.activeColorLayer = "rgb"
            
            face.col[n].r=int(r)
            face.col[n].g=int(g)
            face.col[n].b=int(b)
            face.col[n].a=255
            
         else:
            filename = texname.split(";")
            i=0
            for file in filename:
               param = file.split("|")
               if (len(param)==1):
                  texname = param[0]
               if (len(param)==3):
                  texname = param[2]
                  bu = float(param[0]) ; bv = float(param[1])
                  
                  
               try:
                  mesh.activeUVLayer = ltexname[i]
               except:
                  mesh.addUVLayer(ltexname[i])
                  
               if fileExists(texname) == 0:
                  print "Cannot access to %s" % (texname)               
               
               mesh.activeUVLayer = ltexname[i]
               face.image = Blender.Image.Load(texname)
               face.mode |= Blender.Mesh.FaceModes['TEX']
               face.mode |= Blender.Mesh.FaceModes['TWOSIDE']
               face.uv[n][0] = bu
               face.uv[n][1] = bv
               i=i+1
      
         n = n + 1

      scn = Blender.Scene.GetCurrent()
      ob = scn.objects.new(mesh)
      ob.setLocation(0,0,0)
      Blender.Redraw()
      return 0
      ob = Blender.Object.New("Mesh")
      ob.link(mesh)
      mesh.name = "tri"
      print "end."
      return 0
      

   def close(self):
         #self.file.flush()
         self.file.close()

def importTRI(filename):
   if filename.find('.tri', -4) <= 0: filename += '.tri'
   obj=source(filename)
   ret = obj.readFaces()
   obj.close
   if ret>0:
      print "Imported."
   else:
      Blender.Draw.PupMenu("Bad File type not imported See console.")

def filesel_callback(filename):
  test = importTRI(filename)

Blender.Window.FileSelector(filesel_callback, 'Import TRI','*.tri')
#importTRI("test1.tri")


Waiting for feedback.

Have a nice day
Ouille

P.S.: Is there a blender pro that can detail steps to show this multitexturing directly under blender ?
I try adding a lamp and a camera, but scene is not always textured.
Combining the textures using node editor is a solution but too complex !
Help welcome.

Edit: petite correction du script.


Top
 Profile  
 
 Post subject: Re: Blender .tri import
PostPosted: Sun Oct 19, 2008 5:44 pm 
Offline
User avatar

Joined: Sun Mar 16, 2003 2:53 am
Posts: 2591
Location: gnniiiii (Scrat)
Nice topic !

- Here, phpBB or Firefox (3) adds four space character before each line in a [ code ] bloc when I paste it ... Anything based on indentation is then broken (python scripts, diff files, ...). I was able to fix this with an ugly
Code:
sed -i 's/^    //' triIMP-B2-45.py

... but I could be a good idea to find a correct solution :) Do you have this problem too ?

- The script is quiet slow, you're right ! :) Do you think there's some room for optimization ?

- Original texture name is not used into Blender ... Your screenshot seems to show this is not true, but in my case, "layers" were named texXXX. It's not very important now, but just wondering ...

- Importing "output.tri" shows some troubles ... Perhaps the TGA is only upside down, but I think that more a trouble of "multiple textures" for one layer ... A tri file can be build like this:

Code:
1
x y z nx ny nz u1 v1 tablecafe.tga;u2|v2|output.tri.0.tga
x y z nx ny nz u1 v1 tablecafe.tga;u2|v2|output.tri.0.tga
x y z nx ny nz u1 v1 tablecafe.tga;u2|v2|output.tri.0.tga
x y z nx ny nz u1 v1 tablecafe.tga;u2|v2|output.tri.1.tga
x y z nx ny nz u1 v1 tablecafe.tga;u2|v2|output.tri.1.tga
x y z nx ny nz u1 v1 tablecafe.tga;u2|v2|output.tri.1.tga
...

(note that the second "layer" use 2 different textures)
Did the script is supposed to handle that ?


Top
 Profile  
 
 Post subject: Re: Blender .tri import
PostPosted: Sun Oct 19, 2008 5:58 pm 
Offline

Joined: Sun Oct 09, 2005 10:46 pm
Posts: 759
Hello,

I'll very for the four space. Next time i'll put the file on ftp serveur. perhaps we need to get code for small pieces of code.

Script is slow because vertex and face are directly added to the mesh (this is not recommended) . For information this script use only mesh and not nmesh wich is deprecated.
I don't manage to create face object before adding it to the mesh, i never program with python so it's a lots of test and try !

I'll try to optimise it if it's ok for everybody.

My screenshoot was for the first version of the script. But i create as many uvlayer as texture file, or in fact i need only one uvlayer by level of multitexturing. This part of code is now ok but need to be cleaned a bit.

It seem's that your tga files are upside down, it's raydium problem, not blender. File import and uv read are correct (the same as previous script), see my other post about tga flip there is the clue.

Each level of multitexturing can handle as many texture as needed it's not a limitation.

Bugs ?

have a nice day.

Ouille.


Top
 Profile  
 
 Post subject: Re: Blender .tri import
PostPosted: Sun Oct 19, 2008 8:05 pm 
Offline
User avatar

Joined: Sun Mar 16, 2003 2:53 am
Posts: 2591
Location: gnniiiii (Scrat)
Quote:
Each level of multitexturing can handle as many texture as needed

Ok, so it's a TGA flip trouble, then.

To help following all this, the other part of the discussion is here : viewtopic.php?f=7&t=886


Top
 Profile  
 
 Post subject: Re: Blender .tri import
PostPosted: Sat Oct 25, 2008 9:09 pm 
Offline

Joined: Sun Oct 09, 2005 10:46 pm
Posts: 759
Hello,

This is the last version on blender .tri import script:
http://ftp.cqfd-corp.org/triIMP-B2-31.zip

It is now as fast as light :D

Read Multitextuing
No more (future) compatibility problem with blender (NMESH is deprecated).

It's a quite complete rewrite so can some of you make some tests before commit ?

Have a nice day.
Ouille.


Top
 Profile  
 
 Post subject: Re: Blender .tri import
PostPosted: Sun Oct 26, 2008 3:45 pm 
Offline
User avatar

Joined: Sun Mar 16, 2003 2:53 am
Posts: 2591
Location: gnniiiii (Scrat)
Tested with a few tri files, seems pretty much perfect to me ;) The only trouble I have is with "cinemal.tri", failing with an "IndexError: list index out of range" at the end of loading. Faulty model, perhaps ?


Top
 Profile  
 
 Post subject: Re: Blender .tri import
PostPosted: Sat Nov 01, 2008 6:05 pm 
Offline

Joined: Sun Oct 09, 2005 10:46 pm
Posts: 759
Hello,

Ok it was an other problem, version 0 tri file.

I commit the corrected script, can be reverted if needed.

Please report bugs.

Have a nice day.
Ouille


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 18 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group