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

Last Built: Mar 06, 2020

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

Implementation

class pyffi.formats.cgf.CgfFormat[source]

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 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.

exception CgfError[source]

Bases: Exception

Exception for CGF specific errors.

class Chunk(template=None, argument=None, parent=None)[source]

Bases: pyffi.formats.cgf._Chunk, object

apply_scale(scale)[source]

Apply scale factor on data.

tree(block_type=None, follow_all=True)[source]

A generator for parsing all blocks in the tree (starting from and including C{self}).

Parameters
  • block_type – If not None, yield only blocks of the type C{block_type}.

  • follow_all – If C{block_type} is not None, then if this is True the function will parse the whole tree. Otherwise, the function will not follow branches that start by a non-C{block_type} block.

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

Bases: pyffi.object_models.xml.struct_.StructBase

A CGF chunk header.

class ChunkTable(template=None, argument=None, parent=None)[source]

Bases: pyffi.formats.cgf._ChunkTable, object

get_chunk_types()[source]

Iterate all chunk types (in the form of Python classes) referenced in this table.

class ChunkType(**kwargs)

Bases: pyffi.object_models.xml.enum.EnumBase

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

class ChunkVersion(**kwargs)

Bases: pyffi.object_models.common.UInt

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

class Data(filetype=4294901760, game='Far Cry')[source]

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_detail_child_names(edge_filter=(True, True))[source]

Generator which yields all child names of this item in the detail view.

Override this method if the node has children.

Returns

Generator for detail tree child names.

Return type

generator yielding strs

get_detail_child_nodes(edge_filter=(True, True))[source]

Generator which yields all children of this item in the detail view (by default, all acyclic and active ones).

Override this method if the node has children.

Parameters

edge_filter (EdgeFilter or type(None)) – The edge type to include.

Returns

Generator for detail tree child nodes.

Return type

generator yielding DetailNodes

get_global_child_nodes(edge_filter=(True, True))[source]

Returns chunks without parent.

inspect(stream)[source]

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)[source]

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)[source]

Read a cgf file. Does not reset stream position.

Parameters

stream (file) – The stream from which to read.

replace_global_node(oldbranch, newbranch, edge_filter=(True, True))[source]

Replace a particular branch in the graph.

update_versions()[source]

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

write(stream)[source]

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 DataStreamChunk(template=None, argument=None, parent=None)[source]

Bases: pyffi.formats.cgf._DataStreamChunk, object

apply_scale(scale)[source]

Apply scale factor on data.

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

Bases: pyffi.formats.cgf.Chunk

Export information.

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

Bases: pyffi.object_models.xml.struct_.StructBase

R32G32B32 (float).

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

Bases: pyffi.object_models.xml.struct_.StructBase

A mesh face.

class FileOffset(**kwargs)

Bases: pyffi.object_models.common.Int

Points to a position in a file.

class FileSignature(**kwargs)[source]

Bases: pyffi.object_models.xml.basic.BasicBase

The CryTek file signature with which every cgf file starts.

get_hash(data=None)[source]

Return a hash value for the signature.

Returns

An immutable object that can be used as a hash.

get_size(data=None)[source]

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

Returns

Number of bytes.

get_value()[source]

Get signature.

Returns

The signature.

read(stream, data)[source]

Read signature from stream.

Parameters

stream (file) – The stream to read from.

set_value(value)[source]

Not implemented.

write(stream, data)[source]

Write signature to stream.

Parameters

stream (file) – The stream to read from.

class FileType(**kwargs)

Bases: pyffi.object_models.xml.enum.EnumBase

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

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

Bases: pyffi.object_models.xml.struct_.StructBase

Obsolete, not decoded.

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

Bases: pyffi.object_models.xml.struct_.StructBase

The CGF header.

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

Bases: pyffi.object_models.xml.struct_.StructBase

R8G8B8.

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

Bases: pyffi.object_models.xml.struct_.StructBase

R8G8B8A8.

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

Bases: pyffi.object_models.xml.struct_.StructBase

A bone initial position matrix.

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

Bases: pyffi.object_models.xml.struct_.StructBase

Obsolete, not decoded.

class Matrix33(template=None, argument=None, parent=None)[source]

Bases: pyffi.formats.cgf._Matrix33, object

as_list()[source]

Return matrix as 3x3 list.

as_tuple()[source]

Return matrix as 3x3 tuple.

get_copy()[source]

Return a copy of the matrix.

get_determinant()[source]

Return determinant.

get_inverse()[source]

Get inverse (assuming is_scale_rotation is true!).

get_scale()[source]

Gets the scale (assuming is_scale_rotation is true!).

get_scale_quat()[source]

Decompose matrix into scale and quaternion.

get_scale_rotation()[source]

Decompose the matrix into scale and rotation, where scale is a float and rotation is a C{Matrix33}. Returns a pair (scale, rotation).

get_transpose()[source]

Get transposed of the matrix.

is_identity()[source]

Return True if the matrix is close to identity.

is_rotation()[source]

Returns True if the matrix is a rotation matrix (a member of SO(3)).

is_scale_rotation()[source]

Returns true if the matrix decomposes nicely into scale * rotation.

set_identity()[source]

Set to identity matrix.

set_scale_rotation(scale, rotation)[source]

Compose the matrix as the product of scale * rotation.

class Matrix44(template=None, argument=None, parent=None)[source]

Bases: pyffi.formats.cgf._Matrix44, object

as_list()[source]

Return matrix as 4x4 list.

as_tuple()[source]

Return matrix as 4x4 tuple.

get_copy()[source]

Create a copy of the matrix.

get_inverse(fast=True)[source]

Calculates inverse (fast assumes is_scale_rotation_translation is True).

get_matrix_33()[source]

Returns upper left 3x3 part.

get_translation()[source]

Returns lower left 1x3 part.

is_identity()[source]

Return True if the matrix is close to identity.

set_identity()[source]

Set to identity matrix.

set_matrix_33(m)[source]

Sets upper left 3x3 part.

set_rows(row0, row1, row2, row3)[source]

Set matrix from rows.

set_translation(translation)[source]

Returns lower left 1x3 part.

sup_norm()[source]

Calculate supremum norm of matrix (maximum absolute value of all entries).

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

Bases: pyffi.object_models.xml.struct_.StructBase

Obsolete, not decoded.

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

Bases: pyffi.object_models.xml.struct_.StructBase

Obsolete, not decoded.

class Ptr(**kwargs)[source]

Bases: pyffi.formats.cgf.Ref

Reference to a chunk, down the hierarchy.

get_refs(data=None)[source]

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

Returns

C{[]}

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

Bases: pyffi.object_models.xml.struct_.StructBase

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

class Ref(**kwargs)[source]

Bases: pyffi.object_models.xml.basic.BasicBase

Reference to a chunk, up the hierarchy.

Resolve chunk index into a chunk.

Keyword Arguments

block_dct – Dictionary mapping block index to block.

get_hash(data=None)[source]

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)[source]

Return the chunk reference.

Returns

Empty list if no reference, or single item list containing the reference.

get_size(data=None)[source]

Return number of bytes this type occupies in a file.

Returns

Number of bytes.

get_value()[source]

Get chunk being referred to.

Returns

The chunk being referred to.

read(stream, data)[source]

Read chunk index.

Parameters

stream (file) – The stream to read from.

set_value(value)[source]

Set chunk reference.

Parameters

value (L{CgfFormat.Chunk}) – The value to assign.

write(stream, data)[source]

Write chunk index.

Parameters

stream (file) – The stream to write to.

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

Bases: pyffi.object_models.xml.struct_.StructBase

Not decoded. Nowhere used?

class 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.

String

alias of pyffi.object_models.common.ZString

class String128(**kwargs)[source]

Bases: pyffi.object_models.common.FixedString

String of fixed length 128.

class String16(**kwargs)[source]

Bases: pyffi.object_models.common.FixedString

String of fixed length 16.

class String256(**kwargs)[source]

Bases: pyffi.object_models.common.FixedString

String of fixed length 256.

class String32(**kwargs)[source]

Bases: pyffi.object_models.common.FixedString

String of fixed length 32.

class String64(**kwargs)[source]

Bases: pyffi.object_models.common.FixedString

String of fixed length 64.

class 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.

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

Bases: pyffi.object_models.xml.struct_.StructBase

Texture coordinate.

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

Bases: pyffi.object_models.xml.struct_.StructBase

A texture face (vertex indices).

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

Bases: pyffi.formats.cgf.Chunk

Unknown. An extra block written by the XSI exporter.

class Vector3(template=None, argument=None, parent=None)[source]

Bases: pyffi.formats.cgf._Vector3, object

bool

alias of pyffi.object_models.common.Bool

byte

alias of pyffi.object_models.common.Byte

char

alias of pyffi.object_models.common.Char

float

alias of pyffi.object_models.common.Float

int

alias of pyffi.object_models.common.Int

short

alias of pyffi.object_models.common.Short

ubyte

alias of pyffi.object_models.common.UByte

uint

alias of pyffi.object_models.common.UInt

ushort

alias of pyffi.object_models.common.UShort

static version_number(version_str)[source]

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
>>> from os.path import dirname
>>> dirpath = __file__
>>> for i in range(4): #recurse up to root repo dir
...     dirpath = dirname(dirpath)
>>> repo_root = dirpath
>>> format_root = os.path.join(repo_root, 'tests', 'formats', 'cgf')
>>> stream = open(os.path.join(format_root, '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 '...SourceInfoChunk'> instance at ...
* source_file : <None>
* date : Fri Sep 28 22:40:44 2007
* author : blender@BLENDER

<class '...TimingChunk'> instance at ...
* secs_per_tick : 0.0002083333...
* ticks_per_frame : 160
* global_range :
    <class '...RangeEntity'> instance at ...
    * name : GlobalRange
    * start : 0
    * end : 100
* num_sub_ranges : 0

Parse all CGF files in a directory tree

>>> for stream, data in CgfFormat.walkData(format_root):
...     try:
...         # the replace call makes the doctest also pass on windows
...         os_path = stream.name
...         split = (os_path.split(os.sep))[-4:]
...         rejoin = os.path.join(*split).replace(os.sep, "/")
...         print("reading %s" % rejoin)
...         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)
reading tests/formats/cgf/invalid.cgf
Warning: read failed due corrupt file, corrupt format description, or bug.
0
reading tests/formats/cgf/monkey.cgf
14
reading tests/formats/cgf/test.cgf
2
reading tests/formats/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...

<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...