Skip to content

Commit

Permalink
Cleanup: Make code more readable instead of 1 big WIP file(#1)
Browse files Browse the repository at this point in the history
* Minor file cleanups

* Initial cleanup

* Fix cleanup, remove the wrong bpy imports

Co-authored-by: Chris Rosendorf <[email protected]>
Co-authored-by: Viktor Kim Christiansen <[email protected]>
  • Loading branch information
3 people authored Dec 30, 2020
1 parent 59c7ede commit c3bc95f
Show file tree
Hide file tree
Showing 20 changed files with 319 additions and 237 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode/
__pycache__/
12 changes: 0 additions & 12 deletions .vscode/settings.json

This file was deleted.

30 changes: 17 additions & 13 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
bl_info = {
"name" : "SUT - Sandbox Unreal Tools",
"author" : "Exomemphiz & Viter",
"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.",
"blender" : (2, 91, 0),
"version" : (0, 0, 1),
Expand All @@ -9,29 +9,33 @@
"category" : "Generic"
}

import bpy
from . operator.sut_op import Sut_OT_Operator
from . panel.sut_properties import SutProperties
from . panel.sut_panel import Sut_PT_Panel

from . sut_op import Sut_OT_Operator
from . sut_properties import SutProperties
from . sut_panel import Sut_PT_Panel

from bpy.utils import (register_class, unregister_class)
from bpy.utils import (
register_class,
unregister_class
)
from bpy.props import PointerProperty

classes = (Sut_OT_Operator, Sut_PT_Panel, SutProperties)
from bpy.types import Scene

classes = (
Sut_OT_Operator,
Sut_PT_Panel,
SutProperties
)

def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)

bpy.types.Scene.my_tool = PointerProperty(type=SutProperties)
Scene.sut_tool = PointerProperty(type=SutProperties)

def unregister():
from bpy.utils import unregister_class
for cls in reversed(classes):
unregister_class(cls)
del bpy.types.Scene.my_tool
del Scene.sut_tool


if __name__ == "__main__":
Expand Down
Binary file removed __pycache__/__init__.cpython-37.pyc
Binary file not shown.
Binary file removed __pycache__/my_properties.cpython-37.pyc
Binary file not shown.
Binary file removed __pycache__/sut_op.cpython-37.pyc
Binary file not shown.
Binary file removed __pycache__/sut_panel.cpython-37.pyc
Binary file not shown.
Binary file removed __pycache__/sut_properties.cpython-37.pyc
Binary file not shown.
Binary file removed __pycache__/test_op.cpython-37.pyc
Binary file not shown.
Binary file removed __pycache__/test_panel.cpython-37.pyc
Binary file not shown.
File renamed without changes
72 changes: 72 additions & 0 deletions functions/sut_make_mesh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import bpy

from . sut_unwrap import auto_unwrap
from . sut_utils import (
copy_selected_objects,
select_children_objects_recursively
)

def make_single_mesh(selected_collection, context):
# For scene / own global variables
sut_tool = context.scene.sut_tool

# Go into object mode (maybe actually check stuff instead of this)
try:
bpy.ops.object.mode_set(mode="OBJECT")
except:
pass

# Deselect everything
bpy.ops.object.select_all(action='DESELECT')

# Temp work
select_children_objects_recursively(selected_collection)

# Clone the selected collection
temp_collection_name = 'SUT_COL_' + selected_collection.name
temp_collection = bpy.data.collections.new(name=temp_collection_name)
copy_selected_objects(temp_collection, 0)
bpy.context.scene.collection.children.link(temp_collection)

# Deselect everything
bpy.ops.object.select_all(action='DESELECT')

# Select all objects as active
select_children_objects_recursively(temp_collection)

# Merge the objects
bpy.ops.object.join()
new_merged = temp_collection.all_objects[0]

# Apply transform (Fixes scaling when uv unwrapping + sets object origin to world center)
new_merged.select_set(True)
bpy.ops.object.transform_apply(
location=True,
rotation=False,
scale=True
)
new_merged.select_set(False)

# Prepare naming for the final mesh
mesh_name = 'SM_' + selected_collection.name
mesh_collection = bpy.data.collections[sut_tool.final_col_name]

# Check if mesh exists, remove if it does
try:
old_mesh = bpy.data.collections[sut_tool.final_col_name].objects[mesh_name]
bpy.data.objects.remove(old_mesh)
except:
pass

# Set the new name, now that it is available
new_merged.name = mesh_name

# Move to "Mesh" collection
mesh_collection.objects.link(new_merged)
temp_collection.objects.unlink(new_merged)

# Delete the temp collection
bpy.data.collections.remove(temp_collection)

# Auto unwrap
auto_unwrap(new_merged, context)
27 changes: 27 additions & 0 deletions functions/sut_unwrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import bpy
import math

from bpy.ops import (
object as obj,
mesh,
uv
)

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

obj.select_all(action='DESELECT')
if (input_obj.type == 'MESH'):
input_obj.select_set(True)
obj.mode_set(mode="EDIT")
mesh.select_all(action='SELECT') # for all faces
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
)
obj.mode_set(mode="OBJECT")
input_obj.select_set(False)
55 changes: 55 additions & 0 deletions functions/sut_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import bpy

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

for child_col in input_col.children:
select_children_objects_recursively(child_col)

def copy_selected_objects(to_col, linked):
'''Duplicates selected objects into a desired collection'''
for o in bpy.context.selected_objects:
dupe = o.copy()
if not linked and o.data:
dupe.data = dupe.data.copy()
to_col.objects.link(dupe)

def use_texel_density(sut_tool):
'''Uses the Texel Density addon with desired values'''
bpy.ops.object.preset_set(td_value=sut_tool.sut_texel_density)
bpy.context.scene.td.texture_size = sut_tool.sut_texture_size
bpy.ops.object.texel_density_set()
bpy.ops.object.select_all(action='DESELECT')

def validate_collection_names(self, sut_tool):
"""Ensures greybox collection is made & final collection either exists or will be created
Returns:
bool: Returning value
"""

# Check if collections don't exist, and create them
try:
bpy.data.collections[sut_tool.greybox_col_name]
except:
self.report(
{'ERROR'},
"You have not created a '" + sut_tool.greybox_col_name + "' greybox collection yet!"
)
return False
try:
bpy.data.collections[sut_tool.final_col_name]
except:
new_col = bpy.data.collections.new(name=sut_tool.final_col_name)
bpy.context.scene.collection.children.link(new_col)

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
54 changes: 54 additions & 0 deletions operator/sut_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import bpy

from .. functions.sut_make_mesh import make_single_mesh
from .. functions.sut_utils import (
hide_collection_viewport,
select_children_objects_recursively,
use_texel_density,
validate_collection_names
)

class Sut_OT_Operator(bpy.types.Operator):
bl_idname = "view3d.sut"
bl_label = "SUT"
bl_description = "SUT"

def execute(self, context):
# For scene / own global variables
sut_tool = context.scene.sut_tool

if not validate_collection_names(self, sut_tool):
return {'FINISHED WITH ERRORS'}

# 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)

# Every collection gets merged into it's own unique mesh (or all collections is a single big mesh)
greybox_collection = bpy.data.collections[sut_tool.greybox_col_name]
if sut_tool.every_col_is_own_mesh is True:
# Get final collection, loop over every child collection and run make_single_mesh
for col in greybox_collection.children:
make_single_mesh(col, context)
else:
make_single_mesh(greybox_collection, context)

# For every single object in the final collection, set the texel density
final_collection = bpy.data.collections[sut_tool.final_col_name]
select_children_objects_recursively(final_collection)

use_texel_density(sut_tool)

if sut_tool.sut_send_to_unreal is True:
bpy.ops.wm.send2ue()

# Hide the Collection
hide_collection_viewport(sut_tool.final_col_name, True)
return {'FINISHED'}
33 changes: 33 additions & 0 deletions panel/sut_panel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import bpy

from bpy.types import Panel

# Add to this list after adding a property to SutProperties
UI_PROPERTIES = [
"sut_island_margin",
"sut_angle_limit",
"sut_texture_size",
"sut_texel_density",
"greybox_col_name",
"final_col_name",
"every_col_is_own_mesh",
"sut_send_to_unreal"
]

class Sut_PT_Panel(Panel):
bl_idname = "SUT_PT_Panel"
bl_label = "Sandbox Unreal Tools"
bl_category = "SUT"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"

def draw(self, context):
layout = self.layout
scene = context.scene
sut_tool = scene.sut_tool

for prop in UI_PROPERTIES:
layout.prop(sut_tool, prop)

row = layout.row()
row.operator('view3d.sut', text="SUT")
Loading

0 comments on commit c3bc95f

Please sign in to comment.