Code:
import argparse,os,re
parser = argparse.ArgumentParser(description='LDCAD to Stud.io Converter')
parser.add_argument('file', help='name of input file')
args = parser.parse_args()
if os.path.exists(args.file):
f = open(args.file,"r")
in_data = f.readlines()
else:
print(''.join([args.file," does not exist"]))
#exit()
file_name = args.file.rsplit(".",1)[0]
output_file_name = ''.join([file_name + "_studio.ldr"])
output_buff = ""
output_file = output_file_name
just_started = False
for line in in_data:
if line.startswith("0 FILE "):
if output_file == output_file_name and len(output_buff) > 0:
of = open(output_file,'a')
else:
if os.path.exists(output_file):
print("Output File " + output_file + " exists, overwriting...")
of = open(output_file,'w')
of.writelines(output_buff)
of.close()
output_file = output_file_name
loc_file_name = ''.join([file_name,"_subpart_",line[7:].rsplit(".",1)[0],".dat"])
loc_file_name_split = loc_file_name.split(" ")
loc_file_name = '_'.join(loc_file_name_split)
print(loc_file_name)
output_buff = ''.join(["0 FILE ",loc_file_name,"\n"])
just_started = True
else :
if just_started and line.startswith("0 Name:"):
output_buff += ''.join(["0 ",loc_file_name,"\n"])
just_started = False
if line.strip().endswith(".ldr"):
num_fields = 0
if line.startswith("1"):
num_fields = 14
if line.startswith("0 Name:"):
num_fields = 2
if not num_fields:
print("This line cannot be handled, exiting...")
print(line)
exit()
new_line = ''.join([line.rsplit(".",1)[0],".dat\n"])
line_data = new_line.split(" ")
new_line = ' '.join([' '.join(line_data[:num_fields]),'_'.join([file_name,'subPart','_'.join(line_data[num_fields:])])])
output_buff += new_line
else:
output_buff += line
if line[0].isdigit() and int(line[0]) > 1:
output_file = loc_file_name
print(output_file)
of = open(output_file,'a')
of.writelines(output_buff)
of.close()
I'm new to Lego designing so please bear with me, however this is what I've got so far.
I started with Stud.io and found it pretty nice and the render engine is really great to showcase your work.
Unfortunately it is not the best at manipulating elements, the drag/drop of blocks is useless, the coordinate system sucks in it and parts that are stuck together are not on round values at all, they're all over the place. If you're not looking at the source data some of it wouldn't bother you but I'm a stickler for nice round numbers and engineered precision.
So I found LDCAD and it does everything beautifully and intuitively once you get the hang of it. The keyboard shortcuts are amazing and overall the user interface is definitely for an engineer not an artist. Since I am one it fits me perfectly.
The only shortcoming I was missing was the rendering engine of Stud.io and the problem as everyone in this thread noticed has to do with how it handles flexible parts. Actually the rendering wasn't the issue, as LDView renders the fallback mesh just fine and the spring description hinted at Stud.io being able to handle meshes just in a convoluted way.
So the problem is that Stud.io only allows meshes to be present in part (.dat) files and not in model (.ldr) files. Any mesh that is in a model file gets ignored and fails to render. So the trick is to convert any part/sub-part of a model that contains meshes into .dat files and make them custom part(s).
Since the process outlined was rather labor intensive by hand I wrote a quick and dirty python script to take care of it. This will take the source file and output a new .ldr file with the mesh elements stripped out and put in .dat files that can easily be added to Stud.io as custom parts.
I also fixed the issue with step and subcomponent names not showing in Stud.io for LDRaw files output from LDCAD.
So my workflow now entails
1) Design and work in LDCAD
2) Embed all unofficial parts
3) Run the python script on the <orig>.mpd file
4) copy the produced .dat files into C:\Users\<user>\AppData\Local\Stud.io\CustomParts\parts\
5) Open the <orig>_studio.ldr file in Stud.io
6) Render away.
This produces much nicer renders and is a one step solution after I save the file in LDCAD.
Code might have bugs, let me know...