Skip to content

Instantly share code, notes, and snippets.

@aras-p
Created February 8, 2023 10:10
Show Gist options
  • Save aras-p/78b8b4e3ea3cfe3af0b6568563e8a062 to your computer and use it in GitHub Desktop.
Save aras-p/78b8b4e3ea3cfe3af0b6568563e8a062 to your computer and use it in GitHub Desktop.
Blender FBX Visibility export
diff --git a/io_scene_fbx/export_fbx_bin.py b/io_scene_fbx/export_fbx_bin.py
index c5cd93a7..f15b3a8a 100644
--- a/io_scene_fbx/export_fbx_bin.py
+++ b/io_scene_fbx/export_fbx_bin.py
@@ -1688,7 +1688,8 @@ def fbx_data_object_elements(root, ob_obj, scene_data):
animatable=True, animated=((ob_obj.key, "Lcl Rotation") in scene_data.animated))
elem_props_template_set(tmpl, props, "p_lcl_scaling", b"Lcl Scaling", scale,
animatable=True, animated=((ob_obj.key, "Lcl Scaling") in scene_data.animated))
- elem_props_template_set(tmpl, props, "p_visibility", b"Visibility", float(not ob_obj.hide))
+ elem_props_template_set(tmpl, props, "p_visibility", b"Visibility", float(not ob_obj.hide),
+ animatable=True, animated=((ob_obj.key, "Visibility") in scene_data.animated))
# Absolutely no idea what this is, but seems mandatory for validity of the file, and defaults to
# invalid -1 value...
@@ -1976,7 +1977,8 @@ def fbx_animations_do(scene_data, ref_id, f_start, f_end, start_zero, objects=No
force_key = (simplify_fac == 0.0) or (ob_obj.is_bone and force_keying)
animdata_ob[ob_obj] = (ACNW(ob_obj.key, 'LCL_TRANSLATION', force_key, force_sek, loc),
ACNW(ob_obj.key, 'LCL_ROTATION', force_key, force_sek, rot_deg),
- ACNW(ob_obj.key, 'LCL_SCALING', force_key, force_sek, scale))
+ ACNW(ob_obj.key, 'LCL_SCALING', force_key, force_sek, scale),
+ ACNW(ob_obj.key, 'VISIBILITY', force_key, force_sek, (float(not ob_obj.hide),)))
p_rots[ob_obj] = rot
force_key = (simplify_fac == 0.0)
@@ -2007,7 +2009,7 @@ def fbx_animations_do(scene_data, ref_id, f_start, f_end, start_zero, objects=No
for dp_obj in ob_obj.dupli_list_gen(depsgraph):
pass # Merely updating dupli matrix of ObjectWrapper...
- for ob_obj, (anim_loc, anim_rot, anim_scale) in animdata_ob.items():
+ for ob_obj, (anim_loc, anim_rot, anim_scale, anim_vis) in animdata_ob.items():
# We compute baked loc/rot/scale for all objects (rot being euler-compat with previous value!).
p_rot = p_rots.get(ob_obj, None)
loc, rot, scale, _m, _mr = ob_obj.fbx_object_tx(scene_data, rot_euler_compat=p_rot)
@@ -2015,6 +2017,7 @@ def fbx_animations_do(scene_data, ref_id, f_start, f_end, start_zero, objects=No
anim_loc.add_keyframe(real_currframe, loc)
anim_rot.add_keyframe(real_currframe, tuple(convert_rad_to_deg_iter(rot)))
anim_scale.add_keyframe(real_currframe, scale)
+ anim_vis.add_keyframe(real_currframe, (float(not ob_obj.hide),))
for anim_shape, me, shape in animdata_shapes.values():
anim_shape.add_keyframe(real_currframe, (shape.value * 100.0,))
for anim_camera_lens, anim_camera_focus_distance, camera in animdata_cameras.values():
diff --git a/io_scene_fbx/fbx_utils.py b/io_scene_fbx/fbx_utils.py
index e52cd9eb..0189d4c0 100644
--- a/io_scene_fbx/fbx_utils.py
+++ b/io_scene_fbx/fbx_utils.py
@@ -722,6 +722,7 @@ class AnimationCurveNodeWrapper:
'LCL_TRANSLATION': ("Lcl Translation", "T", ("X", "Y", "Z")),
'LCL_ROTATION': ("Lcl Rotation", "R", ("X", "Y", "Z")),
'LCL_SCALING': ("Lcl Scaling", "S", ("X", "Y", "Z")),
+ 'VISIBILITY': ("Visibility", "Visibility", ("Visibility",)),
'SHAPE_KEY': ("DeformPercent", "DeformPercent", ("DeformPercent",)),
'CAMERA_FOCAL': ("FocalLength", "FocalLength", ("FocalLength",)),
'CAMERA_FOCUS_DISTANCE': ("FocusDistance", "FocusDistance", ("FocusDistance",)),
@@ -978,7 +979,7 @@ class ObjectWrapper(metaclass=MetaObjectWrapper):
# XXX Not sure how much that’s useful now... :/
def get_hide(self):
- return self.bdata.hide_viewport if self._tag in {'OB', 'DP'} else self.bdata.hide
+ return (not self.bdata.visible_get()) if self._tag in {'OB', 'DP'} else self.bdata.hide
hide = property(get_hide)
def get_parent(self):
~/code/blender/blender/release/scripts/addons/io_scene_fbx %
@luducn
Copy link

luducn commented Feb 24, 2024

I modified the code by removing the red text and adding the green text. How do I export visibility animation in Blender? Currently, I am adding instance keyframes in Blender.
![image](https://gist.github.com/assets/61106226/32c72cc1-d0e8-4d11-b172-6b7d9a
屏幕截图 2024-02-24 161320
f7e6f5)

@Wilenino
Copy link

Hello,

Does the modification you created to export the Blender visibility parameter to .fbx work for version 4.1? I'm working on an animation that shows how a building is constructed, and I can't export it as .fbx. Additionally, I'm not very familiar with modifying Blender code. Could you please explain how I can add this modification? I would really appreciate it, as I need to export that parameter for the animation I want to use in Unity.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment