def add_default_image(self, texture_target_str): if 'normal' in texture_target_str: self.default_textures[texture_target_str] = ExportImage( bpy.data.images['default_normal'], 'default_normal.png', osgt_template_functions.getUniqueId()) else: self.default_textures[texture_target_str] = ExportImage( bpy.data.images['default'], 'default.png', osgt_template_functions.getUniqueId())
def write(self, out): out.write('#Ascii Scene') out.write('#Version 78') out.write('#Generator OpenSceneGraph 2.9.17') out.write('osg::Group') out.write('{') out.write('UniqueID %i' % osgt_template_functions.getUniqueId()) out.write('UserDataContainer TRUE') out.write('{') out.write('osg::DefaultUserDataContainer') out.write('{') out.write('UniqueID %i' % osgt_template_functions.getUniqueId()) out.write('UDC_Descriptions 5') out.write('{') out.write('"USE_SHADER %s"' % self.value[1]) # magic: shader name at index 1 out.write('"USE_BUMP_MAP s_mat_diffuse"') # ei helvetti out.write('"USE_BUMP_SCALE 10.0"') out.write('"USE_BUMP_UV_INDEX 0"') out.write( '"BIND_VERTEX_ATTRIBUTE_NAME a_soft_light 10"') #TODO: FIXME! out.write('}') out.write('}') out.write('}') # default texture order self.write_texture_order_state(out) num_children = len(self.children) if PLAIN == False: num_children += 1 out.write('Children %i' % num_children) out.write('{') if PLAIN == False: self.write_texture_definitions(out) for c in self.children: self.defined_textures.append(self.defined_textures[-1].copy()) c.defined_textures = self.defined_textures # pass list reference c.write(out) self.defined_textures.pop() out.write('}') # children out.write('}')
def add_image(self, image): if image != None and image not in self: i = len(self) self[image] = ExportImage(image, 'something_%04i.png' % i, osgt_template_functions.getUniqueId()) print(' +++: adding image: %30s --> %s' % (image.name, self[image].export_name))
def write(self, out): out.write('osg::Group') out.write('{') out.write('UniqueID %i' % osgt_template_functions.getUniqueId()) #if self.common_factor.type == 'root': self._write_node( out ) #elif self.common_factor.type == '_mat_index': self._write_node( out ) #elif self.common_factor.type == '_obj_index': self._write_node( out ) #elif self.common_factor.type.startswith('tex'): self._write_node( out ) #elif self.common_factor.type == 'prim': self._write_geometry( out ) #else: raise RuntimeError('Node: cannot write node into osgt file. (unsupported type received("%s"))' % (self.common_factor.type) ) #else: pass if self.common_factor.type == '_mat_index': self._write_material_stateset(out) elif self.common_factor.type.startswith('tex'): self._write_mesh_texture(out) #elif self.common_factor.type == 'major_axis': # self._write_axis_stateset( out ) # write children out.write('Children %i' % len(self.children)) out.write('{') for c in self.children: #c.write( out ) self.defined_textures.append(self.defined_textures[-1].copy()) c.defined_textures = self.defined_textures # pass list reference c.write(out) self.defined_textures.pop() out.write('}') # children out.write('}') # this group pass
def write_texture_definitions(self, out): #if PLAIN: # return out.write('osg::Group') out.write('{') out.write('UniqueID %i' % osgt_template_functions.getUniqueId()) out.write('StateSet TRUE') out.write('{') out.write('osg::StateSet') out.write('{') out.write('UniqueID %i' % osgt_template_functions.getUniqueId()) out.write('Name "texture_definitions"') out.write('DataVariance STATIC') out.write('TextureAttributeList %i' % (len(imageExportManager.keys()))) out.write('{') for image, export_image in imageExportManager.items(): out.write('Data 1') out.write('{') out.write('osg::Texture2D') out.write('{') out.write('UniqueID %i' % export_image.unique_id) #out.write('Name "s_%s"' % target_name ) out.write('WRAP_S REPEAT') out.write('WRAP_T REPEAT') out.write('WRAP_R CLAMP_TO_EDGE') out.write('MIN_FILTER LINEAR_MIPMAP_LINEAR') out.write('MAG_FILTER LINEAR') out.write('UnRefImageDataAfterApply TRUE') out.write('ResizeNonPowerOfTwoHint TRUE') out.write('Image TRUE') out.write('{') out.write('UniqueID %i' % osgt_template_functions.getUniqueId()) out.write('FileName "%s"' % export_image.export_name) out.write('WriteHint 0 2') out.write('DataVariance STATIC') out.write('}') out.write('}') out.write('Value OFF') out.write('}') out.write('}') # TextureAttributeList ends out.write('}') # StateSet TRUE out.write('}') # osg::StateSet out.write('}') # osg::Group
def write_texture_order_state(self, out): if PLAIN: return texture_list = self.value[0] out.write('StateSet TRUE') out.write('{') out.write('osg::StateSet') out.write('{') out.write('UniqueID %i' % osgt_template_functions.getUniqueId()) out.write('Name "texture_definitions"') out.write('DataVariance STATIC') out.write('TextureAttributeList %i' % len(texture_list)) out.write('{') for target, image in texture_list.items(): out.write('Data 1') out.write('{') out.write('osg::Texture2D') out.write('{') out.write('UniqueID %i' % imageExportManager.get_default_image(target).unique_id) out.write('Name "s_%s"' % target) out.write('WRAP_S REPEAT') out.write('WRAP_T REPEAT') out.write('WRAP_R CLAMP_TO_EDGE') out.write('MIN_FILTER LINEAR_MIPMAP_LINEAR') out.write('MAG_FILTER LINEAR') out.write('UnRefImageDataAfterApply TRUE') out.write('ResizeNonPowerOfTwoHint TRUE') out.write('Image TRUE') out.write('{') out.write('UniqueID %i' % osgt_template_functions.getUniqueId()) out.write('FileName "%s"' % imageExportManager.get_default_image(target).export_name) out.write('WriteHint 0 2') out.write('DataVariance STATIC') out.write('}') out.write('}') out.write('Value OFF') out.write('}') out.write('}') # TextureAttributeList ends out.write('}') # StateSet TRUE out.write('}') # osg::StateSet
def _write_axis_stateset(self, out): if PLAIN: return properties = self.common_factor out.write('StateSet TRUE') out.write('{') out.write('osg::StateSet') out.write('{') out.write('UniqueID %i' % osgt_template_functions.getUniqueId()) out.write('Name "%s.material_state"' % 'hmm') out.write('DataVariance STATIC') out.write('UniformList %i' % (8 + n_custom_properties)) out.write('{') for prop in properties: num_prop_elements = 1 try: num_prop_elements = len(prop.value) except: pass out.write('osg::Uniform') out.write('{') out.write('UniqueID %i' % getUniqueId()) out.write('Name "%s"' % prop.name) out.write('Type %s' % prop.datatype) out.write('NumElements 1') if prop.datatype == 'INT': array_type = 'Int' elif prop.datatype == 'FLOAT': array_type = 'Float' elif prop.datatype == 'FLOAT_VEC3': array_type = 'Float' else: raise RuntimeError('Custom property datatype is invalid!') out.write('Elements TRUE ArrayID %i %sArray %i' % (getUniqueId(), array_type, num_prop_elements)) out.write('{') #out.write( '%5.7f' % ( material_scale ) ) if prop.datatype == 'FLOAT_VEC3': out.write(('%5.7f ' * num_prop_elements) % (prop.value[:])) elif prop.datatype == 'INT': out.write(('%i ' * num_prop_elements) % (prop.value)) else: raise RuntimeError('Custom property datatype is invalid!') out.write('}') out.write('}') out.write('Value OFF') out.write('}') # osg::StateSet out.write('}') # StateSet TRUE
def _write_mesh_texture(self, out): if PLAIN: return locally_defined_textures = self.common_factor.value # magic: self.common_factor.value is material for k, v in locally_defined_textures.items( ): # replace stack values with local values assert (k in self.defined_textures[-1].keys()) if v != None: self.defined_textures[-1][k] = v export_texture = self.defined_textures[-1] out.write('StateSet TRUE') out.write('{') out.write('osg::StateSet') out.write('{') out.write('UniqueID %i' % osgt_template_functions.getUniqueId()) out.write('Name "mesh_texture"') out.write('DataVariance STATIC') if 1: #print('mesh texture stateset: ', self.defined_textures[-1]) L = sum([ 1 if image != None else 0 for image, _ in export_texture.items() ]) L = 0 for target_name, image in export_texture.items(): if image: L += 1 out.write('TextureAttributeList %i' % (len(export_texture.keys()))) out.write('{') for target_name, image in export_texture.items(): out.write('Data 1') out.write('{') out.write('osg::Texture2D') out.write('{') if image: out.write('UniqueID %i' % imageExportManager[image].unique_id) else: out.write('UniqueID %i' % imageExportManager.get_default_image( target_name).unique_id) out.write('}') out.write('Value OFF') out.write('}') out.write('}') # TextureAttributeList ends out.write('}') # osg::StateSet out.write('}') # StateSet TRUE return
def write(self, out): out.write('osg::Geode') out.write('{') out.write('UniqueID %i' % osgt_template_functions.getUniqueId()) out.write('Name "NAME_HERE"') out.write('Drawables %i' % len(self.children)) out.write('{') for c in self.children: c.write(out) out.write('}') # drawables #out.write('Drawables 0') #out.write('{') #out.write('}') out.write('}')
def write(self, out): out.write('osg::Geometry') out.write('{') out.write('UniqueID %i' % osgt_template_functions.getUniqueId()) out.write('Name "%s"' % 'TODO: some name here') out.write('DataVariance STATIC') #write_geometry_stateset ( out, ctx, drawable ) #write_primitive_set_list( out, drawable ) #================ # Write Indices #================ elem_array = self.common_factor.value.elem_array attr_array = self.common_factor.value.attr_array out.write('PrimitiveSetList 1') out.write('{') out.write('DrawElementsUInt GL_TRIANGLES %i' % len(elem_array)) out.write('{') for triplet in zip(*[iter(elem_array)] * 3): out.write_item('% 4i % 4i % 4i' % triplet) out.write('') out.write('}') out.write('}') #def write_vertex_data( out, drawable ): #================ # Write vertices #================ coordinates = attr_array['co'] out.write('VertexData') out.write('{') out.write('Array TRUE ArrayID %i Vec3fArray %i' % (osgt_template_functions.getUniqueId(), len(attr_array))) out.write('{') for co in coordinates: out.write_item('% 12.8f % 12.8f % 12.8f ' % tuple(co)) out.write('') out.write('}') out.write('Indices FALSE') out.write('Binding BIND_PER_VERTEX') out.write('Normalize 0') out.write('}') #def write_normal_data( out, drawable ): #================ # Write Normals #================ normals = attr_array['no'] out.write('NormalData') out.write('{') out.write('Array TRUE ArrayID %i Vec3fArray %i' % (osgt_template_functions.getUniqueId(), len(attr_array))) out.write('{') for no in normals: out.write_item('% 12.8f % 12.8f % 12.8f ' % tuple(no)) out.write('') out.write('}') out.write('Indices FALSE') out.write('Binding BIND_PER_VERTEX') out.write('Normalize 0') out.write('}') # normals for field_name in attr_array.dtype.names: if field_name.startswith('col'): write_color_data(out, attr_array[field_name]) break # write only one set for now uv_fields = [ field_name for field_name in attr_array.dtype.names if field_name.startswith('uv') ] if len(uv_fields) > 0: out.write('TexCoordData %i' % len(uv_fields)) out.write('{') for field_name in uv_fields: if field_name.startswith('uv'): write_texcoord_data(out, attr_array[field_name]) #break # write only one set for now out.write('}') #============= # Custom Data #============= vertex_float_fields = [ attr_array[field_name] for field_name in attr_array.dtype.names if field_name.startswith('vertex_float') ] out.write('VertexAttribData %i' % (len(vertex_float_fields) + 10)) out.write('{') for i in range(10): out.write('Data') out.write('{') out.write('Array FALSE') out.write('Indices FALSE') out.write('Binding BIND_OFF') out.write('Normalize 0') out.write('}') for vertex_field in vertex_float_fields: num_components = vertex_field.shape[1] if num_components == 4: out.write('Data') out.write('{') out.write( 'Array TRUE ArrayID %i Vec4fArray %i' % (osgt_template_functions.getUniqueId(), len(vertex_field))) out.write('{') for val in vertex_field: out.write_item('% 12.8f % 12.8f % 12.8f % 12.8f ' % tuple(val)) out.write('') out.write('}') out.write('Indices FALSE') out.write('Binding BIND_PER_VERTEX') out.write('Normalize 0') out.write('}') else: raise (RuntimeError('Unsupported vector dimensionality')) out.write('}') # VertexAttribData out.write('}') # osg::Geometry
def _write_material_stateset(self, out): if PLAIN: return m = self.common_factor.value[0] # magic: index 0 is material locally_defined_textures = self.common_factor.value[ 1] # magic: index 1 is export texture for k, v in locally_defined_textures.items( ): # replace stack values with local values assert (k in self.defined_textures[-1].keys()) if v != None: self.defined_textures[-1][k] = v if m == None: assert (0) return else: alpha = m.alpha alpha_blend = m.game_settings.alpha_blend diffuse_color = tuple([ m.diffuse_intensity * component for component in m.diffuse_color[:] ]) specular_color = tuple([ m.specular_intensity * component for component in m.specular_color[:] ]) shininess = m.specular_hardness / 512.0 emit = tuple(m.emit * d for d in diffuse_color) render_bin_number = -1 if m != None: render_bin_number = -1 if m.pass_index == 0 else m.pass_index out.write('StateSet TRUE') out.write('{') out.write('osg::StateSet') out.write('{') out.write('UniqueID %i' % osgt_template_functions.getUniqueId()) out.write('Name "%s.material_state"' % 'hmm') out.write('DataVariance STATIC') if 1: out.write('AttributeList 1') out.write('{') out.write('osg::Material') out.write('{') out.write('UniqueID %i' % osgt_template_functions.getUniqueId()) out.write( 'Ambient TRUE Front % 5.3f % 5.3f % 5.3f 1 Back % 5.3f % 5.3f % 5.3f 1' % (0, 0, 0, 0, 0, 0)) out.write( 'Diffuse TRUE Front % 5.3f % 5.3f % 5.3f % 5.3f Back % 5.3f % 5.3f % 5.3f % 5.3f' % ((diffuse_color + (alpha, )) * 2)) out.write( 'Specular TRUE Front % 5.3f % 5.3f % 5.3f 1 Back % 5.3f % 5.3f % 5.3f 1' % (specular_color * 2)) out.write( 'Emission TRUE Front % 5.3f % 5.3f % 5.3f 1 Back % 5.3f % 5.3f % 5.3f 1' % (emit * 2)) out.write('Shininess TRUE Front % 5.3f Back % 5.3f' % (shininess, shininess)) out.write('}') out.write('Value OFF') out.write('}') # AttributeList 1 # write mateiral textures if 1: out.write('TextureModeList 1') out.write('{') out.write('Data 1') out.write('{') out.write('GL_TEXTURE_2D ON') out.write('}') # Data 1 out.write('}') # TextureModeList if 1: #print('material_stateset: ', self.defined_textures[-1]) export_texture = self.defined_textures[-1] #material_textures = [ (target, etex) for target, etex in material_textures.items() if etex is not None ] L = sum([ 1 if image != None else 0 for image, _ in export_texture.items() ]) L = 0 for target_name, image in export_texture.items(): if image: L += 1 out.write('TextureAttributeList %i' % (len(export_texture.keys()))) #out.write('TextureAttributeList %i' % (L) ) out.write('{') # print( '+++' ) # print( export_texture ) # print( '+++' ) for target_name, image in export_texture.items(): #print('TARGET: ', target_name ) out.write('Data 1') out.write('{') out.write('osg::Texture2D') out.write('{') #out.write('UniqueID %i' % osgt_template_functions.getUniqueId()) if image: out.write('UniqueID %i' % imageExportManager[image].unique_id) else: out.write('UniqueID %i' % imageExportManager.get_default_image( target_name).unique_id) #out.write('Name "s_%s"' % target_name ) #out.write('WRAP_S REPEAT') #out.write('WRAP_T REPEAT') #out.write('WRAP_R CLAMP_TO_EDGE') #out.write('MIN_FILTER LINEAR_MIPMAP_LINEAR') #out.write('MAG_FILTER LINEAR') #out.write('UnRefImageDataAfterApply TRUE') #out.write('ResizeNonPowerOfTwoHint TRUE') #out.write('Image TRUE') #out.write('{') #out.write('UniqueID %i' % osgt_template_functions.getUniqueId()) #out.write('FileName "%s"' % getExportName( image ) ) #out.write('WriteHint 0 2') #out.write('DataVariance STATIC') #out.write('}') out.write('}') out.write('Value OFF') out.write('}') out.write('}') # TextureAttributeList ends out.write('}') # osg::StateSet out.write('}') # StateSet TRUE return