Skip to content

Commit

Permalink
Apply modifiers & enable auto smooth by angle support (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Viterkim authored Jan 2, 2021
1 parent 60ae8ea commit ecbb03e
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 46 deletions.
141 changes: 140 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,141 @@
# Own
.vscode/
__pycache__/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/
6 changes: 3 additions & 3 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
bl_info = {
"name" : "SUT - Sandbox Unreal Tools",
"author" : "ExoMemphiz & Viter",
"description" : "Makes prototyping / greyboxing levels in unreal engine an easier process. Idea is to reduce 'double work' when having to place assets multiple times when you are prototyping anyway.",
"description" : "Faster Greyboxing / Prototyping workflow for Unreal Engine & Blender",
"blender" : (2, 91, 0),
"version" : (0, 0, 1),
"version" : (0, 0, 2),
"location" : "View3D",
"warning" : "",
"category" : "Generic"
"category" : "Import-Export"
}

from . operator.sut_op import Sut_OT_Operator
Expand Down
17 changes: 15 additions & 2 deletions functions/sut_make_mesh.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import bpy

from . sut_unwrap import auto_unwrap
from . sut_utils import (
auto_unwrap,
auto_smooth_normals,
copy_selected_objects,
select_children_objects_recursively
)
Expand All @@ -19,7 +20,7 @@ def make_single_mesh(selected_collection, context):
# Deselect everything
bpy.ops.object.select_all(action='DESELECT')

# Temp work
# Select all objects as active
select_children_objects_recursively(selected_collection)

# Clone the selected collection
Expand All @@ -34,6 +35,9 @@ def make_single_mesh(selected_collection, context):
# Select all objects as active
select_children_objects_recursively(temp_collection)

# Apply modifiers
bpy.ops.object.convert(target='MESH')

# Merge the objects
bpy.ops.object.join()
new_merged = temp_collection.all_objects[0]
Expand Down Expand Up @@ -68,5 +72,14 @@ def make_single_mesh(selected_collection, context):
# Delete the temp collection
bpy.data.collections.remove(temp_collection)

# Auto smooth normals
new_merged.select_set(True)
bpy.context.view_layer.objects.active = new_merged
if sut_tool.auto_smooth_enable is True:
auto_smooth_normals(context)
else:
bpy.ops.object.shade_flat()
bpy.ops.object.select_all(action='DESELECT')

# Auto unwrap
auto_unwrap(new_merged, context)
27 changes: 0 additions & 27 deletions functions/sut_unwrap.py

This file was deleted.

53 changes: 50 additions & 3 deletions functions/sut_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
import bpy
import math

def auto_unwrap(input_obj, context):
'''Uses the smart uv unwrapping tool with desired values'''
sut_tool = context.scene.sut_tool

bpy.ops.object.select_all(action='DESELECT')
if (input_obj.type == 'MESH'):
input_obj.select_set(True)
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action='SELECT') # for all faces
bpy.ops.uv.smart_project(
angle_limit=math.radians(66),
island_margin=sut_tool.sut_island_margin,
area_weight=0.0,
correct_aspect=True,
scale_to_bounds=False
)
bpy.ops.object.mode_set(mode="OBJECT")
input_obj.select_set(False)

def select_children_objects_recursively(input_col):
'''Recursively selects all objects within a collection, and its children'''
# Select all the objects
for obj in input_col.all_objects:
obj.select_set(True)
bpy.context.view_layer.objects.active = obj
if obj.type == "MESH":
obj.select_set(True)
bpy.context.view_layer.objects.active = obj

for child_col in input_col.children:
select_children_objects_recursively(child_col)
Expand Down Expand Up @@ -49,7 +70,33 @@ def validate_collection_names(self, sut_tool):

return True

def validate_collection_not_empty(self, sut_tool):
"""Ensures greybox collection is not empty
Returns:
bool: Returning value
"""

# Check if the greybox collection has atleast 1 element
try:
bpy.data.collections[sut_tool.greybox_col_name].all_objects[0]
except:
self.report(
{'ERROR'},
"Your greybox collection '" + sut_tool.greybox_col_name + "' has no elements!"
)
return False

return True

def hide_collection_viewport(collection_name, flag):
'''Hides or Shows a specified collection, based on which flag is given'''
# We don't know what is going on but it works
bpy.context.scene.view_layers[0].layer_collection.children[collection_name].hide_viewport = flag
bpy.context.scene.view_layers[0].layer_collection.children[collection_name].hide_viewport = flag

def auto_smooth_normals(context):
'''Enables auto smoothing of normals by the angle given'''
bpy.ops.object.shade_smooth()
radians = math.radians(float(context.scene.sut_tool.auto_smooth_angle))
bpy.context.object.data.auto_smooth_angle = radians
bpy.context.object.data.use_auto_smooth = True
7 changes: 5 additions & 2 deletions operator/sut_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
hide_collection_viewport,
select_children_objects_recursively,
use_texel_density,
validate_collection_names
validate_collection_names,
validate_collection_not_empty
)

class Sut_OT_Operator(bpy.types.Operator):
Expand All @@ -20,13 +21,15 @@ def execute(self, context):
if not validate_collection_names(self, sut_tool):
return {'CANCELLED'}

if not validate_collection_not_empty(self, sut_tool):
return {'CANCELLED'}

# Unhide the Greybox Collection
hide_collection_viewport(sut_tool.greybox_col_name, False)

# Unhide the Final Collection
hide_collection_viewport(sut_tool.final_col_name, False)


# Cleanup Mesh Collection
for obj in bpy.data.collections[sut_tool.final_col_name].all_objects:
bpy.data.objects.remove(obj)
Expand Down
2 changes: 2 additions & 0 deletions panel/sut_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"sut_texel_density",
"greybox_col_name",
"final_col_name",
"auto_smooth_angle",
"auto_smooth_enable",
"every_col_is_own_mesh",
"sut_send_to_unreal"
]
Expand Down
20 changes: 16 additions & 4 deletions panel/sut_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,37 @@ class SutProperties(PropertyGroup):
)

every_col_is_own_mesh: BoolProperty(
name="Split collection result",
name="Seperate Child Collection Meshes",
description="Check if you want every collection to be their own seperate merged mesh\n(Origin will still be world origin)",
default = False
)

sut_send_to_unreal: BoolProperty(
name="Send to Unreal Engine",
description="Avoids 2 clicks",
default = False
default = True
)

greybox_col_name: StringProperty(
name="Greybox Coll. Name",
description="The greyboxing collection name",
default = "SUT"
description="The greybox collection name",
default = "Collection"
)

final_col_name: StringProperty(
name="Final Coll. Name",
description="Final collection for unreal engine, default is 'Mesh' from the UE Tools addon",
default = "Mesh"
)

auto_smooth_angle: StringProperty(
name="Auto Smooth Angle",
description="Angle for auto smoothing normals on the final mesh",
default = "30"
)

auto_smooth_enable: BoolProperty(
name="Enable Auto Smooth By Angle",
description="Enable auto smoothing",
default = True
)
Loading

0 comments on commit ecbb03e

Please sign in to comment.