Skip to content

Commit e24b51a

Browse files
committed
added option for load_blender_scene() to not triangulate meshes, which can be useful when using the mesh info for code purposes.
1 parent 36dbe8b commit e24b51a

2 files changed

Lines changed: 31 additions & 16 deletions

File tree

ursina/mesh_importer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def load_model(name, path=application.asset_folder, file_types=('.bam', '.ursina
128128
application.blender_paths['default'] = blender_exec
129129

130130

131-
def load_blender_scene(name, path=application.asset_folder, reload=False, skip_hidden=True, models_only=False, uvs=True, vertex_colors=True, normals=True, decimals=4):
131+
def load_blender_scene(name, path=application.asset_folder, reload=False, skip_hidden=True, models_only=False, uvs=True, vertex_colors=True, normals=True, triangulate=True, decimals=4):
132132
scenes_folder = Path(application.asset_folder / 'scenes')
133133
if not scenes_folder.exists():
134134
scenes_folder.mkdir()
@@ -156,6 +156,7 @@ def load_blender_scene(name, path=application.asset_folder, reload=False, skip_h
156156
'--uvs' if uvs else '',
157157
'--normals' if normals else '',
158158
'--vertex_colors' if vertex_colors else '',
159+
'--triangulate' if triangulate else '',
159160
f'--decimals={decimals}',
160161
]
161162

ursina/scripts/_blender_scene_to_ursina.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
script_file = sys.argv[4]
1515
out_file_path = sys.argv[5]
1616
decimals = int([e for e in sys.argv if e.startswith('--decimals=')][0][len('--decimals='):])
17+
TRIANGULATE = '--triangulate' in sys.argv
18+
19+
20+
print('-----------------------', 'decimals:', decimals, 'triangulate:', TRIANGULATE)
1721

1822
scene_name = Path(bpy.data.filepath).stem
1923
print('file_name:', scene_name)
@@ -46,30 +50,39 @@
4650

4751

4852
for key, ob in unique_objects.items():
49-
mesh = ob.evaluated_get(dg).data
50-
51-
# triangulate mesh
52-
bm = bmesh.new()
53-
bm.from_mesh(mesh)
54-
bmesh.ops.triangulate(bm, faces=bm.faces[:], quad_method='BEAUTY', ngon_method='BEAUTY')
55-
bm.normal_update()
56-
bm.to_mesh(mesh)
57-
bm.free()
58-
53+
polygons =[]
5954
verts = []
6055
normals = []
6156
vertex_colors = []
6257
uvs = []
63-
6458
indices = []
6559

60+
mesh = ob.evaluated_get(dg).data
61+
# for poly in mesh.polygons:
62+
# print('---------------poly:', [int(e) for e in poly.vertices])
63+
if TRIANGULATE:
64+
# triangulate mesh
65+
bm = bmesh.new()
66+
bm.from_mesh(mesh)
67+
bmesh.ops.triangulate(bm, faces=bm.faces[:], quad_method='BEAUTY', ngon_method='BEAUTY')
68+
bm.normal_update()
69+
bm.to_mesh(mesh)
70+
bm.free()
71+
else:
72+
for poly in mesh.polygons:
73+
# print('---------------poly:', [int(e) for e in poly.vertices])
74+
polygons.append([int(e) for e in poly.vertices])
75+
76+
6677
for poly in mesh.polygons:
6778
indices.extend(poly.vertices)
6879

69-
for idx in indices:
70-
v = mesh.vertices[idx]
71-
verts.append((round(v.co[0],decimals),round(v.co[2],decimals),round(v.co[1],decimals)))
72-
80+
if TRIANGULATE:
81+
for idx in indices:
82+
v = mesh.vertices[idx]
83+
verts.append((round(v.co[0],decimals),round(v.co[2],decimals),round(v.co[1],decimals)))
84+
else:
85+
verts = [(round(v.co[0],decimals),round(v.co[2],decimals),round(v.co[1],decimals)) for v in mesh.vertices]
7386

7487
if '--vertex_colors' in sys.argv and 'Color' in mesh.attributes:
7588
color_data = mesh.attributes['Color'].data
@@ -104,6 +117,7 @@
104117
code += f'''
105118
'{mesh.name}' : Mesh(
106119
vertices={str(verts).replace(' ', '')},
120+
triangles={polygons},
107121
normals={str(normals).replace(' ', '')},
108122
colors={str(vertex_colors).replace(' ', '')},
109123
uvs={str(uvs).replace(' ', '')},

0 commit comments

Comments
 (0)