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 Answer 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– AGH_TORN2018年09月27日 14:25:13 +00:00Commented 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 whennone
. (just a guess)KHibma– KHibma2018年09月27日 14:27:21 +00:00Commented Sep 27, 2018 at 14:27 -
This answer explains how to do it the neatly, if you would like to. stackoverflow.com/questions/10567644/…Jelle– Jelle2018年09月27日 14:36:32 +00:00Commented 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?AGH_TORN– AGH_TORN2018年10月01日 14:17:35 +00:00Commented 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.KHibma– KHibma2018年10月01日 14:21:27 +00:00Commented Oct 1, 2018 at 14:21