Lasse D

Converting parts with textures from Studio to LDraw

Recommended Posts

Studio 2.0 (or rather, "Part Designer") is able to export to LDraw. Unfortunately Bricklink has chosen to roll out their own standard for texture mapping, rather than following the official spec.

I do not care for the reason for Bricklink to go this way. It personally took me almost a month to implement it, so I fully understand if they wanted to choose something less complex. Instead I would like to dedicate this thread to converting between the two formats. In order to do this, we have to understand the format that Studio exports to. If you want to jump directly to a live demo, then please click "3D" on the page I have set up here.

When creating a new part in Part Designer, you can choose either "Minifig", "Decorated Part" or "Create Part From Scratch". I will start with "Decorated Parts" and take the two other options later.

Update!

The new location for Studio2LDraw is https://brickhub.org/i/apps/studio2ldraw.htm 

 

Using the checker.png by Nils from the LDraw forum and some basic gradient picture, I have created test files for most of the decorated parts currently available from Part Designer

The Studio "LDraw" File Format - Findings

Exporting to LDraw from the Part Designer will result in .dat files containing something along the lines of:

0 FILE studio_test_1_8_checker.dat
0 Brick 1 x 8 checker
0 Name:  studio_test_1_8_checker.dat
0 Author:  
0 !LICENSE Redistributable under CCAL version 2.0 : see CAreadme.txt
0 BFC CERTIFY CCW
0 PE_TEX_PATH -1
0 PE_TEX_INFO iVBORw0KGgoAAAAN(...)uQmCC
1 16 0.0000 -24.0000 0.0000 1.0000 0.0000 0.0000 0.0000 1.0000 0.0000 0.0000 0.0000 1.0000 s/3008s01.dat
3 16 -80.000 -24.000 -10.000 -80.000 0.000 -10.000 80.000 0.000 -10.000 0.045 0.955 0.045 0.045 0.955 0.045
3 16 -80.000 -24.000 -10.000 80.000 0.000 -10.000 80.000 -24.000 -10.000 0.045 0.955 0.955 0.045 0.955 0.955

Please note that I have inserted the "!LICENSE" line in order to ensure that the files may be shared and used. The commands "PE_TEX_PATH" and "PE_TEX_INFO" are proprietary from Studio. I have not yet found a "PE_TEX_PATH" command that does not say "-1", so let's ignore it for now. The other command has a base64 encoded picture as the argument.

The geometry lines have one sub part (in this case "s/3008s01.dat") and 2 or more triangle lines. I have yet to see any file being generated with quads, lines or any other content after the encoded picture.

The Encoded Picture

The base64-encoded picture is not simply the picture you upload to Part Designer. It will be processed. I have found that the encoded picture is scaled up and has been given a transparent border. I do not know why the border is added - perhaps someone can give me some insight here.

Computing Projections

In the LDraw specification you have to specify how to project a texture onto underlying geometry. Unfortunately the exported LDraw files from Studio do not contain any other information than what I have stated above. You thus have to take each part one-by-one and compute your own LDraw TEXMAP projections.

I have found the following heuristic works for most of the supported parts (see exceptions in the section below):

1) Take the minimal axis-aligned 3D bounding box of all the points of the triangles. Call this box B. Let Bx, By, and Bz be its length along the X, Y and Z-axes, respectively.

2) Expand B by 10% in each direction to account for the border introduced to the picture.

3) If 2*By < Bx and 2*By < Bz, then assume the projection should be performed on an XZ plane. Otherwise, project onto an XY plane.

4) No matter the projection, choose the points for the planar projection defined in the LDraw specification to be corners from B.

Exceptions: 2x4 Brick and 2x2x2 Slope

For the parts "Brick 2 x 4" and "Slope 2 x 2 x 2" you can choose two pictures for decorating the front and back of the part.

For the 2x4 brick, the two pictures get merged into a single picture - the front picture on top and the back picture below. There will be 4 triangles in the LDraw file - the two first for the front side, and the other for the back side.

For the 2x2x2 slope, the two pictures also get merged into a single picture, however, this time they are side-by-side: Front picture on the left and rear picture on the right.

For both exceptions, I have had to introduce "magical numbers" in order to get a proper alignment of projections.

Please see the source code here for how to compute projections in JavaScript for the buildinginstructions.js project.

Non-Standard Alignment of Sub-Part and Geometries

Please take a look at the line for the sub part:

1 16 0.0000 -24.0000 0.0000 1.0000 0.0000 0.0000 0.0000 1.0000 0.0000 0.0000 0.0000 1.0000 s/3008s01.dat

The positioning is off: Normally you would place the sub part at (0, 0, 0), but here it is at (0, 24, 0). This position has to be translated so that the sub part is at (0, 0, 0) in order for the custom part to appear correctly.

You have to subtract the position of the sub part from all the positions of the triangles as well.

 

By following the observations above, I have been able to create the converter from Studio LDraw to standard LDraw as seen in the source code here for. This is what powers the 3D viewer here. As you can see, there are still some precision issues - most noticeably on the 1 x 8 tile where the "10% rule" doesn't seem to work perfectly. Another issue is that I cull textured surfaces on transparent parts, while Studio does not.

 

Please tell me if you have other observations. My plan for now is to continue with Minifigs.

Edited by Lasse D
Changed location of app

Share this post


Link to post
Share on other sites

What is the end goal here? Are you looking to use the Studio Part Designer to generate stickers or decorate parts in LDraw vs using LDPatternCreator? (I get the appeal, LDPC is great at what it does but its is a steep learning curve and I dont want to redraw the decals I have made for printing as a vector only to have to draw them again...as a vector)

Another note, on your SVG2DAT have you solved the bounding box issue?

Share this post


Link to post
Share on other sites
10 hours ago, Lasse D said:

 

I have not yet found a "PE_TEX_PATH" command that does not say "-1", so let's ignore it for now.

Probably -1 just means embedded, 0 would be disabled and 1 point to an externally linked image in the future. The rest is just kinda baffling news. I've never bothered using textured parts in either program and for me as a 3D artist this seems pretty lo-fi. Has the thought of proper UV-mapping even ever crossed anyone's mind?

Mylenium

Share this post


Link to post
Share on other sites
On 1/2/2020 at 5:24 AM, supertruper1988 said:

What is the end goal here? Are you looking to use the Studio Part Designer to generate stickers or decorate parts in LDraw vs using LDPatternCreator? (I get the appeal, LDPC is great at what it does but its is a steep learning curve and I dont want to redraw the decals I have made for printing as a vector only to have to draw them again...as a vector)

Another note, on your SVG2DAT have you solved the bounding box issue?

The end goal is to be able to convert between LDraw files as exported by Part Designer and official LDraw files. There are a lot of members in my LUG who make trains using Studio 2.0 and they are perplexed by their models not working in LDView, and other LDraw-compatible software. These are typically people who do not want to deal with vector graphics, but still want logos on their models.

As for svg2ldraw, I have added non-rectangular bounding boxes by computing Convex hulls using Floyd-Steinberg's algorithm. You can try it out with SVG files, such as:

<svg width="40" height="40" viewBox="0 0 400 400" xmlns="http://www.w3.org/2000/svg">
  <circle cx="200" cy="200" r="200" fill="red"/>
  <circle cx="100" cy="200" r="50" fill="blue"/>
</svg>

and the tool is hosted here: https://brickhub.org/i/apps/studio2ldraw.htm 

 

On 1/2/2020 at 9:31 AM, Mylenium said:

Probably -1 just means embedded, 0 would be disabled and 1 point to an externally linked image in the future. The rest is just kinda baffling news. I've never bothered using textured parts in either program and for me as a 3D artist this seems pretty lo-fi. Has the thought of proper UV-mapping even ever crossed anyone's mind?

Mylenium

They do proper UV mapping in the LDraw spec and support 3 types of mapping (planar, cylindrical and spherical) which seems to satisfy the existing need for textures on LEGO models.

Share this post


Link to post
Share on other sites
7 hours ago, Lasse D said:

They do proper UV mapping in the LDraw spec and support 3 types of mapping (planar, cylindrical and spherical) which seems to satisfy the existing need for textures on LEGO models.

I mean mesh-based intrinsic UVs with Atlas mapping and all that good stuff. Just using projections is so 1990. ;-)

Mylenium

Share this post


Link to post
Share on other sites

The LDraw file format specifies neither normals, UV's (apart from for texture mapping) nor actual meshes, so you will find a lot of functionality missing if you are used to work with Blender and similar tools.

 

Most of my work has been in constructing this efficiently so that you can get short loading times when viewing from the browser.

 

Unfortunately I have stumbled into a rather significant issue regarding mapping for the minifig head. The three types of LDraw projections do not cover the type of projection that Studio 2.0 applies!

q5ULlsr.png

 

 

Share this post


Link to post
Share on other sites
1 hour ago, Lasse D said:

Unfortunately I have stumbled into a rather significant issue regarding mapping for the minifig head. The three types of LDraw projections do not cover the type of projection that Studio 2.0 applies!

Do you have an example where Studio UV mapping is significantly different from cylindrical (or even front/back planar) projections? I couldn't find one (and I think that Part Designer uses plain planar anyway)

On 1/5/2020 at 11:14 AM, Mylenium said:

Just using projections is so 1990. ;-)

Why take a hammer to crush a fly? :laugh:  Anyway patterns are mostly acquired by photo/scan so are (essentially) planar projections...

Edited by Philo

Share this post


Link to post
Share on other sites

Do'h. I can't believe I missed this. The triangles lines have the UV's provided as suffixes!

Take one of the lines from my first post:

3 16 -80.000 -24.000 -10.000 -80.000 0.000 -10.000 80.000 0.000 -10.000 0.045 0.955 0.045 0.045 0.955 0.045

It is encoded as follows:

3 color x1 y1 z1 x2 y2 z2 x3 y3 z3 u1 v1 u2 v2 u3 v3

where the LDraw specification ends at z3.

With this I can try out some heuristics for mapping correctly to minifigs - it is the arms in particular which have currently been giving me some issues.

 

@Philo It just didn't look right when I made the projections. I will try to compare it to the inlined UV coordinates and see how far off I am with these.

Share this post


Link to post
Share on other sites
15 hours ago, Lasse D said:

It just didn't look right when I made the projections. I will try to compare it to the inlined UV coordinates and see how far off I am with these.

Eager to see this!

Share this post


Link to post
Share on other sites

I have set up transformations for U/V values from Studio 2.0 files. It works well on all parts with flat textures (bricks, plates, tiles, etc.). However. On discs and minifigs it fails spectacularly:

v4b0f7G.png

I have set up a converter here if you want to try it out yourself: https://brickhub.org/i/apps/studio2ldraw.htm  

It is hosted on brickhub in order to use the LDraw library lying there, so that you get a preview before exporting.

Share this post


Link to post
Share on other sites

OK. This took a lot longer than expected, but using some google foo for vector algebra and solving equations for placing planes that tangent spheres, the projections are finally computed correctly:

 

8v3wwOK.png

 

You can try for yourself on https://brickhub.org/i/apps/studio2ldraw.htm 

This tool now also extracts the image with the texture, so that you can store it in the textures folder. Alternatively, it can inline the texture as well.

 

Next steps will be to improve performance of the converter, see if cylindrical projection can be used on the head, and make the reverse converter than @Philo mentioned.

Share this post


Link to post
Share on other sites

Can't say better... I was expecting to find many textured parts in Studio library to test but I found few of them (128).

Share this post


Link to post
Share on other sites

Oh wonderful. I will have to play with it! Can i ask if it might be possible to have it generate just the "sticker"? I have many instructions I make that would benefit greatly from showing when to place the stickers

 

14 hours ago, Philo said:

@supertruper1988Yes, that's what @Lasse D converter does. Currently testing, worked fine on a modified Minecraft head...

 

Share this post


Link to post
Share on other sites

Hi @supertruper1988. If you want to create a sticker that is placed after the parts that it sticks to, then you should not use parts with textures, as that will leave big gaps in your parts until the stickers are placed. Instead, I recommend making the sticker as a part based on box5.dat, and a quad with the texture on the quad. This will give you stickers similarly to what you see in this model.

I have not tried it yet, but I think Part Creator can make parts like this. If not, then there are LDraw tools that are up for the task. And if not, then we will have to create some.

Share this post


Link to post
Share on other sites
1 hour ago, Lasse D said:

as that will leave big gaps in your parts until the stickers are placed

Stickers are supposed to be placed over normal, gap-less parts. @supertruper1988, You could get images images from Lasse tool and create stickers with @roland online tool (but that works only for flat stickers)

Share this post


Link to post
Share on other sites

@Philo @Lasse D 

I guess what I am asking is for a tool to make a sticker from art I have made in illustrator. I have tried the SVG2DAT and it interprets the bounding box as a color and I cannot make it go away. There is also LD Pattern Creator but I find it very hard to use after having used image editing software for years and the thought of redrawing art that I have already drawn once at 1:1 size is very frustrating.

I have even tried to just apply the art to parts using part designer but that is also a new level of tedium beyond LDPC.

I guess this was more of a vent than anything useful but if there is some tool that can help me, I would be glad to learn more about it. 

Share this post


Link to post
Share on other sites
2 hours ago, supertruper1988 said:

I guess what I am asking is for a tool to make a sticker from art I have made in illustrator. I have tried the SVG2DAT and it interprets the bounding box as a color and I cannot make it go away.

There is also img4dat that converts a bitmap to LDraw pattern. Works pretty well if the source image is clean (such as illustrator image exported to bitmap). Seems awkward to convert vector to bitmap to kind of vector, but when it works... See https://forums.ldraw.org/thread-23242-post-31099.html#pid31099 .The tool itself is a bit rough on edges, but you can get used to it :wink:

Share this post


Link to post
Share on other sites
4 hours ago, supertruper1988 said:

@Philo @Lasse D 

I guess what I am asking is for a tool to make a sticker from art I have made in illustrator. I have tried the SVG2DAT and it interprets the bounding box as a color and I cannot make it go away. There is also LD Pattern Creator but I find it very hard to use after having used image editing software for years and the thought of redrawing art that I have already drawn once at 1:1 size is very frustrating.

I have even tried to just apply the art to parts using part designer but that is also a new level of tedium beyond LDPC.

I guess this was more of a vent than anything useful but if there is some tool that can help me, I would be glad to learn more about it. 

OK. Then the tool in this thread is unrelated to what you are looking for. What happened when you tried to convert your SVG file to LDraw using SVG 2 LDraw?

Share this post


Link to post
Share on other sites
8 hours ago, Lasse D said:

OK. Then the tool in this thread is unrelated to what you are looking for. What happened when you tried to convert your SVG file to LDraw using SVG 2 LDraw?

I had mistakent called your SVG to LDraw tool SVG2Dat. I have not been able to get your tool to work with SVG files output from illustrator. 

10 hours ago, Philo said:

There is also img4dat that converts a bitmap to LDraw pattern. Works pretty well if the source image is clean (such as illustrator image exported to bitmap). Seems awkward to convert vector to bitmap to kind of vector, but when it works... See https://forums.ldraw.org/thread-23242-post-31099.html#pid31099 .The tool itself is a bit rough on edges, but you can get used to it :wink:

I will look at this. I am not too fussed with having to convert to bmp. I can do so easily. 

Share this post


Link to post
Share on other sites
Just now, supertruper1988 said:

I will look at this. I am not too fussed with having to convert to bmp. I can do so easily. 

Actually the last version works with other file formats (eg. png) but file selection still defaults to bmp.

Share this post


Link to post
Share on other sites
13 hours ago, supertruper1988 said:

I had mistakent called your SVG to LDraw tool SVG2Dat. I have not been able to get your tool to work with SVG files output from illustrator.

Ah. OK. Because there is an svg2dat by tcobbs, hence why I had to call mine something else.

What is the problem that you face when using svg files from Illustrator? Do you have a test file I can use for debugging?

Share this post


Link to post
Share on other sites
7 hours ago, Lasse D said:

Ah. OK. Because there is an svg2dat by tcobbs, hence why I had to call mine something else.

What is the problem that you face when using svg files from Illustrator? Do you have a test file I can use for debugging?

I can send over some information. Is the email on your website good to communicate on?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.