Wings 3D Development Forum
Objects exported in reverse order - Printable Version

+- Wings 3D Development Forum (https://www.wings3d.com/forum)
+-- Forum: Wings 3D (https://www.wings3d.com/forum/forumdisplay.php?fid=1)
+--- Forum: Programming (https://www.wings3d.com/forum/forumdisplay.php?fid=7)
+--- Thread: Objects exported in reverse order (/showthread.php?tid=155)



Objects exported in reverse order - oort - 01-25-2013

I have noticed that Yafray and other exporters export objects in reverse order of their Id number. In a scene with two objects, Object Id #2 is exported first and Object Id #1 is exported second. Add a third object to the scene and Object Id #3 is exported first Object Id #2 is exported second and Object Id #1 is exported last.

This only becomes a problem when using YafaRay Meshlights. Apparently they have to be numbered in the same order that they are exported. This gets confusing and every time an object is added everything has to be renumbered... Sad

Does anyone know why Wings3D exports objects in reverse order of their Id number? Is this hard coded or is it a problem with the exporters? Could this be changed?

Thanks,
oort


RE: Objects exported in reverse order - micheus - 01-25-2013

(01-25-2013, 06:33 AM)oort Wrote: Does anyone know why Wings3D exports objects in reverse order of their Id number? Is this hard coded or is it a problem with the exporters? Could this be changed?
oort, did you tried to change the order by changing the lists "enumerator" function: by using foldr (right) instead of foldl (left)?

It's what I can suggest you by seeing the source code I found in your site - at line 3193.


RE: Objects exported in reverse order - oort - 01-25-2013

micheus,
Thanks for taking a look and the tip. I tried the change you suggested but that didn't do anything. That part of the code is for lights and YafaRay Meshlights are actually meshes/objects... I wouldn't expect anyone to know that... Smile

I thought that making that change might change the order in which lights are exported but it didn't. It did make me aware of the fact that not only are objects exported in reverse order but that lights are exported in reverse order as well...

Edit: I just modified wings_export.erl, changing all foldl to foldr. This exports everything in normal order. This broke something in the YafaRay exporter so I need to be more selective in my changes. Hopefully this is the solution but also hopefully this doesn't break something else... Smile Sad

Edit2: I did another test before doing any more code changes and it seems to be working. I will have to do more testing to figure out what I did wrong in my first test... Smile

oort


RE: Objects exported in reverse order - oort - 01-25-2013

I did more testing... I think the reason why YafaRay failed at first was because of the Object numbers I had assigned to the Meshlights.

The YafaRay Meshlight Object #s must be in sync with the number of Objects in the scene. If objects are deleted the numbering can be affected. The "Object #" setting under Material Properties > YafaRay Options > Object Parameters > Object Type > Mesh Light follow the sequence of the Object ID but will not match exactly if objects have been deleted.

A scene where an object has been deleted and a new object created, including 1 Grid (Id 4) and 2 mesh lights, will have two meshlights numbered Object # 1 and Object # 2. This is the case even if the Object Id for the two meshlight objects is Id 2 and Id 3. The meshlight Id 2 will be numbered as Object # 1 and Id 3 will be numbered as Object # 2. If no objects had been deleted the meshlights would have been numbered Object # 2 and Object # 3. I hope that makes some sense...Undecided

The best way to avoid confusion would be to create all your meshlight objects first and then create the rest of your scene objects or create all your meshlight objects last. Probably not practical but that seems to be the best way to avoid having meshlight Object #'s out of sync with the objects Id number.

All this being said, changing "foldl" to "foldr" in wings_export.erl fixed my problem. (Thanks again Micheus) I tested exporting (.3ds, .obj, .x, .wrl, .dae, .lwo, .ndo, .stl, .pov) and saw no ill affects when viewing the exported models.

Can wings_export.erl be modified in the next release?

Thanks,
oort


RE: Objects exported in reverse order - micheus - 01-26-2013

oort, I don't think the wings_export.erl must be changed. It's working as it is - in this case. As each exporter has its particularities I believe that is our responsibility as programmer work around this kind of "problem".

I took a look at the code again (I couldn't change it and test), but - in accord with you've answered me - I believe that the meshlights are being exported in this part of the code:
Code:
section(F, "Objects"),
    foreach(fun (#e3d_object{name=Name,obj=Mesh}) ->
            export_object(F, "w_"++format(Name), Mesh, MatsGb),
            println(F)
        end, Objs),
foreach just call the function passing each item in its order in the list.

If you've changed the result order when changed wings_export file, it seems that you will need only to use an other "integrator" in this code above.
Try to change the code to this one:
Code:
section(F, "Objects"),
    foldr(fun (#e3d_object{name=Name,obj=Mesh}, _) ->
            export_object(F, "w_"++format(Name), Mesh, MatsGb),
            println(F)
        end, [], Objs),

or you can use that one already existent, but reverting the list of objects before use it:
Code:
section(F, "Objects"),
    foreach(fun (#e3d_object{name=Name,obj=Mesh}) ->
            export_object(F, "w_"++format(Name), Mesh, MatsGb),
            println(F)
        end, reverse(Objs)),

and if you still need to change the ID sequence, you can use my first suggestion with some small changes to the code:
Code:
section(F, "Objects"),
    foldr(fun (#e3d_object{name=Name,obj=Mesh}, Id) ->
            export_object(F, "w_"++format(Name), Mesh, MatsGb, Id),
            println(F),
            Id+1
        end, 0, Objs),
and then you need to add the Id parameter to export_object function.


RE: Objects exported in reverse order - oort - 01-26-2013

Micheus,
I was thinking since it would seem more logical for all exporters to export in ascending order instead of descending order it might make sense to make the change but you are right it is probably best not to change something that is working... Smile

Thanks again for taking another look and the suggested changes. I will definitely test to see if that will work. The last one would be the best one, assuming it would fix the problems associated with deleting objects causing the Id numbers to get messed up. Hopefully I could have the meshlights Object # set automatically on export... Smile

I will take a look as soon as possible.

Thanks again,
oort


RE: Objects exported in reverse order - oort - 01-27-2013

Micheus,
Once again you are my hero... Smile

The first recommended code worked and the last one worked even better. It will allow me to get rid of the "Object #" setting from the Meshlight properties dialog.

I had to make one little tweak to your code...

Quote: end, 0, Objs),
had to be changed to...
Quote: end, 1, Objs),

That way objects are numbered 1,2,3,etc instead of 0,1,2,etc.

Thanks again,
oort


RE: Objects exported in reverse order - micheus - 01-27-2013

It's good I could help you. You already did this for me many times.
We are a few here. We should be here to help each other. Count with me. Wink


RE: Objects exported in reverse order - oort - 01-27-2013

Micheus,
I owe you many more helps to pay you back for this... Smile

oort