PDA

View Full Version : help(python)


blackseraphim
02-16-2011, 08:11 AM
hi, i'm a noob in python and i cannot find a solution on my script.


import maya.cmds as mc

factor =1
mc.select(self.nurbsPlaneObj[0]+".u" + "[0.5]" )
mc.select(self.nurbsPlaneObj[0]+".v" + "[" + factor + "]", add=True )

what i'm trying to do is put the value in the v (which is the factor) of the nurbsplane
and i'm having an error of
# TypeError: coercing to Unicode: need string or buffer, list found #


sorry for the noob question

tnx in advance


Peter

NaughtyNathan
02-16-2011, 08:45 AM
in python you can only catenate strings together using +
you have tried to "add" an integer to a string and python doesn't allow this. in python you need to use the % method, which is pretty strange to get your head around to start with but is really very straightforward:

iValue = 1
fValue = 3.14
st = 'adding a string to an int value of: %d and also, PI = %.2f (which is a float!)' % (iValue,fValue)
print st
take a look at the python docs for more details:

http://docs.python.org/library/stdtypes.html#string-formatting

:nathaN

blackseraphim
02-18-2011, 01:03 AM
tnx nathan!
really, it was a big help...
:)

NateH
02-18-2011, 02:36 AM
You can also cast the int/float to a string using str()

blackseraphim
02-18-2011, 08:24 AM
Hi nathan,

I've researched and studied on what you have told and wrote some and it worked,
but in this i case i was wondering why i couldn't get my script right.
here's the script and the error message:

self.factor = 1.0
self.factors = self.segmentMid*(self.factor+(i*2.0))
mc.select(self.nurbsPlaneObj[0]+".u" + "[0.5]" )
mc.select(self.nurbsPlaneObj[0]+".v[%f]", add=True ) %(self.factors)

TypeError: Object prefix_part_SRF.v[%f] is invalid #

basically i was hoping to select the .uv of the nurbsPlane object....

sorry for being a noob XD

tnx in advance


Peter

theflash
02-18-2011, 09:44 AM
check this link:
http://diveintopython.org/native_data_types/formatting_strings.html

And do this "[%f]"%(val)

You have to use % operator WITH string not the whole function.

blackseraphim
02-21-2011, 03:06 AM
@theflash:
thank you!
it works like a charm!


Peter

CGTalk Moderation
02-21-2011, 03:06 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.