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
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.
-
blending
¶ Vertex weight.
-
bone
¶ The bone chunk.
-
offset
¶ The bone offset?
-
-
class
Chunk
(template=None, argument=None, parent=None)¶ Bases:
pyffi.formats.cgf._Chunk
,object
-
apply_scale
(scale)¶ Apply scale factor on data.
-
tree
(block_type=None, follow_all=True)¶ 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.
-
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
ChunkTable
(template=None, argument=None, parent=None)¶ Bases:
pyffi.formats.cgf._ChunkTable
,object
-
get_chunk_types
()¶ 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')¶ 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))¶ 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))¶ 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
-
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.
-
replace_global_node
(oldbranch, newbranch, edge_filter=(True, True))¶ Replace a particular branch in the graph.
-
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
DataStreamChunk
(template=None, argument=None, parent=None)¶ Bases:
pyffi.formats.cgf._DataStreamChunk
,object
-
apply_scale
(scale)¶ 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.
-
material
¶ Material index.
-
sm_group
¶ Smoothing group.
-
v_0
¶ First vertex index.
-
v_1
¶ Second vertex index.
-
v_2
¶ Third vertex index.
-
-
class
FileOffset
(**kwargs)¶ Bases:
pyffi.object_models.common.Int
Points to a position in a file.
-
class
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
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.
-
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
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)¶ Bases:
pyffi.formats.cgf._Matrix33
,object
-
as_list
()¶ Return matrix as 3x3 list.
-
as_tuple
()¶ Return matrix as 3x3 tuple.
-
get_copy
()¶ Return a copy of the matrix.
-
get_determinant
()¶ Return determinant.
-
get_inverse
()¶ Get inverse (assuming is_scale_rotation is true!).
-
get_scale
()¶ Gets the scale (assuming is_scale_rotation is true!).
-
get_scale_quat
()¶ Decompose matrix into scale and quaternion.
-
get_scale_rotation
()¶ 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
()¶ Get transposed of the matrix.
-
is_identity
()¶ Return
True
if the matrix is close to identity.
-
is_rotation
()¶ Returns
True
if the matrix is a rotation matrix (a member of SO(3)).
-
is_scale_rotation
()¶ Returns true if the matrix decomposes nicely into scale * rotation.
-
set_identity
()¶ Set to identity matrix.
-
set_scale_rotation
(scale, rotation)¶ Compose the matrix as the product of scale * rotation.
-
-
class
Matrix44
(template=None, argument=None, parent=None)¶ Bases:
pyffi.formats.cgf._Matrix44
,object
-
as_list
()¶ Return matrix as 4x4 list.
-
as_tuple
()¶ Return matrix as 4x4 tuple.
-
get_copy
()¶ Create a copy of the matrix.
-
get_inverse
(fast=True)¶ Calculates inverse (fast assumes is_scale_rotation_translation is True).
-
get_matrix_33
()¶ Returns upper left 3x3 part.
-
get_translation
()¶ Returns lower left 1x3 part.
-
is_identity
()¶ Return
True
if the matrix is close to identity.
-
set_identity
()¶ Set to identity matrix.
-
set_matrix_33
(m)¶ Sets upper left 3x3 part.
-
set_rows
(row0, row1, row2, row3)¶ Set matrix from rows.
-
set_translation
(translation)¶ Returns lower left 1x3 part.
-
sup_norm
()¶ Calculate supremum norm of matrix (maximum absolute value of all entries).
-
-
class
MeshChunk
(template=None, argument=None, parent=None)¶ Bases:
pyffi.formats.cgf._MeshChunk
,object
-
apply_scale
(scale)¶ Apply scale factor on data.
-
get_colors
()¶ Generator for all vertex colors.
-
get_material_indices
()¶ Generator for all materials (per triangle).
-
get_normals
()¶ Generator for all normals.
-
get_num_triangles
()¶ Get number of triangles.
-
get_triangles
()¶ Generator for all triangles.
-
get_uv_triangles
()¶ Generator for all uv triangles.
-
get_uvs
()¶ Generator for all uv coordinates.
-
get_vertices
()¶ Generator for all vertices.
-
set_geometry
(verticeslist=None, normalslist=None, triangleslist=None, matlist=None, uvslist=None, colorslist=None)¶ Set geometry data.
>>> from pyffi.formats.cgf import CgfFormat >>> chunk = CgfFormat.MeshChunk() >>> vertices1 = [(0,0,0),(0,1,0),(1,0,0),(1,1,0)] >>> vertices2 = [(0,0,1),(0,1,1),(1,0,1),(1,1,1)] >>> normals1 = [(0,0,-1),(0,0,-1),(0,0,-1),(0,0,-1)] >>> normals2 = [(0,0,1),(0,0,1),(0,0,1),(0,0,1)] >>> triangles1 = [(0,1,2),(2,1,3)] >>> triangles2 = [(0,1,2),(2,1,3)] >>> uvs1 = [(0,0),(0,1),(1,0),(1,1)] >>> uvs2 = [(0,0),(0,1),(1,0),(1,1)] >>> colors1 = [(0,1,2,3),(4,5,6,7),(8,9,10,11),(12,13,14,15)] >>> colors_2 = [(50,51,52,53),(54,55,56,57),(58,59,60,61),(62,63,64,65)] >>> chunk.set_geometry(verticeslist = [vertices1, vertices2], ... normalslist = [normals1, normals2], ... triangleslist = [triangles1, triangles2], ... uvslist = [uvs1, uvs2], ... matlist = [2,5], ... colorslist = [colors1, colors_2]) >>> print(chunk) # doctest: +ELLIPSIS +REPORT_UDIFF <class 'pyffi.formats.cgf.MeshChunk'> instance at ... * has_vertex_weights : False * has_vertex_colors : True * in_world_space : False * reserved_1 : 0 * reserved_2 : 0 * flags_1 : 0 * flags_2 : 0 * num_vertices : 8 * num_indices : 12 * num_uvs : 8 * num_faces : 4 * material : None * num_mesh_subsets : 2 * mesh_subsets : <class 'pyffi.formats.cgf.MeshSubsetsChunk'> instance at ... * vert_anim : None * vertices : <class 'pyffi.object_models.xml.array.Array'> instance at ... 0: <class 'pyffi.formats.cgf.Vertex'> instance at ... * p : [ 0.000 0.000 0.000 ] * n : [ 0.000 0.000 -1.000 ] 1: <class 'pyffi.formats.cgf.Vertex'> instance at ... * p : [ 0.000 1.000 0.000 ] * n : [ 0.000 0.000 -1.000 ] 2: <class 'pyffi.formats.cgf.Vertex'> instance at ... * p : [ 1.000 0.000 0.000 ] * n : [ 0.000 0.000 -1.000 ] 3: <class 'pyffi.formats.cgf.Vertex'> instance at ... * p : [ 1.000 1.000 0.000 ] * n : [ 0.000 0.000 -1.000 ] 4: <class 'pyffi.formats.cgf.Vertex'> instance at ... * p : [ 0.000 0.000 1.000 ] * n : [ 0.000 0.000 1.000 ] 5: <class 'pyffi.formats.cgf.Vertex'> instance at ... * p : [ 0.000 1.000 1.000 ] * n : [ 0.000 0.000 1.000 ] 6: <class 'pyffi.formats.cgf.Vertex'> instance at ... * p : [ 1.000 0.000 1.000 ] * n : [ 0.000 0.000 1.000 ] 7: <class 'pyffi.formats.cgf.Vertex'> instance at ... * p : [ 1.000 1.000 1.000 ] * n : [ 0.000 0.000 1.000 ] * faces : <class 'pyffi.object_models.xml.array.Array'> instance at ... 0: <class 'pyffi.formats.cgf.Face'> instance at ... * v_0 : 0 * v_1 : 1 * v_2 : 2 * material : 2 * sm_group : 1 1: <class 'pyffi.formats.cgf.Face'> instance at ... * v_0 : 2 * v_1 : 1 * v_2 : 3 * material : 2 * sm_group : 1 2: <class 'pyffi.formats.cgf.Face'> instance at ... * v_0 : 4 * v_1 : 5 * v_2 : 6 * material : 5 * sm_group : 1 3: <class 'pyffi.formats.cgf.Face'> instance at ... * v_0 : 6 * v_1 : 5 * v_2 : 7 * material : 5 * sm_group : 1 * uvs : <class 'pyffi.object_models.xml.array.Array'> instance at ... 0: <class 'pyffi.formats.cgf.UV'> instance at ... * u : 0.0 * v : 0.0 1: <class 'pyffi.formats.cgf.UV'> instance at ... * u : 0.0 * v : 1.0 2: <class 'pyffi.formats.cgf.UV'> instance at ... * u : 1.0 * v : 0.0 3: <class 'pyffi.formats.cgf.UV'> instance at ... * u : 1.0 * v : 1.0 4: <class 'pyffi.formats.cgf.UV'> instance at ... * u : 0.0 * v : 0.0 5: <class 'pyffi.formats.cgf.UV'> instance at ... * u : 0.0 * v : 1.0 6: <class 'pyffi.formats.cgf.UV'> instance at ... * u : 1.0 * v : 0.0 7: <class 'pyffi.formats.cgf.UV'> instance at ... * u : 1.0 * v : 1.0 * uv_faces : <class 'pyffi.object_models.xml.array.Array'> instance at ... 0: <class 'pyffi.formats.cgf.UVFace'> instance at ... * t_0 : 0 * t_1 : 1 * t_2 : 2 1: <class 'pyffi.formats.cgf.UVFace'> instance at ... * t_0 : 2 * t_1 : 1 * t_2 : 3 2: <class 'pyffi.formats.cgf.UVFace'> instance at ... * t_0 : 4 * t_1 : 5 * t_2 : 6 3: <class 'pyffi.formats.cgf.UVFace'> instance at ... * t_0 : 6 * t_1 : 5 * t_2 : 7 * vertex_colors : <class 'pyffi.object_models.xml.array.Array'> instance at ... 0: <class 'pyffi.formats.cgf.IRGB'> instance at ... * r : 0 * g : 1 * b : 2 1: <class 'pyffi.formats.cgf.IRGB'> instance at ... * r : 4 * g : 5 * b : 6 2: <class 'pyffi.formats.cgf.IRGB'> instance at ... * r : 8 * g : 9 * b : 10 3: <class 'pyffi.formats.cgf.IRGB'> instance at ... * r : 12 * g : 13 * b : 14 4: <class 'pyffi.formats.cgf.IRGB'> instance at ... * r : 50 * g : 51 * b : 52 5: <class 'pyffi.formats.cgf.IRGB'> instance at ... * r : 54 * g : 55 * b : 56 6: <class 'pyffi.formats.cgf.IRGB'> instance at ... * r : 58 * g : 59 * b : 60 7: <class 'pyffi.formats.cgf.IRGB'> instance at ... * r : 62 * g : 63 * b : 64 * vertices_data : <class 'pyffi.formats.cgf.DataStreamChunk'> instance at ... * normals_data : <class 'pyffi.formats.cgf.DataStreamChunk'> instance at ... * uvs_data : <class 'pyffi.formats.cgf.DataStreamChunk'> instance at ... * colors_data : <class 'pyffi.formats.cgf.DataStreamChunk'> instance at ... * colors_2_data : None * indices_data : <class 'pyffi.formats.cgf.DataStreamChunk'> instance at ... * tangents_data : <class 'pyffi.formats.cgf.DataStreamChunk'> instance at ... * sh_coeffs_data : None * shape_deformation_data : None * bone_map_data : None * face_map_data : None * vert_mats_data : None * reserved_data : <class 'pyffi.object_models.xml.array.Array'> instance at ... 0: None 1: None 2: None 3: None * physics_data : <class 'pyffi.object_models.xml.array.Array'> instance at ... 0: None 1: None 2: None 3: None * min_bound : [ 0.000 0.000 0.000 ] * max_bound : [ 1.000 1.000 1.000 ] * reserved_3 : <class 'pyffi.object_models.xml.array.Array'> instance at ... 0: 0 1: 0 2: 0 3: 0 4: 0 5: 0 6: 0 7: 0 8: 0 9: 0 10: 0 11: 0 12: 0 13: 0 14: 0 15: 0 16: 0 etc... <BLANKLINE> >>> print(chunk.mesh_subsets) # doctest: +ELLIPSIS <class 'pyffi.formats.cgf.MeshSubsetsChunk'> instance at ... * flags : <class 'pyffi.formats.cgf.MeshSubsetsFlags'> instance at ... * sh_has_decompr_mat : 0 * bone_indices : 0 * num_mesh_subsets : 2 * reserved_1 : 0 * reserved_2 : 0 * reserved_3 : 0 * mesh_subsets : <class 'pyffi.object_models.xml.array.Array'> instance at ... 0: <class 'pyffi.formats.cgf.MeshSubset'> instance at ... * first_index : 0 * num_indices : 6 * first_vertex : 0 * num_vertices : 4 * mat_id : 2 * radius : 0.7071067... * center : [ 0.500 0.500 0.000 ] 1: <class 'pyffi.formats.cgf.MeshSubset'> instance at ... * first_index : 6 * num_indices : 6 * first_vertex : 4 * num_vertices : 4 * mat_id : 5 * radius : 0.7071067... * center : [ 0.500 0.500 1.000 ] <BLANKLINE> >>> print(chunk.vertices_data) # doctest: +ELLIPSIS <class 'pyffi.formats.cgf.DataStreamChunk'> instance at ... * flags : 0 * data_stream_type : VERTICES * num_elements : 8 * bytes_per_element : 12 * reserved_1 : 0 * reserved_2 : 0 * vertices : <class 'pyffi.object_models.xml.array.Array'> instance at ... 0: [ 0.000 0.000 0.000 ] 1: [ 0.000 1.000 0.000 ] 2: [ 1.000 0.000 0.000 ] 3: [ 1.000 1.000 0.000 ] 4: [ 0.000 0.000 1.000 ] 5: [ 0.000 1.000 1.000 ] 6: [ 1.000 0.000 1.000 ] 7: [ 1.000 1.000 1.000 ] <BLANKLINE> >>> print(chunk.normals_data) # doctest: +ELLIPSIS <class 'pyffi.formats.cgf.DataStreamChunk'> instance at ... * flags : 0 * data_stream_type : NORMALS * num_elements : 8 * bytes_per_element : 12 * reserved_1 : 0 * reserved_2 : 0 * normals : <class 'pyffi.object_models.xml.array.Array'> instance at ... 0: [ 0.000 0.000 -1.000 ] 1: [ 0.000 0.000 -1.000 ] 2: [ 0.000 0.000 -1.000 ] 3: [ 0.000 0.000 -1.000 ] 4: [ 0.000 0.000 1.000 ] 5: [ 0.000 0.000 1.000 ] 6: [ 0.000 0.000 1.000 ] 7: [ 0.000 0.000 1.000 ] <BLANKLINE> >>> print(chunk.indices_data) # doctest: +ELLIPSIS <class 'pyffi.formats.cgf.DataStreamChunk'> instance at ... * flags : 0 * data_stream_type : INDICES * num_elements : 12 * bytes_per_element : 2 * reserved_1 : 0 * reserved_2 : 0 * indices : <class 'pyffi.object_models.xml.array.Array'> instance at ... 0: 0 1: 1 2: 2 3: 2 4: 1 5: 3 6: 4 7: 5 8: 6 9: 6 10: 5 11: 7 <BLANKLINE> >>> print(chunk.uvs_data) # doctest: +ELLIPSIS <class 'pyffi.formats.cgf.DataStreamChunk'> instance at ... * flags : 0 * data_stream_type : UVS * num_elements : 8 * bytes_per_element : 8 * reserved_1 : 0 * reserved_2 : 0 * uvs : <class 'pyffi.object_models.xml.array.Array'> instance at ... 0: <class 'pyffi.formats.cgf.UV'> instance at ... * u : 0.0 * v : 1.0 1: <class 'pyffi.formats.cgf.UV'> instance at ... * u : 0.0 * v : 0.0 2: <class 'pyffi.formats.cgf.UV'> instance at ... * u : 1.0 * v : 1.0 3: <class 'pyffi.formats.cgf.UV'> instance at ... * u : 1.0 * v : 0.0 4: <class 'pyffi.formats.cgf.UV'> instance at ... * u : 0.0 * v : 1.0 5: <class 'pyffi.formats.cgf.UV'> instance at ... * u : 0.0 * v : 0.0 6: <class 'pyffi.formats.cgf.UV'> instance at ... * u : 1.0 * v : 1.0 7: <class 'pyffi.formats.cgf.UV'> instance at ... * u : 1.0 * v : 0.0 <BLANKLINE> >>> print(chunk.tangents_data) # doctest: +ELLIPSIS <class 'pyffi.formats.cgf.DataStreamChunk'> instance at ... * flags : 0 * data_stream_type : TANGENTS * num_elements : 8 * bytes_per_element : 16 * reserved_1 : 0 * reserved_2 : 0 * tangents : <class 'pyffi.object_models.xml.array.Array'> instance at ... 0, 0: <class 'pyffi.formats.cgf.Tangent'> instance at ... * x : 32767 * y : 0 * z : 0 * w : 32767 0, 1: <class 'pyffi.formats.cgf.Tangent'> instance at ... * x : 0 * y : -32767 * z : 0 * w : 32767 1, 0: <class 'pyffi.formats.cgf.Tangent'> instance at ... * x : 32767 * y : 0 * z : 0 * w : 32767 1, 1: <class 'pyffi.formats.cgf.Tangent'> instance at ... * x : 0 * y : -32767 * z : 0 * w : 32767 2, 0: <class 'pyffi.formats.cgf.Tangent'> instance at ... * x : 32767 * y : 0 * z : 0 * w : 32767 2, 1: <class 'pyffi.formats.cgf.Tangent'> instance at ... * x : 0 * y : -32767 * z : 0 * w : 32767 3, 0: <class 'pyffi.formats.cgf.Tangent'> instance at ... * x : 32767 * y : 0 * z : 0 * w : 32767 3, 1: <class 'pyffi.formats.cgf.Tangent'> instance at ... * x : 0 * y : -32767 * z : 0 * w : 32767 4, 0: <class 'pyffi.formats.cgf.Tangent'> instance at ... * x : 32767 * y : 0 * z : 0 * w : 32767 4, 1: <class 'pyffi.formats.cgf.Tangent'> instance at ... * x : 0 * y : -32767 * z : 0 * w : 32767 5, 0: <class 'pyffi.formats.cgf.Tangent'> instance at ... * x : 32767 * y : 0 * z : 0 * w : 32767 5, 1: <class 'pyffi.formats.cgf.Tangent'> instance at ... * x : 0 * y : -32767 * z : 0 * w : 32767 6, 0: <class 'pyffi.formats.cgf.Tangent'> instance at ... * x : 32767 * y : 0 * z : 0 * w : 32767 6, 1: <class 'pyffi.formats.cgf.Tangent'> instance at ... * x : 0 * y : -32767 * z : 0 * w : 32767 7, 0: <class 'pyffi.formats.cgf.Tangent'> instance at ... * x : 32767 * y : 0 * z : 0 * w : 32767 7, 1: <class 'pyffi.formats.cgf.Tangent'> instance at ... * x : 0 * y : -32767 * z : 0 * w : 32767 <BLANKLINE> >>> print(chunk.colors_data) # doctest: +ELLIPSIS <class 'pyffi.formats.cgf.DataStreamChunk'> instance at ... * flags : 0 * data_stream_type : COLORS * num_elements : 8 * bytes_per_element : 4 * reserved_1 : 0 * reserved_2 : 0 * rgba_colors : <class 'pyffi.object_models.xml.array.Array'> instance at ... 0: <class 'pyffi.formats.cgf.IRGBA'> instance at ... * r : 0 * g : 1 * b : 2 * a : 3 1: <class 'pyffi.formats.cgf.IRGBA'> instance at ... * r : 4 * g : 5 * b : 6 * a : 7 2: <class 'pyffi.formats.cgf.IRGBA'> instance at ... * r : 8 * g : 9 * b : 10 * a : 11 3: <class 'pyffi.formats.cgf.IRGBA'> instance at ... * r : 12 * g : 13 * b : 14 * a : 15 4: <class 'pyffi.formats.cgf.IRGBA'> instance at ... * r : 50 * g : 51 * b : 52 * a : 53 5: <class 'pyffi.formats.cgf.IRGBA'> instance at ... * r : 54 * g : 55 * b : 56 * a : 57 6: <class 'pyffi.formats.cgf.IRGBA'> instance at ... * r : 58 * g : 59 * b : 60 * a : 61 7: <class 'pyffi.formats.cgf.IRGBA'> instance at ... * r : 62 * g : 63 * b : 64 * a : 65 <BLANKLINE>
- Parameters
verticeslist – A list of lists of vertices (one list per material).
normalslist – A list of lists of normals (one list per material).
triangleslist – A list of lists of triangles (one list per material).
matlist – A list of material indices. Optional.
uvslist – A list of lists of uvs (one list per material). Optional.
colorslist – A list of lists of RGBA colors (one list per material). Optional. Each color is a tuple (r, g, b, a) with each component an integer between 0 and 255.
-
set_vertices_normals
(vertices, normals)¶ B{Deprecated. Use L{set_geometry} instead.} Set vertices and normals. This used to be the first function to call when setting mesh geometry data.
Returns list of chunks that have been added.
-
update_tangent_space
()¶ Recalculate tangent space data.
-
-
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)¶ 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
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
Ref
(**kwargs)¶ Bases:
pyffi.object_models.xml.basic.BasicBase
Reference to a chunk, up the hierarchy.
-
fix_links
(data)¶ Resolve chunk index into a chunk.
- Keyword Arguments
block_dct – 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.
-
get_links
(data=None)¶ 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
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)¶ Bases:
pyffi.object_models.common.FixedString
String of fixed length 128.
-
class
String16
(**kwargs)¶ Bases:
pyffi.object_models.common.FixedString
String of fixed length 16.
-
class
String256
(**kwargs)¶ Bases:
pyffi.object_models.common.FixedString
String of fixed length 256.
-
class
String32
(**kwargs)¶ Bases:
pyffi.object_models.common.FixedString
String of fixed length 32.
-
class
String64
(**kwargs)¶ 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.
-
w
¶ Handness? Either 32767 (+1.0) or -32767 (-1.0).
-
-
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).
-
t_0
¶ First vertex index.
-
t_1
¶ Second vertex index.
-
t_2
¶ Third vertex index.
-
-
class
UnknownAAFC0005Chunk
(template=None, argument=None, parent=None)¶ Bases:
pyffi.formats.cgf.Chunk
Unknown. An extra block written by the XSI exporter.
-
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) # doctest: +ELLIPSIS
<class '...SourceInfoChunk'> instance at ...
* source_file : <None>
* date : Fri Sep 28 22:40:44 2007
* author : blender@BLENDER
<BLANKLINE>
<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
<BLANKLINE>
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) # doctest: +ELLIPSIS +REPORT_NDIFF
<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>