pyffi.formats.cgf — Crytek (.cgf and .cga)

Implementation

class pyffi.formats.cgf.CgfFormat

Bases: pyffi.object_models.xml.FileFormat

Stores all information about the cgf file format.

class AbstractMtlChunk(template=None, argument=None, parent=None)

Bases: pyffi.formats.cgf.Chunk

Common parent for MtlChunk and MtlNameChunk.

class CgfFormat.AbstractObjectChunk(template=None, argument=None, parent=None)

Bases: pyffi.formats.cgf.Chunk

Common parent for HelperChunk and MeshChunk.

Bases: pyffi.object_models.xml.struct_.StructBase

A bone link.

blending

Vertex weight.

bone

The bone chunk.

offset

The bone offset?

exception CgfFormat.CgfError

Bases: exceptions.Exception

Exception for CGF specific errors.

class CgfFormat.ChunkHeader(template=None, argument=None, parent=None)

Bases: pyffi.object_models.xml.struct_.StructBase

A CGF chunk header.

id

The chunk identifier.

offset

Position of the chunk in the CGF file.

type

Type of chunk referred to.

version

Version of the chunk referred to.

class CgfFormat.ChunkType(**kwargs)

Bases: pyffi.object_models.xml.enum.EnumBase

An unsigned 32-bit integer, describing the chunk type.

class CgfFormat.ChunkVersion(**kwargs)

Bases: pyffi.object_models.common.UInt

The version of a particular chunk, or the version of the chunk table.

class CgfFormat.Data(filetype=4294901760, game='Far Cry')

Bases: pyffi.object_models.Data

A class to contain the actual cgf data.

Note that L{versions} and L{chunk_table} are not automatically kept in sync with the L{chunks}, but they are resynchronized when calling L{write}.

Variables:
  • game – The cgf game.
  • header – The cgf header.
  • chunks – List of chunks (the actual data).
  • versions – List of chunk versions.
get_global_child_nodes(edge_filter=(True, True))

Returns chunks without parent.

inspect(stream)

Quickly checks whether the stream appears to contain cgf data, and read the cgf header and chunk table. Resets stream to original position.

Call this function if you only need to inspect the header and chunk table.

Parameters:stream (file) – The file to inspect.
inspect_version_only(stream)

This function checks the version only, and is faster than the usual inspect function (which reads the full chunk table). Sets the L{header} and L{game} instance variables if the stream contains a valid cgf file.

Call this function if you simply wish to check that a file is a cgf file without having to parse even the header.

Raises ValueError:
 If the stream does not contain a cgf file.
Parameters:stream (file) – The stream from which to read.
read(stream)

Read a cgf file. Does not reset stream position.

Parameters:stream (file) – The stream from which to read.
update_versions()

Update L{versions} for the given chunks and game.

write(stream)

Write a cgf file. The L{header} and L{chunk_table} are recalculated from L{chunks}. Returns number of padding bytes written (this is for debugging purposes only).

Parameters:stream (file) – The stream to which to write.
Returns:Number of padding bytes written.
class CgfFormat.ExportFlagsChunk(template=None, argument=None, parent=None)

Bases: pyffi.formats.cgf.Chunk

Export information.

class CgfFormat.FRGB(template=None, argument=None, parent=None)

Bases: pyffi.object_models.xml.struct_.StructBase

R32G32B32 (float).

class CgfFormat.Face(template=None, argument=None, parent=None)

Bases: pyffi.object_models.xml.struct_.StructBase

A mesh face.

material

Material index.

sm_group

Smoothing group.

v_0

First vertex index.

v_1

Second vertex index.

v_2

Third vertex index.

class CgfFormat.FileOffset(**kwargs)

Bases: pyffi.object_models.common.Int

Points to a position in a file.

class CgfFormat.FileSignature(**kwargs)

Bases: pyffi.object_models.xml.basic.BasicBase

The CryTek file signature with which every cgf file starts.

get_hash(data=None)

Return a hash value for the signature.

Returns:An immutable object that can be used as a hash.
get_size(data=None)

Return number of bytes that the signature occupies in a file.

Returns:Number of bytes.
get_value()

Get signature.

Returns:The signature.
read(stream, data)

Read signature from stream.

Parameters:stream (file) – The stream to read from.
set_value(value)

Not implemented.

write(stream, data)

Write signature to stream.

Parameters:stream (file) – The stream to read from.
class CgfFormat.FileType(**kwargs)

Bases: pyffi.object_models.xml.enum.EnumBase

An unsigned 32-bit integer, describing the file type.

class CgfFormat.GeomNameListChunk(template=None, argument=None, parent=None)

Bases: pyffi.object_models.xml.struct_.StructBase

Obsolete, not decoded.

class CgfFormat.Header(template=None, argument=None, parent=None)

Bases: pyffi.object_models.xml.struct_.StructBase

The CGF header.

offset

Position of the chunk table in the CGF file.

signature

The CGF file signature.

type

The CGF file type (geometry or animation).

version

The version of the chunk table.

class CgfFormat.IRGB(template=None, argument=None, parent=None)

Bases: pyffi.object_models.xml.struct_.StructBase

R8G8B8.

class CgfFormat.IRGBA(template=None, argument=None, parent=None)

Bases: pyffi.object_models.xml.struct_.StructBase

R8G8B8A8.

class CgfFormat.InitialPosMatrix(template=None, argument=None, parent=None)

Bases: pyffi.object_models.xml.struct_.StructBase

A bone initial position matrix.

class CgfFormat.MRMChunk(template=None, argument=None, parent=None)

Bases: pyffi.object_models.xml.struct_.StructBase

Obsolete, not decoded.

class CgfFormat.MtlListChunk(template=None, argument=None, parent=None)

Bases: pyffi.object_models.xml.struct_.StructBase

Obsolete, not decoded.

class CgfFormat.PatchMeshChunk(template=None, argument=None, parent=None)

Bases: pyffi.object_models.xml.struct_.StructBase

Obsolete, not decoded.

class CgfFormat.Ptr(**kwargs)

Bases: pyffi.formats.cgf.Ref

Reference to a chunk, down the hierarchy.

get_refs(data=None)

Ptr does not point down, so get_refs returns empty list.

Returns:C{[]}
class CgfFormat.Quat(template=None, argument=None, parent=None)

Bases: pyffi.object_models.xml.struct_.StructBase

A quaternion (x,y,z,w).

w

Fourth coordinate.

x

First coordinate.

y

Second coordinate.

z

Third coordinate.

class CgfFormat.Ref(**kwargs)

Bases: pyffi.object_models.xml.basic.BasicBase

Reference to a chunk, up the hierarchy.

Resolve chunk index into a chunk.

Parameters:block_dct (dict) – Dictionary mapping block index to block.
get_hash(data=None)

Return a hash value for the chunk referred to.

Returns:An immutable object that can be used as a hash.

Return the chunk reference.

Returns:Empty list if no reference, or single item list containing the reference.
get_refs(data=None)

Return the chunk reference.

Returns:Empty list if no reference, or single item list containing the reference.
get_size(data=None)

Return number of bytes this type occupies in a file.

Returns:Number of bytes.
get_value()

Get chunk being referred to.

Returns:The chunk being referred to.
read(stream, data)

Read chunk index.

Parameters:stream (file) – The stream to read from.
set_value(value)

Set chunk reference.

Parameters:value (L{CgfFormat.Chunk}) – The value to assign.
write(stream, data)

Write chunk index.

Parameters:stream (file) – The stream to write to.
class CgfFormat.ScenePropsChunk(template=None, argument=None, parent=None)

Bases: pyffi.object_models.xml.struct_.StructBase

Not decoded. Nowhere used?

class CgfFormat.SizedString(**kwargs)

Bases: pyffi.object_models.xml.basic.BasicBase, pyffi.object_models.editable.EditableLineEdit

Basic type for strings. The type starts with an unsigned int which describes the length of the string.

>>> from tempfile import TemporaryFile
>>> f = TemporaryFile()
>>> from pyffi.object_models import FileFormat
>>> data = FileFormat.Data()
>>> s = SizedString()
>>> if f.write('\x07\x00\x00\x00abcdefg'.encode("ascii")): pass # ignore result for py3k
>>> if f.seek(0): pass # ignore result for py3k
>>> s.read(f, data)
>>> str(s)
'abcdefg'
>>> if f.seek(0): pass # ignore result for py3k
>>> s.set_value('Hi There')
>>> s.write(f, data)
>>> if f.seek(0): pass # ignore result for py3k
>>> m = SizedString()
>>> m.read(f, data)
>>> str(m)
'Hi There'
get_hash(data=None)

Return a hash value for this string.

Returns:An immutable object that can be used as a hash.
get_size(data=None)

Return number of bytes this type occupies in a file.

Returns:Number of bytes.
get_value()

Return the string.

Returns:The stored string.
read(stream, data)

Read string from stream.

Parameters:stream (file) – The stream to read from.
set_value(value)

Set string to C{value}.

Parameters:value (str) – The value to assign.
write(stream, data)

Write string to stream.

Parameters:stream (file) – The stream to write to.
CgfFormat.String

alias of ZString

class CgfFormat.String128(**kwargs)

Bases: pyffi.object_models.common.FixedString

String of fixed length 128.

class CgfFormat.String16(**kwargs)

Bases: pyffi.object_models.common.FixedString

String of fixed length 16.

class CgfFormat.String256(**kwargs)

Bases: pyffi.object_models.common.FixedString

String of fixed length 256.

class CgfFormat.String32(**kwargs)

Bases: pyffi.object_models.common.FixedString

String of fixed length 32.

class CgfFormat.String64(**kwargs)

Bases: pyffi.object_models.common.FixedString

String of fixed length 64.

class CgfFormat.Tangent(template=None, argument=None, parent=None)

Bases: pyffi.object_models.xml.struct_.StructBase

Tangents. Divide each component by 32767 to get the actual value.

w

Handness? Either 32767 (+1.0) or -32767 (-1.0).

class CgfFormat.UV(template=None, argument=None, parent=None)

Bases: pyffi.object_models.xml.struct_.StructBase

Texture coordinate.

class CgfFormat.UVFace(template=None, argument=None, parent=None)

Bases: pyffi.object_models.xml.struct_.StructBase

A texture face (vertex indices).

t_0

First vertex index.

t_1

Second vertex index.

t_2

Third vertex index.

class CgfFormat.UnknownAAFC0005Chunk(template=None, argument=None, parent=None)

Bases: pyffi.formats.cgf.Chunk

Unknown. An extra block written by the XSI exporter.

CgfFormat.bool

alias of Bool

CgfFormat.byte

alias of Byte

CgfFormat.char

alias of Char

CgfFormat.float

alias of Float

CgfFormat.int

alias of Int

CgfFormat.short

alias of Short

CgfFormat.ubyte

alias of UByte

CgfFormat.uint

alias of UInt

CgfFormat.ushort

alias of UShort

static CgfFormat.version_number(version_str)

Converts version string into an integer.

Parameters:version_str (str) – The version string.
Returns:A version integer.
>>> hex(CgfFormat.version_number('744'))
'0x744'

Regression tests

Read a CGF file

>>> # get file version and file type, and read cgf file
>>> stream = open('tests/cgf/test.cgf', 'rb')
>>> data = CgfFormat.Data()
>>> # read chunk table only
>>> data.inspect(stream)
>>> # check chunk types
>>> list(chunktype.__name__ for chunktype in data.chunk_table.get_chunk_types())
['SourceInfoChunk', 'TimingChunk']
>>> data.chunks # no chunks yet
[]
>>> # read full file
>>> data.read(stream)
>>> # get all chunks
>>> for chunk in data.chunks:
...     print(chunk) 
<class 'pyffi.formats.cgf.SourceInfoChunk'> instance at ...
* source_file : <None>
* date : Fri Sep 28 22:40:44 2007
* author : blender@BLENDER
<BLANKLINE>
<class 'pyffi.formats.cgf.TimingChunk'> instance at ...
* secs_per_tick : 0.000208333338378
* ticks_per_frame : 160
* global_range :
    <class 'pyffi.formats.cgf.RangeEntity'> instance at ...
    * name : GlobalRange
    * start : 0
    * end : 100
* num_sub_ranges : 0
<BLANKLINE>

Parse all CGF files in a directory tree

>>> for stream, data in CgfFormat.walkData('tests/cgf'):
...     print(stream.name)
...     try:
...         data.read(stream)
...     except Exception:
...         print("Warning: read failed due corrupt file, corrupt format description, or bug.")
...     print(len(data.chunks))
...     # do something with the chunks
...     for chunk in data.chunks:
...         chunk.apply_scale(2.0)
tests/cgf/invalid.cgf
Warning: read failed due corrupt file, corrupt format description, or bug.
0
tests/cgf/monkey.cgf
14
tests/cgf/test.cgf
2
tests/cgf/vcols.cgf
6

Create a CGF file from scratch

>>> from pyffi.formats.cgf import CgfFormat
>>> node1 = CgfFormat.NodeChunk()
>>> node1.name = "hello"
>>> node2 = CgfFormat.NodeChunk()
>>> node1.num_children = 1
>>> node1.children.update_size()
>>> node1.children[0] = node2
>>> node2.name = "world"
>>> from tempfile import TemporaryFile
>>> stream = TemporaryFile()
>>> data = CgfFormat.Data() # default is far cry
>>> data.chunks = [node1, node2]
>>> # note: write returns number of padding bytes
>>> data.write(stream)
0
>>> # py3k returns 0 on seek; this hack removes return code from doctest
>>> if stream.seek(0): pass
>>> data.inspect_version_only(stream)
>>> hex(data.header.version)
'0x744'
>>> data.read(stream)
>>> # get all chunks
>>> for chunk in data.chunks:
...     print(chunk) 
<class 'pyffi.formats.cgf.NodeChunk'> instance at 0x...
* name : hello
* object : None
* parent : None
* num_children : 1
* material : None
* is_group_head : False
* is_group_member : False
* reserved_1 :
    <class 'pyffi.object_models.xml.array.Array'> instance at 0x...
    0: 0
    1: 0
* transform :
    [  0.000  0.000  0.000  0.000 ]
    [  0.000  0.000  0.000  0.000 ]
    [  0.000  0.000  0.000  0.000 ]
    [  0.000  0.000  0.000  0.000 ]
* pos : [  0.000  0.000  0.000 ]
* rot :
    <class 'pyffi.formats.cgf.Quat'> instance at 0x...
    * x : 0.0
    * y : 0.0
    * z : 0.0
    * w : 0.0
* scl : [  0.000  0.000  0.000 ]
* pos_ctrl : None
* rot_ctrl : None
* scl_ctrl : None
* property_string : <None>
* children :
    <class 'pyffi.object_models.xml.array.Array'> instance at 0x...
    0: <class 'pyffi.formats.cgf.NodeChunk'> instance at 0x...
<BLANKLINE>
<class 'pyffi.formats.cgf.NodeChunk'> instance at 0x...
* name : world
* object : None
* parent : None
* num_children : 0
* material : None
* is_group_head : False
* is_group_member : False
* reserved_1 :
    <class 'pyffi.object_models.xml.array.Array'> instance at 0x...
    0: 0
    1: 0
* transform :
    [  0.000  0.000  0.000  0.000 ]
    [  0.000  0.000  0.000  0.000 ]
    [  0.000  0.000  0.000  0.000 ]
    [  0.000  0.000  0.000  0.000 ]
* pos : [  0.000  0.000  0.000 ]
* rot :
    <class 'pyffi.formats.cgf.Quat'> instance at 0x...
    * x : 0.0
    * y : 0.0
    * z : 0.0
    * w : 0.0
* scl : [  0.000  0.000  0.000 ]
* pos_ctrl : None
* rot_ctrl : None
* scl_ctrl : None
* property_string : <None>
* children : <class 'pyffi.object_models.xml.array.Array'> instance at 0x...
<BLANKLINE>