0

I've got a model that just copies features from one feature class and appends them to another. If a user supplies a string variable in the NewName field, it overwrites the values for the Name field in the feature class. However, I can't get this working in the code block.

def calcName(Name, NewName):
 if len(NewName) == 0: #Meaning the user did not enter a variable here
 return Name
 else:
 return NewName
calcName(!Name!, '%NewName%')

This results in the field always being changed to '%NewName%' within the field. It doesn't matter what the string is, it always returns '%NewName%'. I can type potato in the NewName parameter and it will return '%NewName%'.

Imgur

asked Sep 27, 2018 at 14:02

1 Answer 1

1

I can replicate what you're seeing. I'm not sure I can explain it, I'd expect that if no value was passed in to your NewName parameter, you'd be able to check for Len of 0 or None or something similar to that. I changed your logic to specifically test for '%NewName%' and it seems to do what you want to do. I know it looks weird, but it works. Give that a try?

def calcName(Name, NewName):
 if NewName == '%NewName%': #Meaning the user did not enter a variable here
 return Name
 else:
 return NewName
calcName(!Name!, '%NewName%')
answered Sep 27, 2018 at 14:19
5
  • It certainly does look a little strange but it works! Still can't comprehend why what I had doesn't work properly. Commented Sep 27, 2018 at 14:25
  • Its probably something to do with an optional parameter and being inline variable from MB to Python. It doesnt pass none, but passes the variable name itself when none. (just a guess) Commented Sep 27, 2018 at 14:27
  • This answer explains how to do it the neatly, if you would like to. stackoverflow.com/questions/10567644/… Commented Sep 27, 2018 at 14:36
  • @KHibma looks like this doesn't actually work if you put a value in for NewName. If you leave it blank it's fine but if you type anything in the parameter it still only returns !Name!. Any other ideas? Commented Oct 1, 2018 at 14:17
  • I'm not in a position to test this right now. When I did though, it was producing the expected result for me with a name and when blank. Commented Oct 1, 2018 at 14:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.