<commit_before>#############################################################################
##
## Copyright   : (C) 2014 <NAME>
## License     : BSD3
##
## Maintainer  : <NAME> <<EMAIL>>
##
############################################################################

bl_info = {
    "name"     : "Export mesh to photon JSON (.ymsh)"
  , "author"   : "<NAME>"
  , "category" : "Import-Export"
  , "location" : "File > Import-Export"
}

import bpy
from bpy_extras.io_utils import ExportHelper
from bpy.props import BoolProperty
import json

class PhotonMeshExporter(bpy.types.Operator, ExportHelper):
  """Photon Mesh Exporter Script"""
  bl_idname      = "object.photon_mesh_exporter"
  bl_label       = "Photon Mesh Exporter"
  bl_description = "Export all meshes from the scene into a directory"
  bl_options     = {'REGISTER'}

  filename_ext   = ".ymsh"

  sparse = BoolProperty (
      name        = "Sparse output"
    , description = "Should the output file be sparse?"
    , default     = False
    , )

  def execute(self, context):
    print("-- ----------------------- --")
    print("-- Photon Mesh JSON Export --")
    o = bpy.context.active_object
    if o == None:
      print("E: no mesh selected")
    else:
      msh = o.data
      if not hasOnlyTris(msh):
        print("W: '" + msh.name + "' is not elegible to export, please convert quadrangles to triangles")
      else:
        print("I: exporting '" + msh.name + "'")
        phmsh = toPhotonMesh(msh)
        fp = open(self.filepath, "w")
        fp.write(phmsh.toJSON(self.sparse))
        fp.close()
    print("-- ----------------------- --")
    return {'FINISHED'}

def register():
  bpy.utils.register_class(PhotonMeshExporter)

def unregister():
  bpy.utils.unregister_class(PhotonMeshExporter)

if __name__ == "__main__":
  register()

class PhotonMesh:
  def __init__(self, vs, vgr):
    self.vertices = vs
    self.vgroup   = vgr

  def toJSON(self, sparse):
    i = 2 if sparse else None
    d = { "vertices" : self.vertices, "vgroup" : self.vgroup }
    return json.dumps(d, sort_keys=True, indent=i)

def hasOnlyTris(msh):
  for poly in msh.polygons:
    if len(poly.vertices) > 3:
      return False
  return True

def toPhotonMesh(msh):
  vs = []
  vg = []

  # vertices
  for vert in msh.vertices:
    pos = [round_(vert.co[0]),round_(vert.co[1]),round_(vert.co[2])]
    nor = [round_(vert.normal[0]),round_(vert.normal[1]),round_(vert.normal[2])]
    vs.append([pos,nor])

  # vertex group
  i = 0
  ll = len(msh.loops)
  while i < ll:
    vg.append([ msh.loops[i].vertex_index
              , msh.loops[i+1].vertex_index
              , msh.loops[i+2].vertex_index
              ])
    i += 3

  return PhotonMesh(vs,vg)

def round_(x):
  return round(x,6)
<commit_msg>Fixed toJSON python function (Blender vertices export).<commit_after>#############################################################################
##
## Copyright   : (C) 2014 <NAME>
## License     : BSD3
##
## Maintainer  : <NAME> <<EMAIL>>
##
############################################################################

bl_info = {
    "name"     : "Export mesh to photon JSON (.ymsh)"
  , "author"   : "<NAME>"
  , "category" : "Import-Export"
  , "location" : "File > Import-Export"
}

import bpy
from bpy_extras.io_utils import ExportHelper
from bpy.props import BoolProperty
import json

class PhotonMeshExporter(bpy.types.Operator, ExportHelper):
  """Photon Mesh Exporter Script"""
  bl_idname      = "object.photon_mesh_exporter"
  bl_label       = "Photon Mesh Exporter"
  bl_description = "Export all meshes from the scene into a directory"
  bl_options     = {'REGISTER'}

  filename_ext   = ".ymsh"

  sparse = BoolProperty (
      name        = "Sparse output"
    , description = "Should the output file be sparse?"
    , default     = False
    , )

  def execute(self, context):
    print("-- ----------------------- --")
    print("-- Photon Mesh JSON Export --")
    o = bpy.context.active_object
    if o == None:
      print("E: no mesh selected")
    else:
      msh = o.data
      if not hasOnlyTris(msh):
        print("W: '" + msh.name + "' is not elegible to export, please convert quadrangles to triangles")
      else:
        print("I: exporting '" + msh.name + "'")
        phmsh = toPhotonMesh(msh)
        fp = open(self.filepath, "w")
        fp.write(phmsh.toJSON(self.sparse))
        fp.close()
    print("-- ----------------------- --")
    return {'FINISHED'}

def register():
  bpy.utils.register_class(PhotonMeshExporter)

def unregister():
  bpy.utils.unregister_class(PhotonMeshExporter)

if __name__ == "__main__":
  register()

class PhotonMesh:
  def __init__(self, vs, vgr):
    self.vertices = vs
    self.vgroup   = vgr

  def toJSON(self, sparse):
    i = 2 if sparse else None
    d = { "vertices" : {"interleaved" : True, "values" : self.vertices}, "vgroup" : self.vgroup }
    return json.dumps(d, sort_keys=True, indent=i)

def hasOnlyTris(msh):
  for poly in msh.polygons:
    if len(poly.vertices) > 3:
      return False
  return True

def toPhotonMesh(msh):
  vs = []
  vg = []

  # vertices
  for vert in msh.vertices:
    pos = [round_(vert.co[0]),round_(vert.co[1]),round_(vert.co[2])]
    nor = [round_(vert.normal[0]),round_(vert.normal[1]),round_(vert.normal[2])]
    vs.append([pos,nor])

  # vertex group
  i = 0
  ll = len(msh.loops)
  while i < ll:
    vg.append([ msh.loops[i].vertex_index
              , msh.loops[i+1].vertex_index
              , msh.loops[i+2].vertex_index
              ])
    i += 3

  return PhotonMesh(vs,vg)

def round_(x):
  return round(x,6)

{'fraction_non_alphanumeric': 0.20424924120692733, 'fraction_numerical': 0.006427423674343867, 'mean_word_length': 2.823890784982935, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4097870', 'n_tokens_mistral': 1935, 'n_tokens_neox': 1870, 'n_words': 437}