From 84d7ce02b92b860fde39814aa9907f468673679f Mon Sep 17 00:00:00 2001 From: BurntRanch <69512353+BurntRanch@users.noreply.github.com> Date: Mon, 30 Oct 2023 13:19:15 +0300 Subject: [PATCH 1/3] Added smooth_normals argument again --- ursina/mesh_importer.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ursina/mesh_importer.py b/ursina/mesh_importer.py index 0f5c0585..7932b0dc 100644 --- a/ursina/mesh_importer.py +++ b/ursina/mesh_importer.py @@ -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() @@ -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}', ] From be96161b87c4ac330124e92a1ac83dcc8180051d Mon Sep 17 00:00:00 2001 From: BurntRanch <69512353+BurntRanch@users.noreply.github.com> Date: Mon, 30 Oct 2023 13:21:23 +0300 Subject: [PATCH 2/3] add normal smoothing, without regenerating everything this time. --- ursina/scripts/_blender_scene_to_ursina.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/ursina/scripts/_blender_scene_to_ursina.py b/ursina/scripts/_blender_scene_to_ursina.py index 94b4aa98..27ba5f7f 100644 --- a/ursina/scripts/_blender_scene_to_ursina.py +++ b/ursina/scripts/_blender_scene_to_ursina.py @@ -105,7 +105,22 @@ sharp_normals.append(averaged_normal) normals = sharp_normals - + if '--smooth_normals' in sys.argv: + vertices=verts + bucket = list() + 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 if '--uvs' in sys.argv: uv_layer = mesh.uv_layers.active From d5b8d278ec32b4c39e2d112241ff54d2212a4e1c Mon Sep 17 00:00:00 2001 From: BurntRanch <69512353+BurntRanch@users.noreply.github.com> Date: Mon, 30 Oct 2023 13:42:22 +0300 Subject: [PATCH 3/3] Fix a few bugs, It needed numpy so I added support for that. The normals array will always get converted to numpy and converted back to string, Which shouldn't affect the normals in any way. --- ursina/scripts/_blender_scene_to_ursina.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ursina/scripts/_blender_scene_to_ursina.py b/ursina/scripts/_blender_scene_to_ursina.py index 27ba5f7f..0758d3f8 100644 --- a/ursina/scripts/_blender_scene_to_ursina.py +++ b/ursina/scripts/_blender_scene_to_ursina.py @@ -1,6 +1,7 @@ import bpy import os import sys +import numpy from pathlib import Path from math import degrees import bmesh @@ -108,6 +109,7 @@ 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 @@ -117,10 +119,12 @@ if w == v: overlapping_verts_indices.append(j) bucket.append(j) - - average_normal = sum(normals[e] for e in overlapping_verts_indices) / 3 + + average_normal = sum(_normals[e] for e in overlapping_verts_indices) / 3 for index in overlapping_verts_indices: - normals[index] = average_normal + _normals[index] = average_normal + normals = list(_normals) + normals = numpy.array(normals) if '--uvs' in sys.argv: uv_layer = mesh.uv_layers.active @@ -129,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(' ', '')}, ),