soupday / cc_blender_tools

Add-on for Blender, for importing and auto-setup of character creator 3 & 4 and iClone 7 & 8 character exports into Blender.
GNU General Public License v3.0
460 stars 84 forks source link

.fbx Re-Exporting Characters and Animations to Unity #144

Open mertakbal opened 1 year ago

mertakbal commented 1 year ago

Following steps:

throws following error: Traceback (most recent call last): File "/home/mrt/.config/blender/3.5/scripts/addons/cc_blender_tools-main/exporter.py", line 2145, in execute export_to_unity(self, chr_cache, self.include_anim, self.filepath, self.include_selected) File "/home/mrt/.config/blender/3.5/scripts/addons/cc_blender_tools-main/exporter.py", line 1807, in export_to_unity set_T_pose(arm, json_data[name]["Object"][name]) TypeError: 'NoneType' object is not subscriptable

soupday commented 1 year ago

Why are you re-importing the export to Unity back into Blender? That export is meant for Unity only.

I'm surprised it re-imports to Blender at all given how much re-writing I have to do to make it compatible with Unity.

soupday commented 1 year ago

On the other hand, it might be useful to make that work.

soupday commented 1 year ago

In fact just tried this and it worked.

So does the character you originally imported into Blender have a .json file? and is it exported to unity with the rest of the character? (The error would suggest that it is missing)

mertakbal commented 1 year ago

Yes, you are right. The code worked if the .json file is in the same folder.

For all those who need import and export .fbx files from and back to Unity, here is the working code for blender, which translates the mesh and armature in blender +1 unit in Z axis. You might need to check, if your .fbx files are ending with .Fbx or .fbx

import bpy
import os

def clear_scene():
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete()

def import_fbx(filepath):
    bpy.ops.cc3.importer(param="IMPORT", filepath=filepath)

def translate_z(obj, distance):
    obj.location.z += distance

def export_fbx(filepath):
    bpy.ops.cc3.exporter(param="EXPORT_UNITY", include_selected=True, include_textures=True, filepath=filepath, export_face_smoothing=True)

source_directory = "/home/yourSourceFolderPath/"
output_directory = "/home/yourOutputFolderPath/"

if not os.path.exists(output_directory):
    os.makedirs(output_directory)

print("Processing files...")

for file in os.listdir(source_directory):
    if file.endswith(".Fbx"):
        file_path = os.path.join(source_directory, file)

        print(f"Processing {file_path}...")

        clear_scene()
        import_fbx(file_path)

        for obj in bpy.context.visible_objects:
            if obj.type == 'MESH' or obj.type == 'ARMATURE':
                translate_z(obj, 1)

        output_path = os.path.join(output_directory, file)
        export_fbx(output_path)

        print(f"Finished processing {file_path}. Saved to {output_path}.")

print("Finished processing all files.")