IFamilyLoadOptions / CPython3 Issue

I’m currently working on a python script within Dynamo that will batch load families . It works as is in IronPython ; however, when I switch to CPython I get a TypeError : interface takes exactly one argument line 58 (the famDoc.LoadFamily(doc,FamilyOption()) line). It appears to be related to the FamilyOption Class, but can’t seem to figure out how to fix it. Any thoughts?

class FamilyOption(IFamilyLoadOptions):
    def OnFamilyFound(self, familyInUse, overwriteParameterValues):
        overwriteParameterValues = True
        return True

    def OnSharedFamilyFound(self, sharedFamily, familyInUse, source, overwriteParameterValues):
        overwriteParameterValues = True
        return True

def ChangeCategory(doc, element, bic):
    # Retrieve the family from the given element
    family = element.Symbol.Family
    TransactionManager.Instance.ForceCloseTransaction()
    famDoc = doc.EditFamily(family)
    ownerFamily = famDoc.OwnerFamily
    
    # Change the category of the owner family to a new category, in this case, Windows
    ownerFamily.FamilyCategory = Category.GetCategory(famDoc, bic)
    
    # Define new subcategories to be added to the family
    newSubCats = ["Glass", "Trim", "Frame/Mullion", "Sill/Head", "Panel"]

    TransactionManager.Instance.EnsureInTransaction(famDoc)

    subCats = ownerFamily.FamilyCategory.SubCategories
    subCatDict = {}
    for s in subCats:
        subCatDict[s.Name] = s

    for w in newSubCats:
        # Add new subcategory if it does not already exist
        if w not in subCatDict:
            try:
                newCat = famDoc.Settings.Categories.NewSubcategory(ownerFamily.FamilyCategory, w)
            except: pass

    TransactionManager.Instance.TransactionTaskDone()
    
    # Start a new transaction in the main document to load the modified family
    TransactionManager.Instance.EnsureInTransaction(doc)
      
    famDoc.LoadFamily(doc, FamilyOption())
    
    TransactionManager.Instance.TransactionTaskDone()

    return ownerFamily

Hello @criddell

Hi,

it’s a current PythonNet issue

see here.

use IronPython 2 or 3 instead (you can build a C# library too)

Ahh , yeah that is where I was headed. Thanks for confirming .

There is a cpython 3 / pythonnet hack that I have seen @jacob.small use. Interface subclassing appears to work with a unique namespace

import uuid

class FamilyOption(IFamilyLoadOptions):
    __namespace__ = uuid.uuid4()

    def OnFamilyFound(self, familyInUse, overwriteParameterValues):
        overwriteParameterValues = True
        return True

    def OnSharedFamilyFound(self, sharedFamily, familyInUse, source, overwriteParameterValues):
        overwriteParameterValues = True
        return True

interesting… I’ll have to try that out. Thanks for the reply!

for info, this hack (under PythonNet 2.x) seem only works correctly with certain Net Framework components

in Revit 2024.2 (or 2025 beta)

hence my previous reply

1 Like