[Closed] Can anyone help with my lookup and replace script?
Hi,
I deal with alot of cad files for furnishings and i use a script we had made a few years ago to replace cad blocks with 3d models from a prebuilt library.
However the cad block references change so often that its becoming an issue to work with the replacer.
Ideally what i would like to do is hold the cad block and the filepath in a csv file and have a script that reads the csv, finds the correct filepath for the block is is working on, and then replaces the cad block with that model at the exact same coordinates (all of the pivots should match up in both the cad block and the model library)
I can write a pseudocode version of the script fairly well but i dont know the maxscript syntax enough to be able to start from scratch or do something the most efficient way.
Basically if anyone can point me in the direction of
a) a piece of code that will search a csv for a matching name
and
b) replace all instances of that name with instanced models
id really appreciate any help anyone can give! It would my day to day job a little simpler!
Thanks
Jack
Let’s start with the CSV reader:
Suppose you have a csv with a format similar to next one:
1;1;212011.0983;453231.228;5×7;Pj
1;2;211882.1809;453276.9943;5×6;Pj
1;3;211848.2391;453291.8135;3×3;At
1;4;211815.3704;453303.8378;3×3;At
1;5;211661.2854;453321.9426;2×2;Pj
1;6;211323.1491;453398.0239;3×4;Pj
1;7;211296.6978;453427.5791;7×9;Pj
1;8;211072.8592;453529.9608;7×10;Pj
-- Reading a CSV file into the array 'data'
-- CSV columns must be divided by ";" symbol
-- Based on a DenisT code.
fn trimSpaces ss =
(
for k=1 to ss.count do ss[k] = (dotnetobject "System.String" ss[k]).Trim()
)
fn csvSplitLine line =
(
ss = (dotnetobject "System.String" line).Split ";"
trimSpaces ss
ss
)
fn csvSimpleParser file =
(
lines = (dotnetclass "System.IO.File").ReadAllLines file
data = for k=1 to lines.count collect
(
row = csvSplitLine lines[k]
)
)
file = @"C:\....\yourFile.csv"
data = csvSimpleParser file
/*
format "data = %
" data
format "data.count = %
" data.count
format "data[1] = %
" data[1]
format "data[1].count = %
" data[1].count
/*
You’ll get an array “data” with ‘n’ items where ‘n’ is the number of rows in your CSV file.
The first and last items, for example, will be:
data[1] = #(“1”, “1”, “212011.0983”, “453231.228”, “5×7”, “Pj”)
data[8] = #(“1”, “8”, “211072.8592”, “453529.9608”, “7×10”, “Pj”)
You can them iterate through the ‘data’ array:
for item in data do
(
coordx = item[3] as float - Ref_coord_x
coordy = item[4] as float - Ref_coord_y
if coordx > 210000 do ()
)
And now, replace selected objects (CAD blocks) with the corresponding 3D object in a model library, using “substitute” modifier:
testpath = @"C:\.....\kk_sphere.max" -- The file that contains the Xref object that will replace the CAD block
objectname = "Sphere001" -- The name of the object you want to merge from the file
Xref_Object = xrefs.addNewXRefObject testpath objectname -- Import the Xref Object into your scene
The_modifier = Substitute() -- Create a Substitute modifier
The_modifier.ObjectReference = Xref_Object -- Asign the Xref Object to the modifier
The_modifier.ObjectName = objectname -- Assign a name to display in the modifier pannel (not necessary)
addModifier selection (The_modifier) -- Add the modifier to all selected objects (CAD Blocks) of your scene (as an instance)
mat = Xref_Object.mat -- Get material from Xref Object
-- mat = mat.GetSourceMaterial(true) -- Don't comment this line if you want the material not to be an XrefMaterial
for o in selection do o.mat = mat -- Assign material to selected objects
delete Xref_Object -- Delete the imported Xref Object from the scene (I'm sure there's a way to import an Xref Object whithout creating it, but I don't know how)
Thats awesome thanks ill give it a try when i get to work tomorrow. Ive also realised that max has a build in replace in the import menu and that i could simply xref all of the files in the lobrary into one large ‘palette’ file and simply run replace with that file.
Ill definitely give the code a try though. it seems like it might be slightly easier to work into our current pipeline.
the task itself is simple. but the making a ‘test stand’ takes a time.
so if you want to get a help
upload a test (example) scene with nodes needed to be replaced
give a CSV file that tells what needs to be replaced and with what
give replace objects
upload a file which is how the test file has to look after replace
it will help… first of all you