Page 6 of 6

Re: [Noesis] Plugin to export to a new format

Posted: Fri Jul 12, 2013 2:02 am
by MrAdults
You probably want to use FBX as your main export target, it supports the most data types and material properties. When you're exporting back to your format, you just flatten the bone list (you can use the bone matrices as-is since they're in model space, but you have to use rpgMultiplyBones on animation bone sets because anims coming out of Noesis will be parent-relative, and you want to support exporting skeletons that *aren't* flattened to your format just to be nice)

What I would do is run through the model and build unique meshes from bone references. So each collection of triangles becomes a mesh based on which bones in the skeleton the weights for the verts for each triangle references. Your format can't support more than 1 weight per triangle, so when you encounter that, you can either barf and tell the user to fix the model, or you can try to handle it gracefully. By picking the bone index most referenced by the triangle, or just using the bone index from the first vertex of each tri, or whatever.

I would also store out your extraneous data that isn't supported directly (like anim keyframe events/sounds) to a file next to the exported model (when your importer is running and rapi.noesisIsExporting() returns non-0) in a format of your own specification. Then on export to bsc, your script should check if that file exists for the file that was imported, and if it does, parse it and write that data back out in the new bsc.

All of that combined will give you a clean path that allows you to go bsc->fbx->bsc with no data loss. And if you're robust enough, it will let you convert pretty much any model+anim to the format with no user intervention.

Re: [Noesis] Plugin to export to a new format

Posted: Sat Jul 13, 2013 2:11 am
by Alsair
I see, you make it seem like it will be so simple, but it's going to be quite the task.

One thing I've noticed is, when I import the model, all of the meshes in the original .bsc file get merged into one in Noesis. This would mean if I were to export it (regardless of what format) there would always be 1 Mesh (as opposed to the multiple meshes in the original bsc file).

I would not only have to single out each mesh, but to associate it with the original names used to reference with the .bon file.

How would I go about on accomplishing this before continuing onto the next step?

Re: [Noesis] Plugin to export to a new format

Posted: Sat Jul 13, 2013 4:33 am
by MrAdults
You can use rapi.rpgSetName before you commit the triangles to associate the triangles with a unique mesh name. That'll prevent them from being merged into mesh buckets even though they share materials.

Although, your exporter shouldn't actually care if everything is 1 mesh or not. It should create its own mesh buckets based on unique materials and bone indices, that way it handles source models that aren't broken up as well as models that are.

Re: [Noesis] Plugin to export to a new format

Posted: Sat Jul 13, 2013 11:41 pm
by Alsair
MrAdults wrote:You can use rapi.rpgSetName before you commit the triangles to associate the triangles with a unique mesh name. That'll prevent them from being merged into mesh buckets even though they share materials.

Although, your exporter shouldn't actually care if everything is 1 mesh or not. It should create its own mesh buckets based on unique materials and bone indices, that way it handles source models that aren't broken up as well as models that are.
When I export multiple models into lets say an fbx format, I would only get the model I originally selected, not all of the models currently loaded in Noesis.

For example:
1. Body file is selected in Noesis
2. The body, with the other arm and head files are loaded
- The animations and all the meshes for all three objects appear correct in Noesis
3. Export the model (as fbx) and re-open it in Noesis. Exports four files, texture file for each model, and one .fbx file.
4. Noesis loads everything fine, except for the fact it only loaded the body.

Re: [Noesis] Plugin to export to a new format

Posted: Sun Jul 14, 2013 12:16 am
by MrAdults
Yeah, when you export from a scene with multiple models, you have to specify the model index for the destination file using -modelindex. So from here, there are a number of approaches you can take.

1) Export each model using -modelindex #, then merge the FBX files together in your modeling app of choice. If you want to automate the export process, you could write a simple .bat file to run Noesis.exe ?cmode yourfile.bsc model00.fbx -modelindex 0, Noesis.exe ?cmode yourfile.bsc model01.fbx -modelindex 1, etc. sequentially.
2) Rework your script to just load all of the files with meshes, bones, anims, and textures you want into a single RPG context. This would probably actually work best for you, because your skeleton and anims are shared between files. That means you really just want to load them all into the same context, only set the skeleton and bones once, and only call rpgConstructModel once after all of your files are loaded into the context.
3) Use the model merging script: https://code.google.com/p/noesis-plugin ... elmerge.py It will scrape a directory for all models of a selected type, and combine them together. It does so by generating a .noesis file and loading it. If you want to export its merged results, you then use "Export from preview". Or you could modify the script to just export that .noesis file directly to .fbx if you wanted. When you use .noesis files to load multiple models, you can collapse them all into a single model/scene. It handles merging skeletons, animations, materials, and textures for you, so that you don't need to worry about any conflicts.
4) Write your own .noesis file to handle specific files, as the merger script would do for you. You can probably figure out how the .noesis file works by looking at the merger script.

Re: [Noesis] Plugin to export to a new format

Posted: Sun Jul 14, 2013 5:10 am
by Alsair
While trying write export code, I noticed some values stored in the mesh aren't the same as the original file.

Here's what I mean:
Positions have different float values

e.g. Noesis would have it as:
(-2.5455565452575684, 78.94608306884766, -1.5863863229751587)
When it's actually:
(6.6151938, 1.8743709e-005, -2.1816142)

Normals have different values
e.g. Noesis would have it as:
(-0.1995614916086197, 0.6651973724365234, -0.7195051908493042)
When it's actually:
(0.9030363, 3.7814422e-007, -0.42956412)

UV's had correct values so I had no problems with that.

How would I obtain the correct values?

Edit:

It turns out when the skeleton is loaded the values are changed, however when the skeleton isn't loaded the values appear correctly.
I was wondering if there's a way to export it without the skeleton embedded within the fbx file like that. How would I unbind the skeleton already bound?

Edit 2:
I found a workaround but this has some downfalls. I made it so that I would check rapi.noesisIsExporting when the model is loading to check to see whether it's being exported. If it is, I made it so that it would not load the skeleton. The problem with this is that once it's in fbx format, the model will be junk of models in the middle and will be difficult to modify and re-export. Is it possible to make it so that fbx doesn't merge the skeletal data with the actual positions of each mesh?

Re: [Noesis] Plugin to export to a new format

Posted: Mon Jul 15, 2013 12:00 am
by MrAdults
Your vertices were in bone space on import, you put them in model space. So on export, you want to put them back in bone space. That can be accomplished by transforming your verts using inverse bone matrices.

Re: [Noesis] Plugin to export to a new format

Posted: Mon Jul 15, 2013 12:05 am
by Alsair
MrAdults wrote:Your vertices were in bone space on import, you put them in model space. So on export, you want to put them back in bone space. That can be accomplished by transforming your verts using inverse bone matrices.
I just want to remove the bone vertices that where skinned onto my model (basically what it would be like without the skeleton loaded), how would I accomplish this? If transforming the verts using inverse bone matrices does this, how would I accomplish this on export to bsc?

Re: [Noesis] Plugin to export to a new format

Posted: Mon Jul 15, 2013 12:21 am
by MrAdults
Something like...

Code: Select all

invMats = []
for bone in mdl.bones:
	invMats.append(bone.getMatrix().inverse())

for mesh in mdl.meshes:
	for i in range(0, len(mesh.positions)):
		#actually verifying that the mesh has positions/normals/weights here would be a good idea.
		#additionally, you'd have to do a weighted transform for models that actually use more than 1
		#weight per vert. but for yours, it doesn't matter.
		boneIndex = mesh.weights[i].indices[0]
		mesh.positions[i] = invMats[boneIndex].transformPoint(mesh.positions[i])
		mesh.normals[i] = invMats[boneIndex].transformNormal(mesh.normals[i])
I just wrote this up without testing it or even seeing if it's parseable, but you get the idea.

Re: [Noesis] Plugin to export to a new format

Posted: Mon Jul 15, 2013 1:16 am
by Alsair
If I recall correctly, I'm quite sure mdl.bones is empty. I am not home so I can't test this out right now.


Edit:

Nvm, I was wrong. Anyways, I tried your code and it worked perfectly. For someone who doesn't even need to test his code, you sure do know a lot. :P

btw you can't assign the .position / .normals variables since I guess they're read-only. So I just assigned it to a temporary local variable, and used that to write into the file.

Edit2:

Another problem, when I exported the .bsc into .fbx and imported that .fbx in Cinema 4D I modified the points and re-exported the file as .fbx. Using this fbx file, I tried exporting back to .bsc in Noesis and found out the Bone name references are associated with the wrong meshes. I think Cinema4D swapped them or something, but I am unsure as to why.

The .fbx exported from Noesis is perfectly fine and when exported back to .bsc is good too.

Edit3:
Made some changes to the import/export settings. It works perfectly now.

Re: [Noesis] Plugin to export to a new format

Posted: Sun Sep 22, 2013 12:06 am
by Alsair
It turns out all of the models w/ animations uses Right Hand Coordinate System.

Is there a way to use this method of coordinate system in Noesis?

I'd assume there would be some sorta function or setting in Noesis that does this?

Re: [Noesis] Plugin to export to a new format

Posted: Tue Sep 24, 2013 3:16 pm
by MrAdults
You could use rapi.rpgSetOption(noesis.RPGOPT_SWAPHANDEDNESS, 1), and use mat = mat.swapHandedness() on all of your matrices. However, it should be noted that this actually flips the geometry itself. Which means you'd want to switch it back when exporting back to the format too. It might be preferable to leave it as-is, and just render it mirrored in whatever modeling app you're using to modify it before you export it back again.

Edit: Also, for animations, if you're performing the swap in local-space, you only need to do it on root bones. It'll naturally propagate down the transform chain.

Re: [Noesis] Plugin to export to a new format

Posted: Tue Sep 24, 2013 8:38 pm
by Alsair
Thank you for the help.

I did have a small issue though, but it's probably just an issue with the way I used it.

Upon using rapi.rpgSetOption(noesis.RPGOPT_SWAPHANDEDNESS, 1)

Some of the meshes appeared to be mis-aligned in the animations, but all of the meshes appeared to be the correct handedness.

I was able to fix that by removing it, and manually negating the X value of each vertices in the meshes.

mat.swapHandedness() worked perfectly for the animation clips though.


I have another quick question, is there a way to automatically turn off shading/face culling when done loading the model files?

Re: [Noesis] Plugin to export to a new format

Posted: Wed Sep 25, 2013 7:29 am
by MrAdults
RPGOPT_SWAPHANDEDNESS will also automatically swap handedness on bone and anim matrices, but only if they're set in the RPG context before constructing the model. So that might be happening, and maybe one of the matrix sets is getting double-swapped or something, it's hard to say.

To turn off shading and culling, if it's something you want as a default, it's probably best to make it a material property. You can use NoeMaterial.setFlags(noesis.NMATFLAG_TWOSIDED, 1) (the 1 is to also disable lighting, it's a separate parameter for silly internal legacy support reasons)

Re: [Noesis] Plugin to export to a new format

Posted: Wed Sep 25, 2013 8:16 am
by Alsair
Ahh beautiful, thanks so much for the help!