-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup: Make code more readable instead of 1 big WIP file(#1)
* 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
1 parent
59c7ede
commit c3bc95f
Showing
20 changed files
with
319 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.vscode/ | ||
__pycache__/ |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Oops, something went wrong.