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.
-
class
BoneLink
(template=None, argument=None, parent=None)¶ Bases:
pyffi.object_models.xml.struct_.StructBase
A bone link.
-
class
Chunk
(template=None, argument=None, parent=None)[source]¶ Bases:
pyffi.formats.cgf._Chunk
,object
-
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 isTrue
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
-
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
-
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
str
s
-
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
ortype(None)
) – The edge type to include.- Returns
Generator for detail tree child nodes.
- Return type
generator yielding
DetailNode
s
-
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.
-
class
DataStreamChunk
(template=None, argument=None, parent=None)[source]¶ Bases:
pyffi.formats.cgf._DataStreamChunk
,object
-
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.
-
-
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
-
class
Matrix44
(template=None, argument=None, parent=None)[source]¶ Bases:
pyffi.formats.cgf._Matrix44
,object
-
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
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.
-
fix_links
(data)[source]¶ 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.
-
get_links
(data=None)[source]¶ 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.
-
-
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.
-
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
-
class
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...