Blender > .3dp import/export script.

Stop by here for help with RRT3 Editor problems and tips.
User avatar
Gumboots
CEO
Posts: 4813
Joined: Mon Aug 13, 2012 4:32 am
Location: Australia

Blender > .3dp import/export script. Unread post

I figured this was worth a thread of its own, to keep track of progress with importing and exporting from Blender.

--------------------------------------------------------------------------

Update: New and better working script attached to the bottom of this post.

Improvements may follow, but it already works. I'll sort out a bit of a readme for it later.

Short version is that it will export any Blender model to .3dp, but it does require the components to have UV mapping. However, you can test unfinished models by deleting anything which is not UV mapped and exporting the rest. You'll have to trim it down to whatever you want in that file too, such as temporarily deleting everything except one wheel when making a bogie .3dp file.

This is quite fast in practice. You can just select the bit you want (right click), invert selection (Ctrl+I), delete the unwanted stuff (Delete key, of course), then export the remaining object, then Ctrl+Z backwards a few steps to get the entire model back the way it was. Repeat as necessary. :-D

To install the script into Blender, copy it to the C:\Program Files\Blender Foundation\Blender\2.74\scripts folder. Then fire up Blender, go into user preferences > add ons, find the script in the list, and enable it.

Regarding importing: Just as a quick example I grabbed the default H10 2-8-2 model I had been messing around with a while back. The imported model is shown in the screenshot. It's all there, and all correct. (0!!0)
Test_import.jpg
Test_import.jpg (309.53 KiB) Viewed 17679 times

The story so far is that CoDEmanX over at the Blender Artists forum has very kindly roughed out an import/export script for going straight from RT3 .3dp files to Blender, and from Blender out to RT3 .3dp format.

CoDEmanX and I are talking about possible improvements to the script and he seems ok with implementing them, but obviously this will depend on his available time and enthusiasm. He's been great so far and I don't want to hassle him.

For the basics of Blender interface use, see the Noob to Pro tuts. These tuts are very good, and well worth reading.
You might also want to look at the Blender reference manual.
Last edited by Gumboots on Mon Nov 23, 2015 9:02 pm, edited 4 times in total.
User avatar
Gumboots
CEO
Posts: 4813
Joined: Mon Aug 13, 2012 4:32 am
Location: Australia

Re: Blender > .3dp import/export script. Unread post

There's been a bit more progress on this. For a start, the script will now export vertex normals instead of face normals, so that's sorted. It just required changing one word in the code ("loop" is now "vertex"). !*th_up*!

He's also had a go at getting the UV mapping to import, and it looks like that will be ok (haven't seen the code yet). So, we should be able to view the skins on the model in Blender, which will be handy for skinning of default models if anyone wants to do that, or for later skinning of imported custom models. My assumption is that for custom models the Blender file would be made available anyway, so .3dp importing wouldn't be necessary, but on the off chance the .blend file wasn't available it would still be possible to import the custom .3dp and work with that.

I'll try to get some in-game testing organised tonight.
User avatar
Gumboots
CEO
Posts: 4813
Joined: Mon Aug 13, 2012 4:32 am
Location: Australia

Re: Blender > .3dp import/export script. Unread post

Ok, the script works. I've just tested it live with functioning locomotive export. See the Schools Class V thread for details. !*th_up*!

A zipped copy of the current script is attached to the OP of this thread. Improvements may follow, but it already works. I'll sort out a bit of a readme for it later.

Short version is that it will export any Blender model to .3dp, but it does require the components to have UV mapping. However, you can test unfinished models by deleting anything which is not UV mapped and exporting the rest. You'll have to trim it down to whatever you want in that file too, such as temporarily deleting everything except one wheel when making a bogie .3dp file.

This is quite fast in practice. You can just select the bit you want, invert selection, delete the unwanted stuff, then export the remaining object, then Ctrl+Z backwards a few steps to get the entire model back the way it was. Repeat as necessary. :-D

To install the script into Blender, copy it to the C:\Program Files\Blender Foundation\Blender\2.74\scripts folder. Then fire up Blender, go into user preferences > add ons, find the script in the list, and enable it.
Last edited by Gumboots on Wed Jul 01, 2015 4:15 am, edited 1 time in total.
User avatar
Hawk
The Big Dawg
Posts: 6503
Joined: Fri Nov 10, 2006 10:28 am
Location: North Georgia - USA

Re: Blender > .3dp import/export script. Unread post

The Blender site says 2.74 isn't supported for XP, and if I do install it I most likely will have to install yet another version of Visual C++. I already have 6 of them installed. This is getting worse than NetFrameWork.
Hawk
User avatar
Gumboots
CEO
Posts: 4813
Joined: Mon Aug 13, 2012 4:32 am
Location: Australia

Re: Blender > .3dp import/export script. Unread post

Ok, shouldn't matter that much. I suspect the script will run ok with Blender 2.6X. What's the latest Blender that will run on XP? Shouldn't be hard to find out.

The other thing is that just because the main page says XP is "not in (active) support", that doesn't necessarily mean that 2.74 won't run on XP. It may just mean that since Windoze is not supporting XP any more, Blender won't officially support XP either. From a support crew perspective this makes sense, but it may still run ok. Could be worth a try.

-----------------------------------

Just did a quick search for "Blender on XP". Found a few things that indicate it may work ok, apart from some of the more computationally expensive stuff (rendering, etc).

http://www.oldapps.com/blender.php?system=Windows_XP
https://developer.blender.org/T43177
http://lists.blender.org/pipermail/bf-c ... 42373.html
http://lists.blender.org/pipermail/bf-c ... 42375.html
User avatar
Hawk
The Big Dawg
Posts: 6503
Joined: Fri Nov 10, 2006 10:28 am
Location: North Georgia - USA

Re: Blender > .3dp import/export script. Unread post

I'll check those links out. I have 6 other versions of Blender. I just figured since that plugin was written for 2.74 it might not be backwards compatible.

That second link, that discusses the issues with opengl32.dll for XP makes me want to steer away from any 2.7.. versions. I may try that plugin on a 2.6.. version to see if tit works, but not right away. I'm kind of wrapped up in a new game I just got. Not a new release, but new to me. :mrgreen:
Hawk
User avatar
Gumboots
CEO
Posts: 4813
Joined: Mon Aug 13, 2012 4:32 am
Location: Australia

Re: Blender > .3dp import/export script. Unread post

Y'know if you get in a bind with the script not working on pre-2.7x, you could just model stuff in whatever Blender you can run on XP, then get some other mug to open the .blend in their 2.74 and export it to .3dp. That should be pretty painless for everyone.

Anyway, no rush. Enjoy your new game. !*th_up*!
User avatar
Hawk
The Big Dawg
Posts: 6503
Joined: Fri Nov 10, 2006 10:28 am
Location: North Georgia - USA

Re: Blender > .3dp import/export script. Unread post

Yea, that would work.
Hawk
User avatar
Gumboots
CEO
Posts: 4813
Joined: Mon Aug 13, 2012 4:32 am
Location: Australia

Re: Blender > .3dp import/export script. Unread post

Ha. Found a bug. I put a lighter skin on the test loco so I could see what was going on, and it turns out the model is being flat-shaded instead of Gouraud shaded. This is because it is being exported with face normals assigned to vertices, instead of the correct vertex normal (which splits the difference between faces). So with a dark skin it doesn't show up, but with a lighter skin the flat shading becomes more obvious.

It's still ok for basic export testing and won't stop me making progress on the Schools class V, but it'll need to be sorted for vertex normals before it can be called a (really) working script. At least for locos anyway. For buildings it would probably even be better the way it is, because most faces on those really should be flat shaded. We may end up with two scripts: one that exports flat shaded for square stuff like buildings, and one that does smooth shaded for locos, etc.

I'm confident it can be debugged for locos. !*th_up*!
User avatar
RulerofRails
CEO
Posts: 2061
Joined: Sun Dec 08, 2013 1:26 am

Re: Blender > .3dp import/export script. Unread post

I am glad that you finally have a script working for the models! I am not really a modder, preferring to play. I haven't even made a decent-quality map yet. :oops: Exploiting the dynamics of the game's engine in-game are more interesting to me, but if I get a bunch of free time. . . . Still, it's a great resource to have, thanks for pursuing this, and to the kind sir who made the code for you!
User avatar
Gumboots
CEO
Posts: 4813
Joined: Mon Aug 13, 2012 4:32 am
Location: Australia

Re: Blender > .3dp import/export script. Unread post

Ok cool, the latest version of the script (now attached to the OP of this thread) exports with vertex normals for smooth shading of curved surfaces. So that's good. !*th_up*!

If there are any situations where somebody specifically wants to export with flat shading, which may be preferable for some buildings that are all flat surfaces, then I can zip up the earlier version of the script that exported with face normals. There's no reason why both could not be installed and just given different names (Blender_3dp_flat_shading.py for instance).

The script still won't import UV mapping. It will only import .3dp body meshes. Having it able to also import UV mapping would be very handy for correcting faults in older existing models, but that's going to depend on the good will of the Blender and Python gods for now.

IF anyone wanted to, it would still be possible to import with the current script and fairly quickly reinstate the UV mapping in Blender by using the UV overlay guides that can be exported from 3dConverter. It'd be a bit of a nuisance for complex locomotives, etc, but it's quite doable. For simple buildings or cargo cars it would be pretty easy. The thing could then be customised and exported straight back to .3dp.
User avatar
Hawk
The Big Dawg
Posts: 6503
Joined: Fri Nov 10, 2006 10:28 am
Location: North Georgia - USA

Re: Blender > .3dp import/export script. Unread post

I have the Trial_3dp_export.zip. Is that the same as the final script for flat shading?
Hawk
User avatar
Gumboots
CEO
Posts: 4813
Joined: Mon Aug 13, 2012 4:32 am
Location: Australia

Re: Blender > .3dp import/export script. Unread post

Yeah I think so. Can't honestly remember what code was in that zip. The bit that was changed to get vertex normals was this:

Code: Select all

# replace the following line:
                    file.write(struct.pack("<3f", *f.loops[i].calc_normal()))

# by this code:
                    file.write(struct.pack("<3f", *f.verts[i].normal))
So if the one in the zip says loops.calc_normal() it will flat shade. If it says vets.normal it will smooth shade.
User avatar
Hawk
The Big Dawg
Posts: 6503
Joined: Fri Nov 10, 2006 10:28 am
Location: North Georgia - USA

Re: Blender > .3dp import/export script. Unread post

OK! Thanks.

I checked and the only difference between the Trial py script and the newly posted Blender py script is at line 40.

Trial:

Code: Select all

"00 00 3E C0  ED 1E 5F C1  09 0A 4D 40  49 4E 53 54"
New Blender:

Code: Select all

"00 00 00 00  00 00 00 00  00 00 00 00  49 4E 53 54"
So I can delete the Trial zip and just make the change you noted above to have both correct versions, right?

Edit 1: OK! Now I have two versions. Flat and smooth. The only difference in the two is at line 74.

flat

Code: Select all

file.write(struct.pack("<3f", *f.loops[i].calc_normal()))
smooth

Code: Select all

file.write(struct.pack("<3f", *f.verts[i].normal))
Both have line 40 as

Code: Select all

"00 00 00 00  00 00 00 00  00 00 00 00  49 4E 53 54"
Hawk
User avatar
Gumboots
CEO
Posts: 4813
Joined: Mon Aug 13, 2012 4:32 am
Location: Australia

Re: Blender > .3dp import/export script. Unread post

Yup. That sounds right. The line 40 thing was him just throwing in the body attachment point from an example .3dp screenshot that I'd posted over there. It should be all zeroes for loco bodies (I think my example screenshot was from a truck).

I actually made the mistake of exporting a loco body with that code and loading it into the game. It had an interesting effect, in that the loco body was hugging the ground, and set back and to one side. Looked quite radical. :mrgreen:

Line 78 is what determines flat vs smooth shading.

We should get some more improvements to the script sometime this month. CoDEmanX has come down with a case of tendonitis, which is putting a stop to any coding at the moment. Once he has his hands back, he says he'll throw in a few of the tweaks that were being discussed (UV importing, etc).
User avatar
Tomix
Brakeman
Posts: 105
Joined: Sun Sep 29, 2013 9:58 pm

Re: Blender > .3dp import/export script. Unread post

This is amazing Gumboots! I'll have to give this a try!
User avatar
Gumboots
CEO
Posts: 4813
Joined: Mon Aug 13, 2012 4:32 am
Location: Australia

Re: Blender > .3dp import/export script. Unread post

Has anyone had a chance to play around with this stuff? CoDEmanX has just threatened to make improvements to the current script, so feedback would be good.
User avatar
Gumboots
CEO
Posts: 4813
Joined: Mon Aug 13, 2012 4:32 am
Location: Australia

Re: Blender > .3dp import/export script. Unread post

Ok, we have improvements already. No need for 3dconverter at all any more, and no need to screw around going from .3dp, to .obj, and then into Blender.

The import script will now import the UV mapping as well as the mesh. This means skinning is now a lot easier. You can just import any RT3 model straight into Blender, and see the skin on it, and see how it all maps to the skin. I've already done the Class 01 and the Kriegslok, just to test the new script. Piece of cake. !*th_up*!

Obviously this means that minor changes to existing models are now easier too. You can change the mesh you want to change and leave the rest alone, sort the skin out so it all suits, then export out to .3dp.
User avatar
Gumboots
CEO
Posts: 4813
Joined: Mon Aug 13, 2012 4:32 am
Location: Australia

Re: Blender > .3dp import/export script. Unread post

If anyone wants the latest version of this script I've attached a zip to this post. The new script will import UV mapping now, as well as exporting it. This is very useful for quick customisation of existing models.

The zip also includes a basic Blender file (named !Base_file_for_importing.blend) that is already set up with lamps for most layers, so you can import different .3dp's to different layers and immediately be able to see what you're doing. This can be handy for model levels of detail, and for importing a locomotive and its tender to the same file.

The .blend also has a basic material and texture locked in, using settings that will automatically work well for RT3 skinning.

To play around with an existing model, just import all the relevant .3dp files one at a time (batch import is not possible yet). All the imported objects will not have a material and texture assigned yet, so just after import all components will be a basic grey in colour.

There is no need to create any new materials if you are only importing one model. To get the imported model looking like it does in the game, start by setting the loco or building body to the default material (it's called Base Material in the .blend, just to give it an obvious name). Now use the A key twice to select all objects, then use Shift+Right Click to make the loco body the active object. Next, use Ctrl+L to open a pop-up menu, and left click Materials in the menu options. All objects will now be set to the Base Material.

This base material will still be grey until you call in the model's A skin. Once all objects are set to the same material, go to the texture settings and scroll down to the image section. Left click the X button next to where it says Works_grey_base.png (clicking this unlinks the default packed file and unlocks the file browser).

Now open the file browser (Open button, of course) to find and select the model's .dds or .tga A skin. The model should now be skinned correctly. (0!!0)

To see how each component of the model maps to the skin you need to right click on a component to select it, change the view from Object mode to Edit mode, then hit the A key to select all vertices. The mapping for that component will now be visible in the UV editor window, beneath the main 3D window.

If you are only wanting to re-skin an existing model this is all you have to worry about. You don't have to concern yourself with the export side of things. You can just edit your skin image in Photoshop or whatever, and use the update button to update the view in Blender. This will give you a much faster skinning time than having to check it all in the game every time you make a small change.

If you want to customise the mesh for a model there are a few more tricks you need to know.

The script will still only export components that have UV mapping. Any components that do not have UV mapping will cause an error if you try to export them. Before exporting, make sure any components that lack UV mapping are not selected.

This new version of the script will only export selected objects. If you have one object that is your locomotive body and you want to export that, just right click it to select it before running the export script. The created .3dp will only contain the locomotive body.

If you have the loco body split down into several components (boiler, cab, whatever) but you want them all to be included in the finished .3dp, hold down Shift and right click every component you want to include, then run the export script.

After exporting you may need to check the attachment points in the resulting .3dp's. By default they are set to zero on all three axes. This is fine for loco and tender bodies, and for buildings, but will require manual hex editing for some other components (connecting rods, etc). That's not difficult to do. If anyone needs tips on how to do it, just ask. !*th_up*!
Attachments
Blender_3dp_starter_kit_v2.zip
(75.1 KiB) Downloaded 310 times
Last edited by Gumboots on Fri Nov 27, 2015 4:25 pm, edited 7 times in total.
User avatar
Gumboots
CEO
Posts: 4813
Joined: Mon Aug 13, 2012 4:32 am
Location: Australia

Re: Blender > .3dp import/export script. Unread post

Just updated the post above with new files and more information. I've also linked that post from the OP of this thread.

I did notice that the original version of the script, that was in the OP and was not as user-friendly as the current version, was downloaded 15 times. I've removed that now. If anyone wants to try this stuff, grabbing the latest script and preset .blend from the zip just above is a much better option. !*th_up*!
Post Reply