- g_encoder = \
- {
- # The actual file header
- #
- 'header': mdl_header(),
-
- # Options
- #
- 'pack_textures': collection.cv_data.pack_textures,
-
- # Compiled data chunks (each can be read optionally by the client)
- #
- 'data':
- {
- #1---------------------------------
- 'node': [], # Metadata 'chunk'
- 'submesh': [],
- 'material': [],
- 'texture': [],
- 'anim': [],
- 'entdata': bytearray(), # variable width
- 'strings': bytearray(), # .
- #2---------------------------------
- 'keyframe': [], # Animations
- #3---------------------------------
- 'vertex': [], # Mesh data
- 'indice': [],
- #4---------------------------------
- 'pack': bytearray() # Other generic packed data
- },
-
- # All objects of the model in their final heirachy
- #
- "uid_count": 1,
- "scene_graph":{},
- "graph_lookup":{},
-
- # Allows us to reuse definitions
- #
- 'string_cache':{},
- 'mesh_cache': {},
- 'material_cache': {},
- 'texture_cache': {}
- }
-
- g_encoder['header'].identifier = 0xABCD0000
- g_encoder['header'].version = 1
-
- # Add fake NoneID material and texture
- #
- none_material = mdl_material()
- none_material.pstr_name = encoder_process_pstr( "" )
- none_material.texture_id = 0
-
- none_texture = mdl_texture()
- none_texture.pstr_name = encoder_process_pstr( "" )
- none_texture.pack_offset = 0
- none_texture.pack_length = 0
-
- g_encoder['data']['material'] += [none_material]
- g_encoder['data']['texture'] += [none_texture]
-
- g_encoder['data']['pack'].extend( b'datapack\0\0\0\0\0\0\0\0' )
-
- # Add root node
- #
- root = mdl_node()
- root.co[0] = 0
- root.co[1] = 0
- root.co[2] = 0
- root.q[0] = 0
- root.q[1] = 0
- root.q[2] = 0
- root.q[3] = 1
- root.s[0] = 1
- root.s[1] = 1
- root.s[2] = 1
- root.pstr_name = encoder_process_pstr('')
- root.submesh_start = 0
- root.submesh_count = 0
- root.offset = 0
- root.classtype = 0
- root.parent = 0xffffffff
-
- g_encoder['data']['node'] += [root]
-#}
-
-
-# fill with 0x00 until a multiple of align. Returns how many bytes it added
-#
-def bytearray_align_to( buffer, align, offset=0 ):
-#{
- count = 0
-
- while ((len(buffer)+offset) % align) != 0:
- #{
- buffer.extend( b'\0' )
- count += 1
- #}
-
- return count
-#}
-
-# Add a string to the string buffer except if it already exists there then we
-# just return its ID.
-#
-def encoder_process_pstr( s ):
-#{
- global g_encoder
-
- cache = g_encoder['string_cache']
-
- if s in cache:
- return cache[s]
-
- cache[s] = len( g_encoder['data']['strings'] )
-
- buffer = g_encoder['data']['strings']
- buffer.extend( s.encode('utf-8') )
- buffer.extend( b'\0' )
-
- bytearray_align_to( buffer, 4 )
- return cache[s]
-#}
-
-def get_texture_resource_name( img ):
-#{
- return os.path.splitext( img.name )[0]
-#}
-
-# Pack a texture
-#
-def encoder_process_texture( img ):
-#{
- global g_encoder
-
- if img == None:
- return 0
-
- cache = g_encoder['texture_cache']
- buffer = g_encoder['data']['texture']
- pack = g_encoder['data']['pack']
-
- name = get_texture_resource_name( img )
-
- if name in cache:
- return cache[name]
-
- cache[name] = len( buffer )
-
- tex = mdl_texture()
- tex.pstr_name = encoder_process_pstr( name )
-
- if g_encoder['pack_textures']:
- #{
- tex.pack_offset = len( pack )
- pack.extend( qoi_encode( img ) )
- tex.pack_length = len( pack ) - tex.pack_offset
- #}
- else:
- tex.pack_offset = 0
-
- buffer += [ tex ]
- return cache[name]
-#}
-
-def material_tex_image(v):
-#{
- return {
- "Image Texture":
- {
- "image": F"{v}"
- }
- }
-#}
-
-cxr_graph_mapping = \
-{
- # Default shader setup
- "Principled BSDF":
- {
- "Base Color":
- {
- "Image Texture":
- {
- "image": "tex_diffuse"
- },
- "Mix":
- {
- "A": material_tex_image("tex_diffuse"),
- "B": material_tex_image("tex_decal")
- },
- },
- "Normal":
- {
- "Normal Map":
- {
- "Color": material_tex_image("tex_normal")
- }
- }
- }
-}
-
-# https://harrygodden.com/git/?p=convexer.git;a=blob;f=__init__.py;#l1164
-#
-def material_info(mat):
-#{
- info = {}
-
- # Using the cv_graph_mapping as a reference, go through the shader
- # graph and gather all $props from it.
- #
- def _graph_read( node_def, node=None, depth=0 ):
- #{
- nonlocal mat
- nonlocal info
-
- # Find rootnodes
- #
- if node == None:
- #{
- _graph_read.extracted = []
-
- for node_idname in node_def:
- #{
- for n in mat.node_tree.nodes:
- #{
- if n.name == node_idname:
- #{
- node_def = node_def[node_idname]
- node = n
- break
- #}