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

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%')
  • It certainly does look a little strange but it works! Still can't comprehend why what I had doesn't work properly. – AGH_TORN Sep 27 '18 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) – KHibma Sep 27 '18 at 14:27
  • This answer explains how to do it the neatly, if you would like to. stackoverflow.com/questions/10567644/… – Jelle Sep 27 '18 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? – AGH_TORN Oct 1 '18 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. – KHibma Oct 1 '18 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.