Jump to content

Recommended Posts

In the Python SOP, I've run into a wall trying to get an integer converted to a string. In the larger picture, I'm trying to subtract 1 from a value which is currently part of a string. There are probably neater ways to write these steps, but I'm fairly new to Python so I'm writing it all out to make things easier to follow.

I start with a string such as: 

string = "f27"

Then remove the 'f' like so:

string = string[1:]

Convert it to an integer:

integer = int(string)

Subtract 1 from the value:

integer -= 1

set new string

string_new = "f" + str(integer)

 

The last line returns an error on the Python SOP: TypeError: str() object is not callable. This is confusing because these same steps work just fine in the Python shell. Is there an alternative to str(), or am I using it wrong?

Share this post


Link to post
Share on other sites

should work, unless you overwrote str with something else higher in the script, like str = "something"

Share this post


Link to post
Share on other sites

I did have 'str' set as a variable elsewhere in the code. But even after I got rid of it, the error persisted.

 

But I have managed to get it working now. I solved it by making a new Python SOP and using the same code - initially I was copy/pasting lines and seeing where it broke. Weird how I now have two python nodes side by side with the same inputs, the old one errors and the new one is fine.

Share this post


Link to post
Share on other sites

Just cut/paste the old node or save your scene and reopen, it probably has your str still overwritten and cached somewhere 

Share this post


Link to post
Share on other sites

Don't know if this is a thing in python but I guess it might be worth mentioning, its probably a good idea to refrain from naming your variables stuff like string, int/integer, float, vector etc because this be messing with the internal classes and structs of your coding language and give you hassle later. Don't know if you just named them that in your example for demonstration purposes or not but figured I'd add my 2 cents.

Share this post


Link to post
Share on other sites

TypeError: 'str' object is not callable usually means you are using your () notation on a string, and Python tries to use that str object as a function. e.g. "hello world"(), or "hello"("world"). Or,  if you have variable str and trying to call str() function. Finally: Do not declare python variable name same with python built-in function name, keyword etc.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×