Want to start off by saying this is not my script. This is supposed to be part of a Dynamo script that numbers the doors according to the room number. The tutorial on Youtube didnt provide the Python script so I tried to recreate it so I could follow along with the tutorial as well as get better at Python script. Getting this error.
Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 61, in
NameError: name ‘from_room’ is not defined
Put this through a Python syntax checker and it says there are no errors yet I still get the error within Dynamo. Can someone help me please.
Would you be able to provide the code you have written? The error implies that you are using a variable which you have not previously defined. For example:
a = 1
b = 2
c = a + d
This would raise the following error:
NameError: name 'd' is not defined
The syntax may be valid but the code will not execute.
Not sure if that pasted as intended
RevitDoorNumber.py (2.0 KB)
I attached the file becasue there was some formatting issues before when I tried to paste. Thanks for helping!
See if this code gives you the intended result:
import clr
clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
elements = UnwrapElement(IN[0])
room_number, room_name, doors, room = [], [], [], []
for e in elements:
phase_id = e.CreatedPhaseId
phase = doc.GetElement(phase_id)
to_room = e.ToRoom[phase]
from_room = e.FromRoom[phase]
if from_room is None and to_room is None:
room_number.append("No to or from room")
room_name.append("No to or from room")
doors.append("No to or from room")
elif to_room is not None:
num = to_room.LookupParameter("Number")
name = to_room.LookupParameter("Name")
if num:
room_number.append(num.AsString())
else:
room_number.append("")
if name:
room_name.append(name.AsString())
else:
room_name.append("")
doors.append(e)
room.append(to_room)
OUT = room_number, room_name, doors, room
In your original script, the variable from_room
was being defined within an if statement. Whenever the if statement evaluated to false, the from_room would not be defined, therefore resulting in your NameError.
1 Like