[Closed] Read names from text file and replace instances in scene?
Hi, Im trying to figure out how to read a list of names from a text file and their model path.
Eg if in my scene i had splines or cad blocks named A12-1234 and in my model library i had a file at C:\Models\A12-1234.max and they were in a text file like this
~MODELDESCRIPTION~A12-1234~C:\Models\A12-1234.max
Is there a simple way i could read in that string, split out both the name and the path and then replace all instances of that object name with the model at the path location aligning them at the same location and object minimum to object minimum?
Im not sure if im explaining this well enough that you guys will know what im talking about but ill give it a shot!
Thanks in advance!
Jack
are you an artist or a tech artist?
if you are an artist without any technical skill and any technical support i can help an orphan
but if you want to be a tech artist please show us at least an attempt of solving the task
Sorry, Im an artist first and foremost and ive been attempting maxscripting for a few months now. But at the moment at my day job i work with large supermarkets laying out their floorplans and fixtures in 3D. We have a script that kind of does what we need it to at the moment but it was written a long time ago and its encrypted and locked down so we cant add new fixtures to the script ourselves even though it was written specifically for us.
I have been reading up on the filestream stuff in maxscript and ive got a line for replacing the splines with an object instance;
for o in objects where o.name == "OBJECTNAME" do (local a = instance $MODELNAME; a.transform = o.transform; delete o)
For the replacing of the blocks themselves but at the moment it doesnt carry across the model materials it takes on the wire colour of the spline its replacing. I’ve never attempted to load external files via maxscript before but from my experience with vb.net i kind of understand the structure.
I Know maxscript integrates with vb.net. Is it possible to do this in .net and then just send a line of maxscript from the .net window to do the replacing? Similar to the one above but instead of using a local instance, merge in a model from a specified math?
Id quite like to do more in .net because its something im more familiar with.
Thanks
Jack
Im making a little headway in how to read and split the file
if the line being read is ~MODELDESCRIPTION~A12-1234~C:\Models\A12-1234.max
f = openfile #"TEXT FILE PATH"
if f != undefined do
(
NO FILE IMPORTED/NOT VALID FILE
while not eof f do
(
line = readline f
and
output = ""
for word in (filterString f "~") do
(
output += (word)
)
format "%
" output
fn replaceModels str =
(
data = filterstring str "~"
name = data[2]
file = data[3]
models = getnodebyname name all:on
if models.count > 0 and doesfileexist file do
(
names = getMAXFileObjectNames file quiet:on
if finditem names name > 0 do
(
count = objects.count
mergeMAXFile file #(name) #noRedraw #mergeDups #useMergedMtlDups #neverReparent
if count < objects.count do
(
target = objects[objects.count]
instancereplace models target
delete target
)
)
)
models
)
/* how to use ***
replaceModels "~MODELDESCRIPTION~A12-1234~C:\Models\A12-1234.max"
**********************/
how to read lines from file you probably know yourself
Thanks so much, thats awesome!
Im having trouble getting the materials to stay the same though. Its still turning the colour of the wirecolour of the spline. Do you know if theres anything i can do about this?
Jack
i would try to replace baseobjects instead of objects:
for model in models do replaceinstances model.baseobject target.baseobject
That throws an error ‘Controller superclasses not the same’
It brings in the materials perfectly, it just doesnt align the model with the cad block.
Any ideas?
Thanks by the way i really appreciate your help.
for model in models do
(
mat = model.mat
replaceinstances model target
model.mat = mat
)
preserves old materials
Its throwing the same error on the line
replaceinstances model target
Here is what i have at the moment
fn replaceModels str =
(
data = filterstring str "~"
name = data[2]
file = data[3]
models = getnodebyname name all:on
if models.count > 0 and doesfileexist file do
(
names = getMAXFileObjectNames file quiet:on
if finditem names name > 0 do
(
count = objects.count
mergeMAXFile file #(name) #noRedraw #mergeDups #useMergedMtlDups #neverReparent
if count < objects.count do
(
target = objects[objects.count]
for model in models do
(
mat = model.mat
replaceinstances model target
model.mat = mat
)
delete target
)
)
)
models
)
replaceModels "~FISH TABLE~R29-0012_Mobile_Fish_Counter~X:\MODEL_LIBRARY\Departments\Counters\R29-0012_Mobile_Fish_Counter.max"