Skip to content

Commit

Permalink
Adds a check for namespaces in model names.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kleptine committed Apr 23, 2024
1 parent f09e491 commit f05c48d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ See `checks/` for a detailed list of checks and reasonings. In summary:
- **Is Binary**: Verifies the file is saved in the FBX Binary format. (Blender can't open ASCII files)
- **Bounding Box**: Verifies any given mesh is not massive or tiny. This can cause "Generate Lightmap UVs" in Unity to fail.
- **No Scale Compensation**: Maya animations use Scale Compensation by default. Unity (or any other tool) doesn't support this attribute.
- **No Namespaces**: Requires all model names to be exported without namespaces.

## Usage
```
Expand Down
1 change: 1 addition & 0 deletions fbx-sanitizer/src/checks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ pub mod no_quads;
pub mod root_has_identity_transform;
pub mod units_are_in_meters;
pub mod no_scale_compensation;
pub mod no_namespaces;
35 changes: 35 additions & 0 deletions fbx-sanitizer/src/checks/no_namespaces.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use fbxcel_dom::v7400::object::TypedObjectHandle;
use fbxcel_dom::v7400::Document;
use lazy_static::lazy_static;
use regex::Regex;

lazy_static! {
static ref RE_NAMESPACE: Regex = Regex::new(r"^(.*):.*$").unwrap();
}

/// Depending on your setup, Maya may or may not export namespaces within the names of objects in a
/// model. This can result in mismatches if only certain meshes have namespaces. For example, exporting
/// a rig with namespaces, but an animation pointing at that rig *without* namespaces.
///
/// This check ensures that no namespaces are exported with a mesh. This is a sensible default as
/// namespaces are unnecessary in Unity, and just add noise.
pub fn verify(doc: &Document) -> anyhow::Result<Vec<String>> {
let mut errors = vec![];

for obj in doc.objects() {
if let TypedObjectHandle::Model(model) = obj.get_typed() {
let name = model.name();
match name {
None => {}
Some(name) => {
if RE_NAMESPACE.is_match(name) {
errors.push(format!("Objects should not be exported with namespaces: [{name}]"))
}
}
}

}
}

Ok(errors)
}
5 changes: 5 additions & 0 deletions fbx-sanitizer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use checks::units_are_in_meters;
use checks::bounding_box_size;
use checks::no_scale_compensation;
use itertools::Itertools;
use crate::checks::no_namespaces;

fn main() {
// Custom logging formatting: "[ERROR] Error text."
Expand Down Expand Up @@ -166,6 +167,10 @@ pub fn check_fbx_file(path: &Path, args: &clap::ArgMatches) -> Result<bool, anyh
.entry("Scaling compensation is enabled")
.or_insert(vec![])
.extend(no_scale_compensation::verify(&doc)?);
errors
.entry("Objects should not have namespaces")
.or_insert(vec![])
.extend(no_namespaces::verify(&doc)?);

// Skip quad checks on the High Poly meshes in Raw~. These don't need to be triangulated.
if !is_highpoly {
Expand Down
9 changes: 9 additions & 0 deletions fbx-sanitizer/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,12 @@ fn scale_compensation_is_disabled() {
command.args(&[d]);
command.assert().failure();
}

#[test]
fn file_with_namespaces_fails() {
let mut command = Command::cargo_bin("fbx_sanitizer").unwrap();
let mut d = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
d.push("tests/maya_export_has_namespaces.fbx");
command.args(&[d]);
command.assert().failure();
}
Binary file not shown.

0 comments on commit f05c48d

Please sign in to comment.