View Full Version : instancing an object based on text file

11 November 2011, 08:30 PM
I want to populate maya with a forest. I have a text file with position.scale.rotation, tree type and a tint color for where i want the trees to go

the tint color isnt so important but i would like to create a forest by pulling this information from a text file and instance a tree with the values based on the text file informantion.

I have a feeling something like this exists but i cant find it...

11 November 2011, 10:24 PM
If you're using OS X or Linux, it's doable by doing something like this:

but otherwise, I don't know how to source text for scripting in Windows and Maya. Maybe it could be done with Python in Maya - I'm not very familiar with it.

11 November 2011, 11:23 PM
I found this

i cant get it to work...looks like it might be to old (2004) and im not sure how to update it

So after some searching i found this
Got me this-

// Reads A Text File And Returns A String Array Of Each Line global proc string[] jgTextFileToStringArray (int $skipFirstLine, string $filePath) {
string $filePath = "E:/Scripts/TreePlaces.txt" ;
// Open File
$fileId = `fopen $filePath "r"` ;

// Define String Array
string $dataArray[] ;

// Get The First Line
string $nextLine = `fgetline $fileId` ;

// Loop Until The String Size Is Zero (No Data On That Line)
while (size($nextLine) > 0) {

// Strip Whitespace From The Beginning And End Of The Line
string $cleanLine = strip($nextLine) ;

// Add To Array
$dataArray[size($dataArray)] = $cleanLine ;

// Get Next Line And Continue
$nextLine = `fgetline $fileId` ;


// Remove First Line
if($skipFirstLine) stringArrayRemoveAtIndex(0,$dataArray) ;

// Return Array
return $dataArray ;


Pretty much reads data and stors into an array (as far as i understand)
Im stumped on the syntax for what i want to do next... I want to be able to create instances of trees (or selected object) and have the new objects inhearte the values ( will be xyz for translate, rotate, scale)

something like - for every item in $dataArray instance the selected object

11 November 2011, 12:43 AM
so after some tweaking and spending far to long on this i have come up with the following-

string $array[]; $array = stringToStringArray($tree,"\n");
string $filePath = "E:/Scripts/TreePlaces.txt" ;
$fileId = `fopen $filePath "r"` ;
$tree =`fread $fileId $tree`;
fclose $fileId;
for($t=0 ; $t<size($array) ; $t++)
print $array[$t]; // for testing purposes


so im pretty sure so far this is what i want. However it is doing stuff im not sure i want.

the idea is to take ( a text file)-

// End test

and instance selected objects from that... or ideally any format of this. (ie, rotate first or scale or name last)
im pretty sure im heading in the right direction and so far what i got will seperate the lines out, as for doing something with that information is where im stumped.

how do i specify "pCube" as the name and "1,0,0,0,0,0,0,1,1,1" as the translate, rotate,scale for every line in the text file

CGTalk Moderation
11 November 2011, 12:43 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.