Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add smooth_normals option to load_blender_scene #605

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ursina/mesh_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def load_model(name, path=application.asset_folder, file_types=('.bam', '.ursina
application.blender_paths['default'] = blender_exec


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):
def load_blender_scene(name, path=application.asset_folder, reload=False, skip_hidden=True, models_only=False, uvs=True, vertex_colors=True, normals=True, smooth_normals=False, triangulate=True, decimals=4):
scenes_folder = Path(application.asset_folder / 'scenes')
if not scenes_folder.exists():
scenes_folder.mkdir()
Expand Down Expand Up @@ -156,6 +156,7 @@ def load_blender_scene(name, path=application.asset_folder, reload=False, skip_h
'--uvs' if uvs else '',
'--normals' if normals else '',
'--vertex_colors' if vertex_colors else '',
'--smooth_normals' if smooth_normals else '',
'--triangulate' if triangulate else '',
f'--decimals={decimals}',
]
Expand Down
24 changes: 22 additions & 2 deletions ursina/scripts/_blender_scene_to_ursina.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import bpy
import os
import sys
import numpy
from pathlib import Path
from math import degrees
import bmesh
Expand Down Expand Up @@ -105,7 +106,25 @@
sharp_normals.append(averaged_normal)

normals = sharp_normals

if '--smooth_normals' in sys.argv:
vertices=verts
bucket = list()
_normals = numpy.array(normals)
for i, v in enumerate(vertices):
if i in bucket:
continue

overlapping_verts_indices = list()
for j, w in enumerate(vertices):
if w == v:
overlapping_verts_indices.append(j)
bucket.append(j)

average_normal = sum(_normals[e] for e in overlapping_verts_indices) / 3
for index in overlapping_verts_indices:
_normals[index] = average_normal
normals = list(_normals)
normals = numpy.array(normals)

if '--uvs' in sys.argv:
uv_layer = mesh.uv_layers.active
Expand All @@ -114,11 +133,12 @@



normals_string = numpy.array2string(normals, separator=',').replace('\n', '')
code += f'''
'{mesh.name}' : Mesh(
vertices={str(verts).replace(' ', '')},
triangles={polygons},
normals={str(normals).replace(' ', '')},
normals={normals_string.replace(' ', '')},
colors={str(vertex_colors).replace(' ', '')},
uvs={str(uvs).replace(' ', '')},
),
Expand Down