Skip to content
Snippets Groups Projects
export_fbx.py 114 KiB
Newer Older
  • Learn to ignore specific revisions
  • 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910
                                    tex = uf.image
                                    textures[tex] = texture_mapping_local[tex] = None
    
                                    try:
                                        mat = mats[f.material_index]
                                    except:
                                        mat = None
    
                                    materials[mat, tex] = material_mapping_local[mat, tex] = None  # should use sets, wait for blender 2.5
    
                        else:
                            for mat in mats:
                                # 2.44 use mat.lib too for uniqueness
                                materials[mat, None] = material_mapping_local[mat, None] = None
                            else:
                                materials[None, None] = None
    
                        if EXP_ARMATURE:
                            armob = ob.find_armature()
                            blenParentBoneName = None
    
                            # parent bone - special case
                            if (not armob) and ob.parent and ob.parent.type == 'ARMATURE' and \
                                    ob.parent_type == 'BONE':
                                armob = ob.parent
                                blenParentBoneName = ob.parent_bone
    
                            if armob and armob not in ob_arms:
                                ob_arms.append(armob)
    
                            # Warning for scaled, mesh objects with armatures
                            if abs(ob.scale[0] - 1.0) > 0.05 or abs(ob.scale[1] - 1.0) > 0.05 or abs(ob.scale[1] - 1.0) > 0.05:
                                operator.report('WARNING', "Object '%s' has a scale of (%.3f, %.3f, %.3f), Armature deformation will not work as expected!, Apply Scale to fix." % ((ob.name,) + tuple(ob.scale)))
    
                        else:
                            blenParentBoneName = armob = None
    
                        my_mesh = my_object_generic(ob, mtx)
                        my_mesh.blenData = me
                        my_mesh.origData = origData
                        my_mesh.blenMaterials = list(material_mapping_local.keys())
                        my_mesh.blenMaterialList = mats
                        my_mesh.blenTextures = list(texture_mapping_local.keys())
    
                        # if only 1 null texture then empty the list
                        if len(my_mesh.blenTextures) == 1 and my_mesh.blenTextures[0] is None:
                            my_mesh.blenTextures = []
    
                        my_mesh.fbxArm = armob  # replace with my_object_generic armature instance later
                        my_mesh.fbxBoneParent = blenParentBoneName  # replace with my_bone instance later
    
                        ob_meshes.append(my_mesh)
    
            # not forgetting to free dupli_list
            if ob_base.dupli_list:
                ob_base.free_dupli_list()
    
        if EXP_ARMATURE:
            # now we have the meshes, restore the rest arm position
            for i, arm in enumerate(bpy.data.armatures):
                arm.pose_position = ob_arms_orig_rest[i]
    
            if ob_arms_orig_rest:
                for ob_base in bpy.data.objects:
                    if ob_base.type == 'ARMATURE':
                        ob_base.update()
                # This causes the makeDisplayList command to effect the mesh
                scene.frame_set(scene.frame_current)
    
        del tmp_ob_type, tmp_objects
    
        # now we have collected all armatures, add bones
        for i, ob in enumerate(ob_arms):
    
            ob_arms[i] = my_arm = my_object_generic(ob)
    
            my_arm.fbxBones = []
            my_arm.blenData = ob.data
            if ob.animation_data:
                my_arm.blenAction = ob.animation_data.action
            else:
                my_arm.blenAction = None
    # 		my_arm.blenAction =		ob.action
            my_arm.blenActionList = []
    
            # fbxName, blenderObject, my_bones, blenderActions
            #ob_arms[i] = fbxArmObName, ob, arm_my_bones, (ob.action, [])
    
            for bone in my_arm.blenData.bones:
                my_bone = my_bone_class(bone, my_arm)
                my_arm.fbxBones.append(my_bone)
                ob_bones.append(my_bone)
    
        # add the meshes to the bones and replace the meshes armature with own armature class
        #for obname, ob, mtx, me, mats, arm, armname in ob_meshes:
        for my_mesh in ob_meshes:
            # Replace
            # ...this could be sped up with dictionary mapping but its unlikely for
            # it ever to be a bottleneck - (would need 100+ meshes using armatures)
            if my_mesh.fbxArm:
                for my_arm in ob_arms:
                    if my_arm.blenObject == my_mesh.fbxArm:
                        my_mesh.fbxArm = my_arm
                        break
    
            for my_bone in ob_bones:
    
                # The mesh uses this bones armature!
                if my_bone.fbxArm == my_mesh.fbxArm:
                    if my_bone.blenBone.use_deform:
                        my_bone.blenMeshes[my_mesh.fbxName] = me
    
                    # parent bone: replace bone names with our class instances
                    # my_mesh.fbxBoneParent is None or a blender bone name initialy, replacing if the names match.
                    if my_mesh.fbxBoneParent == my_bone.blenName:
                        my_mesh.fbxBoneParent = my_bone
    
        bone_deformer_count = 0  # count how many bones deform a mesh
        my_bone_blenParent = None
        for my_bone in ob_bones:
            my_bone_blenParent = my_bone.blenBone.parent
            if my_bone_blenParent:
                for my_bone_parent in ob_bones:
                    # Note 2.45rc2 you can compare bones normally
                    if my_bone_blenParent.name == my_bone_parent.blenName and my_bone.fbxArm == my_bone_parent.fbxArm:
                        my_bone.parent = my_bone_parent
                        break
    
            # Not used at the moment
            # my_bone.calcRestMatrixLocal()
            bone_deformer_count += len(my_bone.blenMeshes)
    
        del my_bone_blenParent
    
        # Build blenObject -> fbxObject mapping
        # this is needed for groups as well as fbxParenting
        bpy.data.objects.tag(False)
    
        # using a list of object names for tagging (Arystan)
    
        tmp_obmapping = {}
        for ob_generic in ob_all_typegroups:
            for ob_base in ob_generic:
                ob_base.blenObject.tag = True
                tmp_obmapping[ob_base.blenObject] = ob_base
    
        # Build Groups from objects we export
        for blenGroup in bpy.data.groups:
            fbxGroupName = None
            for ob in blenGroup.objects:
                if ob.tag:
                    if fbxGroupName is None:
                        fbxGroupName = sane_groupname(blenGroup)
                        groups.append((fbxGroupName, blenGroup))
    
                    tmp_obmapping[ob].fbxGroupNames.append(fbxGroupName)  # also adds to the objects fbxGroupNames
    
        groups.sort()  # not really needed
    
        # Assign parents using this mapping
        for ob_generic in ob_all_typegroups:
            for my_ob in ob_generic:
                parent = my_ob.blenObject.parent
                if parent and parent.tag:  # does it exist and is it in the mapping
                    my_ob.fbxParent = tmp_obmapping[parent]
    
        del tmp_obmapping
        # Finished finding groups we use
    
        materials = [(sane_matname(mat_tex_pair), mat_tex_pair) for mat_tex_pair in materials.keys()]
        textures = [(sane_texname(tex), tex) for tex in textures.keys()  if tex]
        materials.sort()  # sort by name
        textures.sort()
    
        camera_count = 8
        file.write('''
    
    ; Object definitions
    ;------------------------------------------------------------------
    
    Definitions:  {
        Version: 100
        Count: %i''' % (\
            1 + 1 + camera_count + \
            len(ob_meshes) + \
            len(ob_lights) + \
            len(ob_cameras) + \
            len(ob_arms) + \
            len(ob_null) + \
            len(ob_bones) + \
            bone_deformer_count + \
            len(materials) + \
            (len(textures) * 2)))  # add 1 for the root model 1 for global settings
    
        del bone_deformer_count
    
        file.write('''
        ObjectType: "Model" {
            Count: %i
        }''' % (\
            1 + camera_count + \
            len(ob_meshes) + \
            len(ob_lights) + \
            len(ob_cameras) + \
            len(ob_arms) + \
            len(ob_null) + \
            len(ob_bones)))  # add 1 for the root model
    
        file.write('''
        ObjectType: "Geometry" {
            Count: %i
        }''' % len(ob_meshes))
    
        if materials:
            file.write('''
        ObjectType: "Material" {
            Count: %i
        }''' % len(materials))
    
        if textures:
            file.write('''
        ObjectType: "Texture" {
            Count: %i
        }''' % len(textures))  # add 1 for an empty tex
            file.write('''
        ObjectType: "Video" {
            Count: %i
        }''' % len(textures))  # add 1 for an empty tex
    
        tmp = 0
        # Add deformer nodes
        for my_mesh in ob_meshes:
            if my_mesh.fbxArm:
                tmp += 1
    
        # Add subdeformers
        for my_bone in ob_bones:
            tmp += len(my_bone.blenMeshes)
    
        if tmp:
            file.write('''
        ObjectType: "Deformer" {
            Count: %i
        }''' % tmp)
        del tmp
    
        # we could avoid writing this possibly but for now just write it
    
        file.write('''
        ObjectType: "Pose" {
            Count: 1
        }''')
    
        if groups:
            file.write('''
        ObjectType: "GroupSelection" {
            Count: %i
        }''' % len(groups))
    
        file.write('''
        ObjectType: "GlobalSettings" {
            Count: 1
        }
    }''')
    
        file.write('''
    
    ; Object properties
    ;------------------------------------------------------------------
    
    Objects:  {''')
    
        # To comply with other FBX FILES
        write_camera_switch()
    
        # Write the null object
        write_null(None, 'blend_root')  # , GLOBAL_MATRIX)
    
        for my_null in ob_null:
            write_null(my_null)
    
        for my_arm in ob_arms:
            write_null(my_arm)
    
        for my_cam in ob_cameras:
            write_camera(my_cam)
    
        for my_light in ob_lights:
            write_light(my_light)
    
        for my_mesh in ob_meshes:
            write_mesh(my_mesh)
    
        #for bonename, bone, obname, me, armob in ob_bones:
        for my_bone in ob_bones:
            write_bone(my_bone)
    
        write_camera_default()
    
        for matname, (mat, tex) in materials:
            write_material(matname, mat)  # We only need to have a material per image pair, but no need to write any image info into the material (dumb fbx standard)
    
        # each texture uses a video, odd
        for texname, tex in textures:
            write_video(texname, tex)
        i = 0
        for texname, tex in textures:
            write_texture(texname, tex, i)
            i += 1
    
        for groupname, group in groups:
            write_group(groupname)
    
        # NOTE - c4d and motionbuilder dont need normalized weights, but deep-exploration 5 does and (max?) do.
    
        # Write armature modifiers
        # TODO - add another MODEL? - because of this skin definition.
        for my_mesh in ob_meshes:
            if my_mesh.fbxArm:
                write_deformer_skin(my_mesh.fbxName)
    
                # Get normalized weights for temorary use
                if my_mesh.fbxBoneParent:
                    weights = None
                else:
                    weights = meshNormalizedWeights(my_mesh.blenObject, my_mesh.blenData)
    
                #for bonename, bone, obname, bone_mesh, armob in ob_bones:
                for my_bone in ob_bones:
                    if me in iter(my_bone.blenMeshes.values()):
                        write_sub_deformer_skin(my_mesh, my_bone, weights)
    
        # Write pose's really weired, only needed when an armature and mesh are used together
        # each by themselves dont need pose data. for now only pose meshes and bones
    
        file.write('''
        Pose: "Pose::BIND_POSES", "BindPose" {
            Type: "BindPose"
            Version: 100
            Properties60:  {
            }
            NbPoseNodes: ''')
        file.write(str(len(pose_items)))
    
        for fbxName, matrix in pose_items:
            file.write('\n\t\tPoseNode:  {')
            file.write('\n\t\t\tNode: "Model::%s"' % fbxName)
            file.write('\n\t\t\tMatrix: %s' % mat4x4str(matrix if matrix else Matrix()))
            file.write('\n\t\t}')
    
        file.write('\n\t}')
    
        # Finish Writing Objects
        # Write global settings
        file.write('''
        GlobalSettings:  {
            Version: 1000
            Properties60:  {
                Property: "UpAxis", "int", "",1
                Property: "UpAxisSign", "int", "",1
                Property: "FrontAxis", "int", "",2
                Property: "FrontAxisSign", "int", "",1
                Property: "CoordAxis", "int", "",0
                Property: "CoordAxisSign", "int", "",1
                Property: "UnitScaleFactor", "double", "",100
            }
        }
    ''')
        file.write('}')
    
        file.write('''
    
    ; Object relations
    ;------------------------------------------------------------------
    
    Relations:  {''')
    
        file.write('\n\tModel: "Model::blend_root", "Null" {\n\t}')
    
        for my_null in ob_null:
            file.write('\n\tModel: "Model::%s", "Null" {\n\t}' % my_null.fbxName)
    
        for my_arm in ob_arms:
            file.write('\n\tModel: "Model::%s", "Null" {\n\t}' % my_arm.fbxName)
    
        for my_mesh in ob_meshes:
            file.write('\n\tModel: "Model::%s", "Mesh" {\n\t}' % my_mesh.fbxName)
    
        # TODO - limbs can have the same name for multiple armatures, should prefix.
        #for bonename, bone, obname, me, armob in ob_bones:
        for my_bone in ob_bones:
            file.write('\n\tModel: "Model::%s", "Limb" {\n\t}' % my_bone.fbxName)
    
        for my_cam in ob_cameras:
            file.write('\n\tModel: "Model::%s", "Camera" {\n\t}' % my_cam.fbxName)
    
        for my_light in ob_lights:
            file.write('\n\tModel: "Model::%s", "Light" {\n\t}' % my_light.fbxName)
    
        file.write('''
        Model: "Model::Producer Perspective", "Camera" {
        }
        Model: "Model::Producer Top", "Camera" {
        }
        Model: "Model::Producer Bottom", "Camera" {
        }
        Model: "Model::Producer Front", "Camera" {
        }
        Model: "Model::Producer Back", "Camera" {
        }
        Model: "Model::Producer Right", "Camera" {
        }
        Model: "Model::Producer Left", "Camera" {
        }
        Model: "Model::Camera Switcher", "CameraSwitcher" {
        }''')
    
        for matname, (mat, tex) in materials:
            file.write('\n\tMaterial: "Material::%s", "" {\n\t}' % matname)
    
        if textures:
            for texname, tex in textures:
                file.write('\n\tTexture: "Texture::%s", "TextureVideoClip" {\n\t}' % texname)
            for texname, tex in textures:
                file.write('\n\tVideo: "Video::%s", "Clip" {\n\t}' % texname)
    
        # deformers - modifiers
        for my_mesh in ob_meshes:
            if my_mesh.fbxArm:
                file.write('\n\tDeformer: "Deformer::Skin %s", "Skin" {\n\t}' % my_mesh.fbxName)
    
        #for bonename, bone, obname, me, armob in ob_bones:
        for my_bone in ob_bones:
            for fbxMeshObName in my_bone.blenMeshes:  # .keys() - fbxMeshObName
                # is this bone effecting a mesh?
                file.write('\n\tDeformer: "SubDeformer::Cluster %s %s", "Cluster" {\n\t}' % (fbxMeshObName, my_bone.fbxName))
    
        # This should be at the end
        # file.write('\n\tPose: "Pose::BIND_POSES", "BindPose" {\n\t}')
    
        for groupname, group in groups:
            file.write('\n\tGroupSelection: "GroupSelection::%s", "Default" {\n\t}' % groupname)
    
        file.write('\n}')
        file.write('''
    
    ; Object connections
    ;------------------------------------------------------------------
    
    Connections:  {''')
    
        # NOTE - The FBX SDK dosnt care about the order but some importers DO!
        # for instance, defining the material->mesh connection
        # before the mesh->blend_root crashes cinema4d
    
        # write the fake root node
        file.write('\n\tConnect: "OO", "Model::blend_root", "Model::Scene"')
    
        for ob_generic in ob_all_typegroups:  # all blender 'Object's we support
            for my_ob in ob_generic:
                if my_ob.fbxParent:
                    file.write('\n\tConnect: "OO", "Model::%s", "Model::%s"' % (my_ob.fbxName, my_ob.fbxParent.fbxName))
                else:
                    file.write('\n\tConnect: "OO", "Model::%s", "Model::blend_root"' % my_ob.fbxName)
    
        if materials:
            for my_mesh in ob_meshes:
                # Connect all materials to all objects, not good form but ok for now.
                for mat, tex in my_mesh.blenMaterials:
                    mat_name = mat.name if mat else None
                    tex_name = tex.name if tex else None
    
                    file.write('\n\tConnect: "OO", "Material::%s", "Model::%s"' % (sane_name_mapping_mat[mat_name, tex_name], my_mesh.fbxName))
    
        if textures:
            for my_mesh in ob_meshes:
                if my_mesh.blenTextures:
                    # file.write('\n\tConnect: "OO", "Texture::_empty_", "Model::%s"' % my_mesh.fbxName)
                    for tex in my_mesh.blenTextures:
                        if tex:
                            file.write('\n\tConnect: "OO", "Texture::%s", "Model::%s"' % (sane_name_mapping_tex[tex.name], my_mesh.fbxName))
    
            for texname, tex in textures:
                file.write('\n\tConnect: "OO", "Video::%s", "Texture::%s"' % (texname, texname))
    
        for my_mesh in ob_meshes:
            if my_mesh.fbxArm:
                file.write('\n\tConnect: "OO", "Deformer::Skin %s", "Model::%s"' % (my_mesh.fbxName, my_mesh.fbxName))
    
        #for bonename, bone, obname, me, armob in ob_bones:
        for my_bone in ob_bones:
            for fbxMeshObName in my_bone.blenMeshes:  # .keys()
                file.write('\n\tConnect: "OO", "SubDeformer::Cluster %s %s", "Deformer::Skin %s"' % (fbxMeshObName, my_bone.fbxName, fbxMeshObName))
    
        # limbs -> deformers
        # for bonename, bone, obname, me, armob in ob_bones:
        for my_bone in ob_bones:
            for fbxMeshObName in my_bone.blenMeshes:  # .keys()
                file.write('\n\tConnect: "OO", "Model::%s", "SubDeformer::Cluster %s %s"' % (my_bone.fbxName, fbxMeshObName, my_bone.fbxName))
    
        #for bonename, bone, obname, me, armob in ob_bones:
        for my_bone in ob_bones:
            # Always parent to armature now
            if my_bone.parent:
                file.write('\n\tConnect: "OO", "Model::%s", "Model::%s"' % (my_bone.fbxName, my_bone.parent.fbxName))
            else:
                # the armature object is written as an empty and all root level bones connect to it
                file.write('\n\tConnect: "OO", "Model::%s", "Model::%s"' % (my_bone.fbxName, my_bone.fbxArm.fbxName))
    
        # groups
        if groups:
            for ob_generic in ob_all_typegroups:
                for ob_base in ob_generic:
                    for fbxGroupName in ob_base.fbxGroupNames:
                        file.write('\n\tConnect: "OO", "Model::%s", "GroupSelection::%s"' % (ob_base.fbxName, fbxGroupName))
    
        for my_arm in ob_arms:
            file.write('\n\tConnect: "OO", "Model::%s", "Model::blend_root"' % my_arm.fbxName)
    
        file.write('\n}')
    
        # Needed for scene footer as well as animation
        render = scene.render
    
        # from the FBX sdk
        #define KTIME_ONE_SECOND        KTime (K_LONGLONG(46186158000))
        def fbx_time(t):
            # 0.5 + val is the same as rounding.
            return int(0.5 + ((t / fps) * 46186158000))
    
        fps = float(render.fps)
        start = scene.frame_start
        end = scene.frame_end
        if end < start:
            start, end = end, st
    
        # comment the following line, otherwise we dont get the pose
        # if start==end: ANIM_ENABLE = False
    
        # animations for these object types
        ob_anim_lists = ob_bones, ob_meshes, ob_null, ob_cameras, ob_lights, ob_arms
    
        if ANIM_ENABLE and [tmp for tmp in ob_anim_lists if tmp]:
    
            frame_orig = scene.frame_current
    
            if ANIM_OPTIMIZE:
                ANIM_OPTIMIZE_PRECISSION_FLOAT = 0.1 ** ANIM_OPTIMIZE_PRECISSION
    
            # default action, when no actions are avaioable
            tmp_actions = []
            blenActionDefault = None
            action_lastcompat = None
    
            # instead of tagging
            tagged_actions = []
    
            if ANIM_ACTION_ALL:
    # 			bpy.data.actions.tag = False
                tmp_actions = bpy.data.actions[:]
    
                # find which actions are compatible with the armatures
                # blenActions is not yet initialized so do it now.
                tmp_act_count = 0
                for my_arm in ob_arms:
    
                    # get the default name
                    if not blenActionDefault:
                        blenActionDefault = my_arm.blenAction
    
                    arm_bone_names = set([my_bone.blenName for my_bone in my_arm.fbxBones])
    
                    for action in tmp_actions:
    
                        action_chan_names = arm_bone_names.intersection(set([g.name for g in action.groups]))
    # 					action_chan_names = arm_bone_names.intersection( set(action.getChannelNames()) )
    
                        if action_chan_names:  # at least one channel matches.
                            my_arm.blenActionList.append(action)
                            tagged_actions.append(action.name)
    # 						action.tag = True
                            tmp_act_count += 1
    
                            # incase there is no actions applied to armatures
                            action_lastcompat = action
    
                if tmp_act_count:
                    # unlikely to ever happen but if no actions applied to armatures, just use the last compatible armature.
                    if not blenActionDefault:
                        blenActionDefault = action_lastcompat
    
            del action_lastcompat
    
            tmp_actions.insert(0, None)  # None is the default action
    
            file.write('''
    ;Takes and animation section
    ;----------------------------------------------------
    
    Takes:  {''')
    
            if blenActionDefault:
                file.write('\n\tCurrent: "%s"' % sane_takename(blenActionDefault))
            else:
                file.write('\n\tCurrent: "Default Take"')
    
            for blenAction in tmp_actions:
                # we have tagged all actious that are used be selected armatures
                if blenAction:
                    if blenAction.name in tagged_actions:
    # 				if blenAction.tag:
                        print('\taction: "%s" exporting...' % blenAction.name)
                    else:
                        print('\taction: "%s" has no armature using it, skipping' % blenAction.name)
                        continue
    
                if blenAction is None:
                    # Warning, this only accounts for tmp_actions being [None]
                    file.write('\n\tTake: "Default Take" {')
                    act_start = start
                    act_end = end
                else:
                    # use existing name
                    if blenAction == blenActionDefault:  # have we already got the name
                        file.write('\n\tTake: "%s" {' % sane_name_mapping_take[blenAction.name])
                    else:
                        file.write('\n\tTake: "%s" {' % sane_takename(blenAction))
    
                    act_start, act_end = blenAction.frame_range
                    act_start = int(act_start)
                    act_end = int(act_end)
    
                    # Set the action active
                    for my_arm in ob_arms:
                        if my_arm.blenObject.animation_data and blenAction in my_arm.blenActionList:
                            my_arm.blenObject.animation_data.action = blenAction
                            # print('\t\tSetting Action!', blenAction)
                    # scene.update(1)
    
                file.write('\n\t\tFileName: "Default_Take.tak"')  # ??? - not sure why this is needed
                file.write('\n\t\tLocalTime: %i,%i' % (fbx_time(act_start - 1), fbx_time(act_end - 1)))  # ??? - not sure why this is needed
                file.write('\n\t\tReferenceTime: %i,%i' % (fbx_time(act_start - 1), fbx_time(act_end - 1)))  # ??? - not sure why this is needed
    
                file.write('''
    
            ;Models animation
            ;----------------------------------------------------''')
    
                # set pose data for all bones
                # do this here incase the action changes
                '''
                for my_bone in ob_bones:
                    my_bone.flushAnimData()
                '''
                i = act_start
                while i <= act_end:
                    scene.frame_set(i)
                    for ob_generic in ob_anim_lists:
                        for my_ob in ob_generic:
                            #Blender.Window.RedrawAll()
                            if ob_generic == ob_meshes and my_ob.fbxArm:
                                # We cant animate armature meshes!
                                my_ob.setPoseFrame(i, fake=True)
                            else:
                                my_ob.setPoseFrame(i)
    
                    i += 1
    
                #for bonename, bone, obname, me, armob in ob_bones:
                for ob_generic in (ob_bones, ob_meshes, ob_null, ob_cameras, ob_lights, ob_arms):
    
                    for my_ob in ob_generic:
    
                        if ob_generic == ob_meshes and my_ob.fbxArm:
                            # do nothing,
                            pass
                        else:
    
                            file.write('\n\t\tModel: "Model::%s" {' % my_ob.fbxName)  # ??? - not sure why this is needed
                            file.write('\n\t\t\tVersion: 1.1')
                            file.write('\n\t\t\tChannel: "Transform" {')
    
                            context_bone_anim_mats = [(my_ob.getAnimParRelMatrix(frame), my_ob.getAnimParRelMatrixRot(frame)) for frame in range(act_start, act_end + 1)]
    
                            # ----------------
                            # ----------------
                            for TX_LAYER, TX_CHAN in enumerate('TRS'):  # transform, rotate, scale
    
                                if TX_CHAN == 'T':
                                    context_bone_anim_vecs = [mtx[0].translation_part() for mtx in context_bone_anim_mats]
                                elif	TX_CHAN == 'S':
                                    context_bone_anim_vecs = [mtx[0].scale_part() for mtx in context_bone_anim_mats]
                                elif	TX_CHAN == 'R':
                                    # Was....
                                    # elif 	TX_CHAN=='R':	context_bone_anim_vecs = [mtx[1].to_euler()			for mtx in context_bone_anim_mats]
                                    #
                                    # ...but we need to use the previous euler for compatible conversion.
                                    context_bone_anim_vecs = []
                                    prev_eul = None
                                    for mtx in context_bone_anim_mats:
                                        if prev_eul:
                                            prev_eul = mtx[1].to_euler('XYZ', prev_eul)
                                        else:
                                            prev_eul = mtx[1].to_euler()
                                        context_bone_anim_vecs.append(tuple_rad_to_deg(prev_eul))
    
                                file.write('\n\t\t\t\tChannel: "%s" {' % TX_CHAN)  # translation
    
                                for i in range(3):
                                    # Loop on each axis of the bone
                                    file.write('\n\t\t\t\t\tChannel: "%s" {' % ('XYZ'[i]))  # translation
                                    file.write('\n\t\t\t\t\t\tDefault: %.15f' % context_bone_anim_vecs[0][i])
                                    file.write('\n\t\t\t\t\t\tKeyVer: 4005')
    
                                    if not ANIM_OPTIMIZE:
                                        # Just write all frames, simple but in-eficient
                                        file.write('\n\t\t\t\t\t\tKeyCount: %i' % (1 + act_end - act_start))
                                        file.write('\n\t\t\t\t\t\tKey: ')
                                        frame = act_start
                                        while frame <= act_end:
                                            if frame != act_start:
                                                file.write(',')
    
                                            # Curve types are 'C,n' for constant, 'L' for linear
                                            # C,n is for bezier? - linear is best for now so we can do simple keyframe removal
                                            file.write('\n\t\t\t\t\t\t\t%i,%.15f,L' % (fbx_time(frame - 1), context_bone_anim_vecs[frame - act_start][i]))
                                            frame += 1
                                    else:
                                        # remove unneeded keys, j is the frame, needed when some frames are removed.
                                        context_bone_anim_keys = [(vec[i], j) for j, vec in enumerate(context_bone_anim_vecs)]
    
                                        # last frame to fisrt frame, missing 1 frame on either side.
                                        # removeing in a backwards loop is faster
                                        #for j in xrange( (act_end-act_start)-1, 0, -1 ):
                                        # j = (act_end-act_start)-1
                                        j = len(context_bone_anim_keys) - 2
                                        while j > 0 and len(context_bone_anim_keys) > 2:
                                            # print j, len(context_bone_anim_keys)
                                            # Is this key the same as the ones next to it?
    
                                            # co-linear horizontal...
                                            if		abs(context_bone_anim_keys[j][0] - context_bone_anim_keys[j - 1][0]) < ANIM_OPTIMIZE_PRECISSION_FLOAT and \
                                                    abs(context_bone_anim_keys[j][0] - context_bone_anim_keys[j + 1][0]) < ANIM_OPTIMIZE_PRECISSION_FLOAT:
    
                                                del context_bone_anim_keys[j]
    
                                            else:
                                                frame_range = float(context_bone_anim_keys[j + 1][1] - context_bone_anim_keys[j - 1][1])
                                                frame_range_fac1 = (context_bone_anim_keys[j + 1][1] - context_bone_anim_keys[j][1]) / frame_range
                                                frame_range_fac2 = 1.0 - frame_range_fac1
    
                                                if abs(((context_bone_anim_keys[j - 1][0] * frame_range_fac1 + context_bone_anim_keys[j + 1][0] * frame_range_fac2)) - context_bone_anim_keys[j][0]) < ANIM_OPTIMIZE_PRECISSION_FLOAT:
                                                    del context_bone_anim_keys[j]
                                                else:
                                                    j -= 1
    
                                            # keep the index below the list length
                                            if j > len(context_bone_anim_keys) - 2:
                                                j = len(context_bone_anim_keys) - 2
    
                                        if len(context_bone_anim_keys) == 2 and context_bone_anim_keys[0][0] == context_bone_anim_keys[1][0]:
    
                                            # This axis has no moton, its okay to skip KeyCount and Keys in this case
                                            # pass
    
                                            # better write one, otherwise we loose poses with no animation
                                            file.write('\n\t\t\t\t\t\tKeyCount: 1')
                                            file.write('\n\t\t\t\t\t\tKey: ')
                                            file.write('\n\t\t\t\t\t\t\t%i,%.15f,L' % (fbx_time(start), context_bone_anim_keys[0][0]))
                                        else:
                                            # We only need to write these if there is at least one
                                            file.write('\n\t\t\t\t\t\tKeyCount: %i' % len(context_bone_anim_keys))
                                            file.write('\n\t\t\t\t\t\tKey: ')
                                            for val, frame in context_bone_anim_keys:
                                                if frame != context_bone_anim_keys[0][1]:  # not the first
                                                    file.write(',')
                                                # frame is already one less then blenders frame
                                                file.write('\n\t\t\t\t\t\t\t%i,%.15f,L' % (fbx_time(frame), val))
    
                                    if i == 0:
                                        file.write('\n\t\t\t\t\t\tColor: 1,0,0')
                                    elif i == 1:
                                        file.write('\n\t\t\t\t\t\tColor: 0,1,0')
                                    elif i == 2:
                                        file.write('\n\t\t\t\t\t\tColor: 0,0,1')
    
                                    file.write('\n\t\t\t\t\t}')
                                file.write('\n\t\t\t\t\tLayerType: %i' % (TX_LAYER + 1))
                                file.write('\n\t\t\t\t}')
    
                            # ---------------
    
                            file.write('\n\t\t\t}')
                            file.write('\n\t\t}')
    
                # end the take
                file.write('\n\t}')
    
                # end action loop. set original actions
                # do this after every loop incase actions effect eachother.
                for my_arm in ob_arms:
                    if my_arm.blenObject.animation_data:
                        my_arm.blenObject.animation_data.action = my_arm.blenAction
    
            file.write('\n}')
    
            scene.frame_set(frame_orig)
    
        else:
            # no animation
            file.write('\n;Takes and animation section')
            file.write('\n;----------------------------------------------------')
            file.write('\n')
            file.write('\nTakes:  {')
            file.write('\n\tCurrent: ""')
            file.write('\n}')
    
        # write meshes animation
        #for obname, ob, mtx, me, mats, arm, armname in ob_meshes:
    
        # Clear mesh data Only when writing with modifiers applied
        for me in meshes_to_clear:
            bpy.data.meshes.remove(me)
    
        # --------------------------- Footer
        if world:
            m = world.mist_settings
            has_mist = m.use_mist
            mist_intense = m.intensity
            mist_start = m.start
            mist_end = m.depth
            mist_height = m.height
            world_hor = world.horizon_color
        else:
            has_mist = mist_intense = mist_start = mist_end = mist_height = 0
            world_hor = 0, 0, 0
    
        file.write('\n;Version 5 settings')
        file.write('\n;------------------------------------------------------------------')
        file.write('\n')
        file.write('\nVersion5:  {')
        file.write('\n\tAmbientRenderSettings:  {')
        file.write('\n\t\tVersion: 101')
        file.write('\n\t\tAmbientLightColor: %.1f,%.1f,%.1f,0' % tuple(world_amb))
        file.write('\n\t}')
        file.write('\n\tFogOptions:  {')
        file.write('\n\t\tFlogEnable: %i' % has_mist)
        file.write('\n\t\tFogMode: 0')
        file.write('\n\t\tFogDensity: %.3f' % mist_intense)
        file.write('\n\t\tFogStart: %.3f' % mist_start)
        file.write('\n\t\tFogEnd: %.3f' % mist_end)
        file.write('\n\t\tFogColor: %.1f,%.1f,%.1f,1' % tuple(world_hor))
        file.write('\n\t}')
        file.write('\n\tSettings:  {')
        file.write('\n\t\tFrameRate: "%i"' % int(fps))
        file.write('\n\t\tTimeFormat: 1')
        file.write('\n\t\tSnapOnFrames: 0')
        file.write('\n\t\tReferenceTimeIndex: -1')
        file.write('\n\t\tTimeLineStartTime: %i' % fbx_time(start - 1))
        file.write('\n\t\tTimeLineStopTime: %i' % fbx_time(end - 1))
        file.write('\n\t}')
        file.write('\n\tRendererSetting:  {')
        file.write('\n\t\tDefaultCamera: "Producer Perspective"')
        file.write('\n\t\tDefaultViewingMode: 0')
        file.write('\n\t}')
        file.write('\n}')
        file.write('\n')
    
        # XXX, shouldnt be global!
        sane_name_mapping_ob.clear()
        sane_name_mapping_mat.clear()
        sane_name_mapping_tex.clear()
        sane_name_mapping_take.clear()
        sane_name_mapping_group.clear()
    
        ob_arms[:] = []
        ob_bones[:] = []
        ob_cameras[:] = []
        ob_lights[:] = []
        ob_meshes[:] = []
        ob_null[:] = []
    
        # copy images if enabled
    # 	if EXP_IMAGE_COPY:
    # # 		copy_images( basepath,  [ tex[1] for tex in textures if tex[1] != None ])
    # 		bpy.util.copy_images( [ tex[1] for tex in textures if tex[1] != None ], basepath)
        file.close()
    
        print('export finished in %.4f sec.' % (time.clock() - start_time))
        return {'FINISHED'}
    
    
    # NOTES (all line numbers correspond to original export_fbx.py (under release/scripts)
    # - Draw.PupMenu alternative in 2.5?, temporarily replaced PupMenu with print
    # - get rid of bpy.path.clean_name somehow
    # + fixed: isinstance(inst, bpy.types.*) doesn't work on RNA objects: line 565
    # + get rid of BPyObject_getObjectArmature, move it in RNA?
    # - BATCH_ENABLE and BATCH_GROUP options: line 327
    # - implement all BPyMesh_* used here with RNA
    # - getDerivedObjects is not fully replicated with .dupli* funcs
    # - talk to Campbell, this code won't work? lines 1867-1875
    # - don't know what those colbits are, do we need them? they're said to be deprecated in DNA_object_types.h: 1886-1893
    # - no hq normals: 1900-1901
    
    # TODO
    
    # - bpy.data.remove_scene: line 366
    # - bpy.sys.time move to bpy.sys.util?
    # - new scene creation, activation: lines 327-342, 368
    # - uses bpy.path.abspath, *.relpath - replace at least relpath