Skip to content

Commit

Permalink
added unity exporter addon.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonNordon4 committed Aug 6, 2024
1 parent 889a8ea commit c994fb4
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 99 deletions.
Binary file removed hello_world.zip
Binary file not shown.
7 changes: 0 additions & 7 deletions hello_world/__init__.py

This file was deleted.

74 changes: 0 additions & 74 deletions hello_world/blender_manifest.toml

This file was deleted.

2 changes: 0 additions & 2 deletions hello_world/util.py

This file was deleted.

10 changes: 5 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
<th>Size</th>
</tr>
<tr>
<td><tt><a href="./hello_world.zip?repository=.%2Findex.json&blender_version_min=4.2.0">hello_world-1.0.0</a></tt></td>
<td>Hello World</td>
<td>This is another extension</td>
<td><tt><a href="./unity_exporter.zip?repository=.%2Findex.json&blender_version_min=4.2.0">unity_exporter-1.0.0</a></tt></td>
<td>UnityExporter</td>
<td>UnityExporter Blender Addon</td>
<td>~</td>
<td>4.2.0 - ~</td>
<td>all</td>
<td>2.7KB</td>
<td>16.5KB</td>
</tr>
</table>

<center><p>Built 2024-08-06, 01:33</p></center>
<center><p>Built 2024-08-06, 11:30</p></center>
</body>
</html>
17 changes: 8 additions & 9 deletions index.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@
"data": [
{
"schema_version": "1.0.0",
"id": "hello_world",
"name": "Hello World",
"tagline": "This is another extension",
"id": "unity_exporter",
"name": "UnityExporter",
"tagline": "UnityExporter Blender Addon",
"version": "1.0.0",
"type": "add-on",
"maintainer": "Simon Nordon <email@address.com>",
"maintainer": "Simon Nordon <simon.nordon@protonmail.com>",
"license": [
"SPDX:GPL-2.0-or-later"
],
"blender_version_min": "4.2.0",
"tags": [
"Animation",
"Sequencer"
"Object"
],
"archive_url": "./hello_world.zip",
"archive_size": 2753,
"archive_hash": "sha256:b1349f2703f9eab7ddf77acfe14dd12095b2529caf36deb044a63b22117e3761"
"archive_url": "./unity_exporter.zip",
"archive_size": 16892,
"archive_hash": "sha256:379ff50e4e45fefc3afc0908d546f141b91e6b80ff55d01d5126b8fb5d826900"
}
]
}
Binary file added unity_exporter.zip
Binary file not shown.
102 changes: 100 additions & 2 deletions unity_exporter/unity_exporter_panel.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import bpy
import os

import bpy.utils

from . import unity_fbx_exporter

# Handler to load the directory path when a Blender file is loaded
def load_directory_path_handler(dummy):
bpy.context.scene.directory_path = bpy.context.scene.get("last_directory_path", "")
Expand Down Expand Up @@ -32,12 +33,53 @@ def draw(self, context):
scene = context.scene
layout = self.layout
layout.prop(scene, "directory_path")

row = layout.row()
row.operator("unity_exporter.clean_image_names")
row.operator("unity_exporter.clean_mesh_names")

layout.operator("unity_exporter.export_images")
layout.operator("unity_exporter.export_selected")

class UnityExporterCleanMeshNames(bpy.types.Operator):
bl_idname = "unity_exporter.clean_mesh_names"
bl_label = "Clean Mesh Names"
bl_description = "Match mesh to object name"
bl_options = {'REGISTER', 'UNDO'}

def execute(self, context):

selected_objects = bpy.context.selected_objects

if not selected_objects:
self.report({'WARNING'}, 'No objects selected.')
return {'CANCELLED'}

# Make sure each mesh data has the same name as the object it belongs to
for obj in selected_objects:
if obj.type == 'MESH':
obj.data.name = obj.name
return {'FINISHED'}

class UnityExporterCleanImageNames(bpy.types.Operator):
bl_idname = "unity_exporter.clean_image_names"
bl_label = "Clean Image Names"
bl_description = "Match image to material name"
bl_options = {'REGISTER', 'UNDO'}

def execute(self, context):
# Ensure each image has the same name as the material it belongs to
for mat in bpy.data.materials:
if mat.node_tree:
for node in mat.node_tree.nodes:
if node.type == 'TEX_IMAGE':
node.image.name = mat.name
return {'FINISHED'}

class UnityExporterExportImages(bpy.types.Operator):
bl_idname = "unity_exporter.export_images"
bl_label = "Export Images to Unity"
bl_label = "Export Images"
bl_description = "Export all images in the blend file to the selected directory"

def execute(self, context):
# Get the directory path from the user
Expand Down Expand Up @@ -75,10 +117,63 @@ def execute(self, context):
self.report({'INFO'}, 'Images exported successfully.')
return {'FINISHED'}

class UnityExporterExportSelected(bpy.types.Operator):
bl_idname = "unity_exporter.export_selected"
bl_label = "Export Selected"
bl_description = "Export selected objects to Unity as an fbx."

def execute(self, context):
try:
# Get the directory path from the user
directory_path = context.scene.directory_path
if not directory_path:
self.report({'WARNING'}, 'Please specify a directory path.')
return {'CANCELLED'}

if not os.path.exists(directory_path):
self.report({'WARNING'}, 'The specified directory path does not exist.')
return {'CANCELLED'}

# Get selected objects
selected_objects = bpy.context.selected_objects
if not selected_objects:
self.report({'WARNING'}, 'No objects selected.')
return {'CANCELLED'}

# Get the name of the active object
if not bpy.context.active_object:
self.report({'WARNING'}, 'No active object found.')
return {'CANCELLED'}

name_of_active_object = bpy.context.active_object.name
fbx_file_path = os.path.join(directory_path, f"{name_of_active_object}.fbx")

# Ensure the directory path is writable
if not os.access(directory_path, os.W_OK):
self.report({'ERROR'}, 'The specified directory path is not writable.')
return {'CANCELLED'}

# Perform the export
try:
bpy.ops.export_scene.fbx(filepath=fbx_file_path, use_selection=True)
self.report({'INFO'}, f"Exported selected objects to {fbx_file_path}")
except Exception as e:
self.report({'ERROR'}, f"Failed to export FBX: {str(e)}")
return {'CANCELLED'}

except Exception as e:
self.report({'ERROR'}, f"An unexpected error occurred: {str(e)}")
return {'CANCELLED'}

return {'FINISHED'}

def register():
print("Registering unity_exporter_panel")
bpy.utils.register_class(UnityExporterPanel)
bpy.utils.register_class(UnityExporterCleanMeshNames)
bpy.utils.register_class(UnityExporterCleanImageNames)
bpy.utils.register_class(UnityExporterExportImages)
bpy.utils.register_class(UnityExporterExportSelected)
bpy.types.Scene.directory_path = bpy.props.StringProperty(
name="Directory Path",
description="Choose a directory",
Expand All @@ -90,7 +185,10 @@ def register():

def unregister():
bpy.utils.unregister_class(UnityExporterPanel)
bpy.utils.unregister_class(UnityExporterCleanMeshNames)
bpy.utils.unregister_class(UnityExporterCleanImageNames)
bpy.utils.unregister_class(UnityExporterExportImages)
bpy.utils.unregister_class(UnityExporterExportSelected)
del bpy.types.Scene.directory_path
bpy.app.handlers.load_post.remove(load_directory_path_handler)
bpy.app.handlers.save_pre.remove(save_directory_path_handler)
Expand Down
Loading

0 comments on commit c994fb4

Please sign in to comment.