[Closed] how creat spline from file
Hello
there are read(Read Geometry Data From Text File ) and write(Output Geometry Data To Text File) script in maxscript manual that can save a shape information in a file and in reverse retrieve the info(poses of vertices and face indexes) then make a mesh.
I need only save the shape and draw back the spline not the mesh from files.
anyone can help me?
TY!
I used to have a script like that for Max 3, but it looks like I have lost the source, at least I cannot find it on my site.
The BFF exporter has some code that does the spline IO.
The theory is similar to meshes, but instead of vertex and face lists, you have to go through all splines in the shape and export the vertex positions, corner types and tangents.
I extracted that code from BFF:
fn export_spline_external o external_file_name =
(
local out_file = createFile external_file_name
e_spline = copy o
addmodifier e_spline (edit_spline())
collapsestack e_spline
format "-------------------------------------------------------------------
" to:out_file
format "-- Spline Definition For [%]
" o.name to:out_file
format "-------------------------------------------------------------------
" to:out_file
format "-- SOURCE SCENE NAME: [%]
" maxFileName to:out_file
format "-- EXPORT DATE: [%]
" localtime to:out_file
format "-- SOURCE VERSION: [%]
" (maxversion()) to:out_file
format "-------------------------------------------------------------------
" to:out_file
format "\"%\"
" o.name to:out_file
format "% --Spline Count
" e_spline.NumSplines to:out_file
for s = 1 to e_spline.NumSplines do
(
format "% --Spline Knot Count
" (numKnots e_spline s) to:out_file
format "% --Spline Closed?
" (isClosed e_spline s) to:out_file
)
for s = 1 to e_spline.NumSplines do
(
num_verts = (numKnots e_spline s)
format "--KNOT LIST FOR SPLINE %
" s to:out_file
for v = 1 to num_verts do
(
get_type = getKnotType e_spline s v
get_vert = getKnotPoint e_spline s v
get_in = getInVec e_spline s v
get_out = getOutVec e_spline s v
format "%, %, %, %, %
" v get_type get_vert get_in get_out to:out_file
)--end v loop
)--end c loop
format "-----------------
" to:out_file
format "-- End Of File --
" to:out_file
format "-----------------
" to:out_file
close out_file
delete e_spline
)
fn generate_spline_code in_file =
(
ext_file = openFile in_file
splineName = readValue ext_file
num_splines = readValue ext_file
spline_verts = #()
spline_closed= #()
for s = 1 to num_splines do
(
append spline_verts (readValue ext_file)
append spline_closed (readValue ext_file)
)
new_obj = splineShape()
for s = 1 to num_splines do
(
addNewSpline new_obj
for v = 1 to spline_verts[s] do
(
readValue ext_file
addKnot new_obj s (readValue ext_file) #curve (readValue ext_file) (readValue ext_file) (readValue ext_file)
)
if spline_closed[s] then close new_obj s
)
updateShape new_obj
new_obj.name = uniquename (splineName+"_Import")
close ext_file
)
theFileToExport = "c:\ emp\\mysplinetest.txt" --define a file name
export_spline_external $ theFileToExport --export the currently selected spline
edit theFileToExport --open the file to see the result
generate_spline_code theFileToExport --import the file
Thank you Bobo for reply.
the script file has problem when I run it, the error comes at this line:
splineName = readValue ext_file
and the error is this :
– Runtime error: Read past end of file
I think I dont need so much of codes.
actualy I have many geometrical shapes that I want to draw them onetime and then use them as reference
to build objects. for this purpose I need write splines only once and read them many times. I dont have problem
with the first part cuz I used the sample file for Output spline from maxscript manual, but I got problem with
drawing spline with my data from exported file.also there is Read data files in that manual but it convert the
datas to mesh (vertices and faces) I only need a function to draw spine from vertices positions that stored in
data file. pls see the sample code in maxscript manual and then make the changes in those files.
Thank you very much!
here is the title in maxscript manual for those sample codes:
How to read/write geometry data from/to text file.
Hi
Ijust came to have to use this, so I made a small UI, and code change
to read/write multiple splines to a text file.
Thx Bobo
(
fn export_spline_external splines external_file_name =
(
local out_file = createFile external_file_name
format "-------------------------------------------------------------------
" to:out_file
format "-- SOURCE SCENE NAME: [%]
" maxFileName to:out_file
format "-- EXPORT DATE: [%]
" localtime to:out_file
format "-- SOURCE VERSION: [%]
" (maxversion()) to:out_file
format "-------------------------------------------------------------------
" to:out_file
for o in splines do
(
format "-- Spline Definition For [%]
" o.name to:out_file
format "-------------------------------------------------------------------
" to:out_file
e_spline = copy o
addmodifier e_spline (edit_spline())
collapsestack e_spline
format "\"%\"
" o.name to:out_file
format "% --Spline Count
" e_spline.NumSplines to:out_file
for s = 1 to e_spline.NumSplines do
(
format "% --Spline Knot Count
" (numKnots e_spline s) to:out_file
format "% --Spline Closed?
" (isClosed e_spline s) to:out_file
)
for s = 1 to e_spline.NumSplines do
(
num_verts = (numKnots e_spline s)
format "--KNOT LIST FOR SPLINE %
" s to:out_file
for v = 1 to num_verts do
(
get_type = getKnotType e_spline s v
get_vert = getKnotPoint e_spline s v
get_in = getInVec e_spline s v
get_out = getOutVec e_spline s v
format "%, %, %, %, %
" v get_type get_vert get_in get_out to:out_file
)--end v loop
)--end s loop
delete e_spline
)
format "-----------------
" to:out_file
format "-- End Of File --
" to:out_file
format "-----------------
" to:out_file
close out_file
)
fn generate_spline_code in_file =
(
ext_file = openFile in_file
while not eof ext_file do
(
splineName = readValue ext_file
num_splines = readValue ext_file
spline_verts = #()
spline_closed= #()
for s = 1 to num_splines do
(
append spline_verts (readValue ext_file)
append spline_closed (readValue ext_file)
)
new_obj = splineShape()
for s = 1 to num_splines do
(
addNewSpline new_obj
for v = 1 to spline_verts[s] do
(
readValue ext_file
addKnot new_obj s (readValue ext_file) #curve (readValue ext_file) (readValue ext_file) (readValue ext_file)
)
if spline_closed[s] then close new_obj s
)
updateShape new_obj
new_obj.name = uniquename (splineName+"_Import")
)
close ext_file
)
rollout SplineIO_Rollout "Spline IO"
(
group "Write"
(
editText edtWritefile ""
button Wbrowse "..."
button Write "Write"
)
group "Read"
(
editText edtReadfile ""
button Rbrowse "..."
button Read "Read"
)
on Wbrowse pressed do
(
f=getSaveFileName caption:"Choose file to Save" filename:(GetDir #scripts +"\\myspline.txt") types:"Text(*.txt)|*.txt|All|*.*|"
if f != undefined do edtWritefile.text=f
)
on Rbrowse pressed do
(
f=getOpenFileName caption:"Choose file to Open" filename:(GetDir #scripts +"\\myspline.txt") types:"Text(*.txt)|*.txt|All|*.*|"
if f != undefined do edtReadfile.text=f
)
on Write pressed do
(
sh=for s in selection where superclassof s==Shape collect s
if edtWritefile.text!="" do
(
export_spline_external sh edtWritefile.text
edit edtWritefile.text
)
)
on Read pressed do
(
if edtReadfile.text!="" do generate_spline_code edtReadfile.text
)
)
createdialog SplineIO_Rollout
)