Topic: Blender to .mod scripts are there any working?  (Read 32994 times)

0 Members and 1 Guest are viewing this topic.

Offline Bonk

  • Commodore
  • *
  • Posts: 13298
  • You don't have to live like a refugee.
Re: Blender to .mod scripts are there any working?
« Reply #120 on: November 09, 2010, 03:38:38 pm »
Right now the arrays (lists) are pissing me off. Why is it that every language has to have their own queer implementation of arrays? Drives me nuts.  :banghead:

Offline marstone

  • Because I can
  • Commander
  • *
  • Posts: 3014
  • Gender: Male
  • G.E.C.K. - The best kit to have
    • Ramblings on the Q3, blog
Re: Blender to .mod scripts are there any working?
« Reply #121 on: November 09, 2010, 03:46:38 pm »
Right now the arrays (lists) are pissing me off. Why is it that every language has to have their own queer implementation of arrays? Drives me nuts.  :banghead:

Makes the writers of the language feel manly redoing everything for no good reason. :crazy2:
The smell of printer ink in the morning,
Tis the smell of programming.

Offline Bonk

  • Commodore
  • *
  • Posts: 13298
  • You don't have to live like a refugee.
Re: Blender to .mod scripts are there any working?
« Reply #122 on: November 09, 2010, 05:01:01 pm »
What's killing me now is the meta-syntax of the python documentation. I just can't see how it translates in most cases to actual usable python code.

I'm trying to break up the bytearray of null terminated strings more efficiently than unpacking character by character as you have, but it is starting to look like that is the only way.

At least Python has authoritative online documentation. The lack of that for Javascript is maddening. I think I'm going to have to spend some time decoding the Python documentation. Perhaps they explain their meta-syntax in its introduction...

Starting to burn out for today...

Offline marstone

  • Because I can
  • Commander
  • *
  • Posts: 3014
  • Gender: Male
  • G.E.C.K. - The best kit to have
    • Ramblings on the Q3, blog
Re: Blender to .mod scripts are there any working?
« Reply #123 on: November 09, 2010, 05:13:41 pm »
remember that Python is named after Monty Python.  So logic and simplicity may not always apply.
The smell of printer ink in the morning,
Tis the smell of programming.

Offline Bonk

  • Commodore
  • *
  • Posts: 13298
  • You don't have to live like a refugee.
Re: Blender to .mod scripts are there any working?
« Reply #124 on: November 09, 2010, 08:38:46 pm »
Got past a few blocks, hit a few more. I should have the whole file parsed by the end of tomorrow. (hit an issue with vertices, and then need to finish polys and looping throgh the LOD count.) Then it is just a matter of building the model. Simple right? ;)

Offline marstone

  • Because I can
  • Commander
  • *
  • Posts: 3014
  • Gender: Male
  • G.E.C.K. - The best kit to have
    • Ramblings on the Q3, blog
Re: Blender to .mod scripts are there any working?
« Reply #125 on: November 09, 2010, 08:50:37 pm »
polys are fun.  They store normals in there also. Maybe, the normals might be on the verts, can't remember.
The smell of printer ink in the morning,
Tis the smell of programming.

Offline Bonk

  • Commodore
  • *
  • Posts: 13298
  • You don't have to live like a refugee.
Re: Blender to .mod scripts are there any working?
« Reply #126 on: November 09, 2010, 08:57:40 pm »
Ya, face normals are stored:
Code: [Select]
struct tFaceDef
{
int vtxCount;
int materialNdx;
tPoint3 normal;
struct tFlatPolyVertex
{
unsigned short vtxNdx;
unsigned short flags;
tPoint3 normal;
float u;
float v;
float light;
}*vertices;
tFaceDef() : vertices(NULL) {}
~tFaceDef() { if ( vertices ) { delete [] vertices; } }
};

So I conclude the vertex normals displayed in the model viewer are calculated. edit: no wait ... i see both normals are stored in the face structs.

I begin to fear the math of the export...  :o

Offline FoaS_XC

  • Photorps, Sammiches, woot woot.
  • Global Moderator
  • Commander
  • *
  • Posts: 4571
  • Gender: Male
    • Robinomicon
Re: Blender to .mod scripts are there any working?
« Reply #127 on: November 09, 2010, 09:20:08 pm »
Typically, IIRC, vertex's and faces each have their own normals.
Robinomicon
"When I was 5 years old, my mom always told me that happiness was the key to life. When I went to school, they asked me what I wanted to be when I grew up. I wrote down “happy.” They told me I didn’t understand the assignment and I told them they didn’t understand life."

Offline Bonk

  • Commodore
  • *
  • Posts: 13298
  • You don't have to live like a refugee.
Re: Blender to .mod scripts are there any working?
« Reply #128 on: November 10, 2010, 05:19:02 am »
Yeah, they are both stored in the face struct. I find it odd that normals should be stored when they can be calculated... perhaps too much to calc them on the fly? Why does the game engine need them anyway? (naieve question?) Perhaps they are just there strictly as a modeling tool? (really easy to see misalignments with them displayed)

Offline marstone

  • Because I can
  • Commander
  • *
  • Posts: 3014
  • Gender: Male
  • G.E.C.K. - The best kit to have
    • Ramblings on the Q3, blog
Re: Blender to .mod scripts are there any working?
« Reply #129 on: November 10, 2010, 07:01:12 am »
I believe the game engine would need it for shading. Also, was probably saved as the target machines for the game were low end and didn't need a 3D card. So would help not bog down the cpu.
The smell of printer ink in the morning,
Tis the smell of programming.

Offline FoaS_XC

  • Photorps, Sammiches, woot woot.
  • Global Moderator
  • Commander
  • *
  • Posts: 4571
  • Gender: Male
    • Robinomicon
Re: Blender to .mod scripts are there any working?
« Reply #130 on: November 10, 2010, 08:34:54 am »
Normals can be altered from their usual vectors, but rarely ever are.
Robinomicon
"When I was 5 years old, my mom always told me that happiness was the key to life. When I went to school, they asked me what I wanted to be when I grew up. I wrote down “happy.” They told me I didn’t understand the assignment and I told them they didn’t understand life."

Offline Bonk

  • Commodore
  • *
  • Posts: 13298
  • You don't have to live like a refugee.
Re: Blender to .mod scripts are there any working?
« Reply #131 on: November 10, 2010, 08:43:15 am »
But.. then... it wouldn't be a normal would it?  ;D

Offline FoaS_XC

  • Photorps, Sammiches, woot woot.
  • Global Moderator
  • Commander
  • *
  • Posts: 4571
  • Gender: Male
    • Robinomicon
Re: Blender to .mod scripts are there any working?
« Reply #132 on: November 10, 2010, 12:58:35 pm »
this model is suffering from abnormal normals?
Robinomicon
"When I was 5 years old, my mom always told me that happiness was the key to life. When I went to school, they asked me what I wanted to be when I grew up. I wrote down “happy.” They told me I didn’t understand the assignment and I told them they didn’t understand life."

Offline Bonk

  • Commodore
  • *
  • Posts: 13298
  • You don't have to live like a refugee.
Re: Blender to .mod scripts are there any working?
« Reply #133 on: November 11, 2010, 04:49:31 am »
Not even groan worthy...  ;)

Actually though, I've gotten stuck on the vertices, they just aren't adding up on the stock fca for me. The first and last values don't "add up". Might be time for that "model model". Though I'll have to be able to handle this regardless.

Offline marstone

  • Because I can
  • Commander
  • *
  • Posts: 3014
  • Gender: Male
  • G.E.C.K. - The best kit to have
    • Ramblings on the Q3, blog
Re: Blender to .mod scripts are there any working?
« Reply #134 on: November 11, 2010, 06:29:40 am »
What isn't "adding up" about the verts for yeah?
The smell of printer ink in the morning,
Tis the smell of programming.

Offline Bonk

  • Commodore
  • *
  • Posts: 13298
  • You don't have to live like a refugee.
Re: Blender to .mod scripts are there any working?
« Reply #135 on: November 11, 2010, 07:29:40 am »
I'm probably missing something dumb... but the first value makes no sense and I have one spare one at the end that also makes no sense... perhaps some screenshots and my current unpacking code will illustrate...
Code: [Select]
bl_addon_info= {
    "name": "Import StarFleet Command Models",
    "author": "Bonk",
    "version": (0, 1),
    "blender": (2, 5, 5),
    "api": 31847,
    "location": "File > Import > StarFleet Command Model (.mod)",
    "description": "Imports a StarFleet Command model file.",
    "warning": "The universe may implode at any moment.",
    "wiki_url": "http://www.dynaverse.net/wiki/index.php?title=Blender:SFC_Models",
    "category": "Import/Export"}

import os
import io
import time
import struct
import chunk
# import array

import bpy
import math
import mathutils

def decodeAsc(byteStr):
    print (byteStr.decode("ascii"))

def import_mod(filename,context):
                   
    print (filename)
   
    name, ext= os.path.splitext(os.path.basename(filename))
    file= open(filename, 'rb')

    # Header
    try:
        modHdrTag, \
        modHdrFileSize, \
        modHdrVersion, \
        modHdrRadius, \
        modHdrTotalLODs, \
        modHdrLODHyst \
        = struct.unpack("<4siIfif", file.read(24))
        print ("modHdrTag: " + modHdrTag.decode("ascii"))
        print ("modHdrFileSize: %d" % modHdrFileSize)
        print ("modHdrVersion: %d" % modHdrVersion)
        print ("modHdrRadius: %f" % modHdrRadius)
        print ("modHdrTotalLODs: %d" % modHdrTotalLODs)
        print ("modHdrLODHyst: %f " % modHdrLODHyst)
        modHdrLODTrans \
        = struct.unpack("<%df" % modHdrLODHyst, file.read(int(modHdrLODHyst)*4))
        print ("modHdrLODTrans:")
        print (modHdrLODTrans)     
    except:
        print("Error parsing file header. Boo!")
        file.close()
        return
   
    # Strings
    modStrTag, \
    modStrSize \
    = struct.unpack("<4si", file.read(8))
    modStrSizePadded = 4*(math.ceil(modStrSize/4))
    print ("modStrTag: " + modStrTag.decode("ascii"))
    print ("modStrSize: %d" % modStrSize)
    print ("modStrSizePadded: %d" % modStrSizePadded)
    modStrPool \
    = struct.unpack("<%ds" % modStrSizePadded, file.read(modStrSizePadded))
    modStrStrings = modStrPool[0].split(b'\x00')
    modStrList = [x.decode("ascii") for x in modStrStrings]
    print ("modStrList: ")
    print (modStrList)
   
    # Named Points
    modPtsTag, \
    modPtsSize \
    = struct.unpack("<4si", file.read(8))
    modPtsStart = file.tell()
    print ("modPtsTag: " + modPtsTag.decode("ascii"))
    print ("modPtsSize: %d" % modPtsSize)
    modPtsNum = modPtsSize / 16
    print ("modPtsNum: %d" % modPtsNum)
    modPts = list()
    while (file.tell() - modPtsStart < modPtsSize):
        modPts.append(struct.unpack("<fffi", file.read(16)))
    print ("modPts:")
    print (modPts)
   
    # Materials (aieee!)
    modMtsTag, \
    modMtsSize \
    = struct.unpack("<4si", file.read(8))
    modMtsStart = file.tell()   
    print ("modMtsTag: " + modMtsTag.decode("ascii"))
    print ("modMtsSize: %d" % modMtsSize)
    modMtsFlags = list()
    modMts = list()     
    while (file.tell() - modMtsStart < modMtsSize):
        modMtsFlags.append(struct.unpack("<i", file.read(4)))
        if modMtsFlags[-1][0] > 20:
            modMts.append(struct.unpack("<Ifffiiii", file.read(32)))
        else:
            modMts.append(struct.unpack("<Ifffii", file.read(24)))
    print ("modMtsFlags:")
    print (modMtsFlags)
    print ("modMts:")
    print (modMts) 

    # Geometries
    modModTag, \
    modModSize, \
    modModVerticesNum, \
    modModFacesNum \
    = struct.unpack("<4siii", file.read(16))
    print ("modModTag: " + modModTag.decode("ascii"))
    print ("modModSize: %d" % modModSize)   
    print ("modModVerticesNum: %d" % modModVerticesNum)   
    print ("modModFacesNum: %d" % modModFacesNum) 
   
    modVtxTag \
    = struct.unpack("<4s", file.read(4))
    modVtxStart = file.tell() 
    print ("modVtxTag: " + modVtxTag[0].decode("ascii"))
    # print (modVtxTag)
   
    modVtx = list()
    while (file.tell() - modVtxStart < modModVerticesNum*12):
        modVtx.append(struct.unpack("<fff", file.read(12)))
    print ("modVtx: ")
    print (modVtx)
   
    modPlyTag, \
    modPlySize, \
    modPlyVtxCount, \
    modPlyMtlIdx, \
    = struct.unpack("<4siii", file.read(16))
    print ("modPlyTag :")
    print (modPlyTag)
    print ("modPlySize :")
    print (modPlySize)
    print ("modPlyVtxCount :")
    print (modPlyVtxCount)
    print ("modPlyMtlIdx :")
    print (modPlyMtlIdx)


from bpy.props import *

class IMPORT_OT_mod(bpy.types.Operator):
    '''Import MOD Operator.'''
    bl_idname= "import.mod"
    bl_label= "Import MOD"
    bl_description= "Import a StarFleet Command model."
    bl_options= {'REGISTER', 'UNDO'}

    filepath= StringProperty(name="File Path", description="Filepath used for importing the model file", maxlen=1024, default="")

    def execute(self, context):
        import_mod(self.filepath,
                 context)
        return {'FINISHED'}

    def invoke(self, context, event):
        wm= context.window_manager
        wm.add_fileselect(self)
        return {'RUNNING_MODAL'}


def menu_func(self, context):
    self.layout.operator(IMPORT_OT_mod.bl_idname, text="StarFleet Command Model (.mod)")

def register():
    bpy.types.INFO_MT_file_import.append(menu_func)

def unregister():
    bpy.types.INFO_MT_file_import.remove(menu_func)

if __name__ == "__main__":
    register()

I'm missing something... as far as I can tell the vertex block is just a set of tPoint3s (fff)...
« Last Edit: November 11, 2010, 07:45:28 am by Bonk »

Offline FoaS_XC

  • Photorps, Sammiches, woot woot.
  • Global Moderator
  • Commander
  • *
  • Posts: 4571
  • Gender: Male
    • Robinomicon
Re: Blender to .mod scripts are there any working?
« Reply #136 on: November 11, 2010, 08:56:36 am »
I'm a little out of my depth here, but I think that first value is a near zero. We get that sometimes in 3D programs where the program doesn't return a perfect zero but returns an extremely small float that might as well be zero. Not sure if that helps, though.

When I get home from work today (which, im actually late for now >.<) I will create a particular box object for you with known dimensions and stuff.
Robinomicon
"When I was 5 years old, my mom always told me that happiness was the key to life. When I went to school, they asked me what I wanted to be when I grew up. I wrote down “happy.” They told me I didn’t understand the assignment and I told them they didn’t understand life."

Offline Bonk

  • Commodore
  • *
  • Posts: 13298
  • You don't have to live like a refugee.
Re: Blender to .mod scripts are there any working?
« Reply #137 on: November 11, 2010, 09:11:49 am »
I found the dumb error, I skipped the VTXS block size int. (reading it as the first float of the coords) :smackhead: I probably did so because it is totally redundant, knowing the number of vertices and the size of their struct.

The last value still leaves me wondering (-1.9390653040624603e-15), but I suspected it may have meaning... probably is just a point near zero on that axis.

On to the polies.
« Last Edit: November 11, 2010, 09:22:49 am by Bonk »

Offline marstone

  • Because I can
  • Commander
  • *
  • Posts: 3014
  • Gender: Male
  • G.E.C.K. - The best kit to have
    • Ramblings on the Q3, blog
Re: Blender to .mod scripts are there any working?
« Reply #138 on: November 11, 2010, 09:19:22 am »
hmm, first how much of a header on the vertex are you pulling?  It only has the name VTX0 and a size. IIRC.
The smell of printer ink in the morning,
Tis the smell of programming.

Offline marstone

  • Because I can
  • Commander
  • *
  • Posts: 3014
  • Gender: Male
  • G.E.C.K. - The best kit to have
    • Ramblings on the Q3, blog
Re: Blender to .mod scripts are there any working?
« Reply #139 on: November 11, 2010, 09:21:29 am »
I don't remember an extra bit of data at the end of verts, it was just the sets of verts.
The smell of printer ink in the morning,
Tis the smell of programming.