codeblock...expression won't evaluate variables i.e. --> GetParameterasText

429
4
04-02-2012 07:38 AM
kyleturner
New Contributor III
First, I'm using Arc10, but calling

import arcgisscripting
gp = arcgisscripting.create(9.3)

because I am making a script for other users.

I get the following error when I try to use a codeblock/expression to calculate a field.

ERROR 000539: Error running expression: autoIncrement(gp.GetParameterAsText(2),gp.GetParameterAsText(3)) <type 'exceptions.NameError'>: name 'gp' is not defined
Failed to execute (CalculateField).


It seems that I can't pass in variables to my calc field code.
e.g.
gp.CalculateField_management(FCls30, IDPK, expression, "PYTHON", codeblock)

where

expression = "autoIncrement(gp.GetParameterAsText(2),gp.GetParameterAsText(3))"

If I pass in a string for each argument, then no problems.

Please help.

Thanks
Tags (2)
0 Kudos
4 Replies
markdenil
Occasional Contributor III
It looks like a scope problem.
try setting each parameter to a variable using GetParameterAsText outside the expression string, and use the variables inside the string.
As it stands, you are referenceing a property of the gp inside a tool running in the gp (the gp.GetParameterAsText(n) is not evaluated until it has already been passed as an expression parameter to CalculateField).

to wit:
param2 = gp.GetParameterAsText(2)
param3 = gp.GetParameterAsText(3)
expression = "autoIncrement(param2, param3)"
0 Kudos
kyleturner
New Contributor III
It looks like a scope problem.
try setting each parameter to a variable using GetParameterAsText outside the expression string, and use the variables inside the string.
As it stands, you are referenceing a property of the gp inside a tool running in the gp (the gp.GetParameterAsText(n) is not evaluated until it has already been passed as an expression parameter to CalculateField).

to wit:
param2 = gp.GetParameterAsText(2)
param3 = gp.GetParameterAsText(3)
expression = "autoIncrement(param2, param3)"


Mark,

Thanks for the help. I tried that and it tells me that the variable is not defined(or whatever the verbiage is... I'm at home now and don't have access to my work PC).
Your efforts are greatly appreciated; maybe I'm not executing it properly??? (just flailing about at this point).

Thanks
0 Kudos
DarrenWiens2
MVP Honored Contributor
Sorry if you know this already, but are you sure you have enough parameters for this? In other words, do you have four parameters - the fourth parameter would be gp.GetParameterAsText(3), not the third?
0 Kudos
kyleturner
New Contributor III
No worries. I'm very familiar with counting from zero.

Thanks anyways, never hurts to be sure.
0 Kudos