Set Family Parameter Formula Issue

I’m working on copying formulas from one family to another and I’m having issues with the doc.FamilyManager.SetFormula() function.

I am using code by @Daniel_Woodcock1 that I found on the forum, and it somewhat works. The problem is that when I first run it, every parameter returns a “There is no valid family type.” error.

The weird thing is that if I were to create a new parameter and then rerun the script, almost all of the formulas will be created. There are still a few “Formula setting failed” errors (which I don’t understand either, any ideas would be appreciated), but overall, most of the formulas are successful.

Is there something I missing regarding making the parameters editable?

import clr

import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc =  DocumentManager.Instance.CurrentDBDocument
app = DocumentManager.Instance.CurrentUIApplication.Application

import Revit

from Autodesk.Revit.DB import *

def tolist(obj1):
	if hasattr(obj1,"__iter__"): return obj1
	else: return [obj1]

pNames = tolist(IN[0])
fString = tolist(IN[1])

outList = []
errors = []
test = []
names = []

if doc.IsFamilyDocument:
	fMan = doc.FamilyManager	
	params = fMan.GetParameters()
	for pName,fStr in zip(pNames,fString):
		for p in params:
			pd = p.Definition
			pn = pd.Name
			if pName == pn:
				if p.CanAssignFormula:
					if fMan.IsParameterLocked(p):
					except Exception, e:
	no =  set(names).difference(set(test))
OUT = outList, no
#For more information refer to RBG Wiki or email me (ctrl + click link):

Try looking in the orchard package as there are some good nodes for this there.

Hello everyone,
I am having the exact same issue with the following error message:

“Exception: Formula setting failed”
Since I am writing the code in dynamo python, it would be good to solve the issue within the code itself.

if doc.IsFamilyDocument:
if True:

Do you mean the Orchid package :wink:
@PAnand you could take a look at the package and if there is a node that accomplish your goal you could jump into the github and have a look at how it is done :slight_smile: I know that @erfajo for the most point have both C# and Python versions of most of the nodes in his package.

Thanks @Jonathan.Olesen, I could only find a C# version of the code.
@erfajo: Would you be able to provide a python version of the node “FamilyDocument.SetFormulaByName” as well please? :slight_smile:

I wonder why do you need a python version if the node that is available can solve the problem!?
The code is surely available somewhere in this forum, I have seen so many tweaks on it, so surely it is here somewhere.
I just don’t understand why fix it if it ain’t broken… and my node works as far as I know :slight_smile:

Be sure that there exists a default family, not just the greyed out field in the family… that is not a type. It is really a problem that Revit accept this since the code doesn’t. However, it looks like Python in some cases still accept no default family… not that I understand why Python code can do things that are not allowed!?

Can you use my nodes instead? Take a look at the Orchid package

This post might inspire you.

Thanks @erfajo
I am actually writing it in Python and want to save the code as a module, which I will import into several scripts.
Will do some research on the forum.

That did the trick for the first issue of “There is no valid family type.” Thanks!

I’m still running into the “Formula Setting Failed” error for 2 parameters, and I can’t figure out why. Have you run across this before when creating your node?

I tried to use your node, but I was getting a “This parameter cannot be assigned a formula” error, which is true, because not all the parameters that are copied from a different family are able to be assigned a formula such as a material parameter. Since there is an error, the entire node fails instead of just excepting the error. I need to functionality to be able to skip the parameters that error out. The current python node I’m working on spits out which parameter errored out on the set formula part and which parameters couldn’t be created at all.


your are pointing on an issue on my todo list… I know this problem, but there is also the issue of creating a converter between Imperial and SI units, which is placed higher on my todo list.
It will come… some day :slight_smile:

I totally get it. Definitely need to set priorities on these things. Definitely appreciate your nodes and the work you’ve put in so far, so thank you!

As for the “Formula Setting Failed”, have you come across that or know what may cause that error at all, or is it just a peculiar thing that pops up?

In the .net implementation, a family type must exist. So it should not be possible at all to do it without also in Python code. I have not used any time I investigating why things work that shouldn’t do it. I have just accepted how it is meant to work.

But even after I create a family type as part of the script, 2 of the 58 parameters get this “Formula Setting Failed”. It’s not the same “There is no valid family type” and I can’t seem to figure out what is causing the “Formula Setting Failed” error.

Oh well, maybe it’s just an issue that’s not really solvable for now. 2 out of 58 isn’t the worst thing in the world to fail on.

try to share you dyn file and a test family/project, then I try to see what happens

See attached. The Excel file has all the parameters and formulas.

The process has been to create a new generic model family, and run the dynamo script in the family environment. It should read the excel file and create the parameters with the proper formula. Let me know what you think.



I think I just figured it out! I’m not importing parameter values, so all of them are set to 0 by default. The because of this, the formulas that break are the ones that are dividing by a parameter that is set to 0.

Such a dumb error. Sorry about that!

CopyFamilyParameters.dyn (8.4 KB)

FamilyParameters.xlsx (10.6 KB)