PDA

View Full Version : instancing an object based on text file


cwisbg
11-22-2011, 07: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...

cgbeige
11-22-2011, 09:24 PM
If you're using OS X or Linux, it's doable by doing something like this:

http://polygonspixelsandpaint.tumblr.com/post/12559776696

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.

cwisbg
11-22-2011, 10:23 PM
I found this
http://www.creativecrash.com/maya/downloads/scripts-plugins/c/readfromfile?os=windows

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
http://www.scriptswell.net/2010/09/mel-tutorial-how-to-read-text-file.html
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

cwisbg
11-28-2011, 11:43 PM
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)-



//objectName,posX,posY,posZ,rotX,rotY,rotZ,scaleX,scaleY,scaleZ
pCube1,0,0,0,0,0,0,1,1,1
pSphere1,-5,10,-5,-90,90,-45,1,1,1
pCylinder1,2.5,2.342,4.2823,0,0,0,1,1,1
pPlane1,0,0,0,360,180,45,1,1,1
// 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-28-2011, 11:43 PM
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.