PDA

View Full Version : placing object based on text file


cwisbg
11-28-2011, 04:53 PM
I have a little code that will read a text file that contains object name and transforms( rotateXYZ,translateXYZ,scaleXYZ). I can print it out but i want to be able to select an object and have that object be renamed and placed based on the text file.

so my question is how do i take the following code, and apply the information it gives to a instanced copy of the selected item. Im stumped as to how i seperate the information so maya sees the first bit as name, the second position...ect. and instance selected


// TreePlacements string $sl[] = `ls -sl`; // selected object to be instanced

string $filePath = "E:/Scripts/TreePlaces.txt" ;
$fileId = `fopen $filePath "r"` ;
string $tree;
$tree =`fread $fileId $tree`;
fclose $fileId;
print( $tree + "\n" );
//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

Azrail
11-28-2011, 06:09 PM
You can use stringToStringArray to separate your source string by the comma sign and then use the 0 element as name, 1-3 as positions etc... Btw this type of operations is much much much much easier and more flexible in python.

cwisbg
11-28-2011, 10:52 PM
You can use stringToStringArray to separate your source string by the comma sign and then use the 0 element as name, 1-3 as positions etc... Btw this type of operations is much much much much easier and more flexible in python.

im always up for learning if you would care to write this out in python form.

Azrail
11-29-2011, 08:14 AM
Something like this:

fname = 'tmp.txt'
# Read the file and get each line as an array item
src = open(fname, 'r').readlines()

# This one strips all trailing whitespace characters from the lines
# It's equivalent to the following loop:
# result = [] # empty list to store cleaned strings
# for line in src:
# result.append( line.strip() )
# src = result
src = [line.strip() for line in src]

# Loop and split the line into items
for line in src:
data = line.split(',')
name = data[0]
pos = [ float(d) for d in data[1:4] ] # Get elements 1,2,3 as pos and convert them from string to float
rot = [ float(d) for d in data[4:7] ] # Get elements 4,5,6 as rotation and convert
scale = [ float(d) for d in data[7:] ] # Get elements from 7 to the end as scale and convert
print name, pos, rot, scale

result is:

pCube1 [0.0, 0.0, 0.0] [0.0, 0.0, 0.0] [1.0, 1.0, 1.0]
pSphere1 [-5.0, 10.0, -5.0] [-90.0, 90.0, -45.0] [1.0, 1.0, 1.0]
pCylinder1 [2.5, 2.3420000000000001, 4.2823000000000002] [0.0, 0.0, 0.0] [1.0, 1.0, 1.0]
pPlane1 [0.0, 0.0, 0.0] [360.0, 180.0, 45.0] [1.0, 1.0, 1.0]


Or you can build a list of dictionaries, where each element will have the name, pos, rot and scale and you can then operate on them in separate loop like this, but it can look a little overcomplicated if you have little experience in python:

fname = 'tmp.txt'
# Read the file and get each line as an array item
src = open(fname, 'r').readlines()
src = [line.strip() for line in src]
result = [] # result list
# Loop and split the line into items
for line in src:
tmp = {} # Temp dictionary to store current item
data = line.split(',')
tmp['name'] = data[0]
tmp['t'] = [ float(d) for d in data[1:4] ] # Get elements 1,2,3 as pos and convert them from string to float
tmp['r'] = [ float(d) for d in data[4:7] ] # Get elements 4,5,6 as rotation and convert
tmp['s'] = [ float(d) for d in data[7:] ] # Get elements from 7 to the end as scale and convert
result.append(tmp)

# Access the dictionary:
for item in result:
print item['name'], item['t'], item['r'], item['s']

Which prints the same result as the previous one...

cwisbg
11-29-2011, 06:51 PM
this is perfect... only thing is it cant seem to find the file... It give error 2 and 22
is there a way to define the path of the file... i thought python is supposed to know where ( maya scripts directory, and in the python root) the file is. anyways i placed the file in all the places i could think of.

fname = 'E:/TreePlaces.txt'

riting it like this gets rid of errors but returns nothing.
one thing is even simple comands like
print 'hello'
only prints in the script editor and not in box next to the comand line... do i need to set something up for maya to know python?

Azrail
11-30-2011, 03:23 PM
I couldn't understand very much what is your problem, but I guess you're trying to run a 'script' file like test.py for example?
If that's the case then python works different from mel.
You have to put you *.py somewhere in maya's PYTHONPATH (use `getenv PYTHONPATH` to see where are those)

in your *.py file you should have one or more procedures like this:

# test.py contents:
def testProc():
print 'You should knock first!'


then from maya you do:

import test
# this should be the name of the file which becomes a module in python terms
test.testProc()
# here we say call procedure testProc from module test

CGTalk Moderation
11-30-2011, 03:23 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.