How to get some attributes in the fbx file through python

How to get some attributes in the fbx file through python

410299258
Contributor Contributor
2,254 Views
5 Replies
Message 1 of 6

How to get some attributes in the fbx file through python

410299258
Contributor
Contributor

How to get some attributes in the fbx file through python
Hi, I want to get some attributes of my fbx file through python
1. Get the file name of the fbx file
2. Time length of obtaining animation data in fbx
3. Get the number of bones named "Hips" in fbx

 

0 Likes
2,255 Views
5 Replies
Replies (5)
Message 2 of 6

RyanCameron
Advocate
Advocate

My python is rusty but I'll take a shot at it. I have not tested this, its meant to get you started.

import fbx

# Create an FBX manager and scene
manager = fbx.FbxManager.Create()
scene = fbx.FbxScene.Create(manager, "")

# Import the FBX file
importer = fbx.FbxImporter.Create(manager, "")
importer.Initialize("your_fbx_file_path")
importer.Import(scene)

# Get the file name
file_name = importer.GetFileName()

# Get the time length of the animation
animation_length = scene.GetSrcObjectCount(fbx.FbxCriteria.ObjectType(fbx.FbxAnimStack.ClassId))
if animation_length > 0:
animation_length = scene.GetSrcObject(fbx.FbxCriteria.ObjectType(fbx.FbxAnimStack.ClassId), 0).LocalTimeSpan.GetDuration().Get()

# Get the number of bones named "Hips"
bone_count = 0
root_node = scene.GetRootNode()
if root_node:
for i in range(root_node.GetChildCount()):
child_node = root_node.GetChild(i)
if child_node.GetNodeAttribute() and child_node.GetNodeAttribute().GetAttributeType() == fbx.FbxNodeAttribute.eSkeleton:
if child_node.GetName() == "Hips":
bone_count += 1

# Clean up
importer.Destroy()
scene.Destroy()
manager.Destroy()

# Print the results
print("File name:", file_name)
print("Animation length:", animation_length)
print("Number of bones named 'Hips':", bone_count)

 

I imagine you'd need to replace "your_fbx_file_path" with the file path of your FBX file.

RB Cameron, AIA, LEED AP, EDAC
Digital Practice Leader

Message 3 of 6

410299258
Contributor
Contributor
This error is encountered when obtaining the time length

animation_length = scene.GetSrcObject(fbx.FbxCriteria.ObjectType(fbx.FbxAnimStack.ClassId), 0).LocalTimeSpan.GetDuration().Get()
AttributeError: 'FbxAnimStack' object has no attribute 'LocalTimeSpan'
0 Likes
Message 4 of 6

410299258
Contributor
Contributor

I need this

time length = (get end - get start)/get fps

 

2023-03-13_204203.jpg

 

0 Likes
Message 5 of 6

741041743
Community Visitor
Community Visitor
Where does your api information come from。I also want to check
0 Likes
Message 6 of 6

410299258
Contributor
Contributor

I tried to modify this, but the value I got was not what I wanted

2023-03-14_111938.jpg

0 Likes