Blender-ZeroEngine-MSH2-Plugin/src/blender_addon/io_mesh_msh2/import_msh2.py

97 lines
2.9 KiB
Python

# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
This script imports a MSH2 files to Blender.
Usage:
Run this script from "File->Import" menu and then load the desired MSH2 file.
Note, This loads mesh objects and materials only, nurbs and curves are not supported.
https://schlechtwetterfront.github.io/ze_filetypes/msh.html
"""
import bpy
from .msh2 import msh2
msh_handler = msh2.MSH2(None)
def load(context,
filepath,
*,
files,
directory,
global_clamp_size=0.0,
use_smooth_groups=True,
use_edges=True,
use_split_objects=True,
use_split_groups=True,
use_image_search=True,
use_groups_as_vgroups=False,
use_cycles=True,
relpath=None,
global_matrix=None
):
"""
Called by the user interface or another script.
load_msh2(path) - should give acceptable results.
This function passes the file and sends the data off
to be split into objects and then converted into mesh objects
"""
msh_handler.import_file(filepath)
msh = msh_handler.get_mesh_obj()
sceen = bpy.context.scene
# Test adding mesh2 to blender
for model in msh.models:
name = model.name.decode("utf-8")
# print(model)
# print(model.name)
# print(model.index)
# print(model.collection)
# print("")
# Create a mesh data block
mesh2 = bpy.data.meshes.new("{}".format(name))
verts = []
edges = []
faces = []
segments = model.segments
for segment in segments:
if segment.classname == "SegmentGeometry":
vertices = segment.vertices
for vert in vertices:
x = vert.x
y = vert.y
z = vert.z
verts.append((x, y, z))
sfaces = segment.faces
for face in sfaces:
'''Using CCW order for importing.'''
faces.append(face.SIindices())
# Add the vertices to the mesh
mesh2.from_pydata(verts, edges, faces)
# Create an object that uses the mesh data
myobj = bpy.data.objects.new("{}_obj".format(name), mesh2)
# Link the object to the scene
sceen.objects.link(myobj)
return {'FINISHED'}