How to Assign Materials in Command Line Mode

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
AtD
Posts: 50
Joined: Mon Mar 18, 2019 7:14 am

How to Assign Materials in Command Line Mode

Post by AtD »

Hello everyone!
I am developing a fully parametrized geometry creation script which shall run in the command line. I am struggling to assign materials to an Arch.Wall object. All the python-console tells me when performing this step in the UI is

Code: Select all

>>> ### Begin command Arch_MaterialTools
>>> mat = Arch.makeMaterial()
>>> FreeCAD.ActiveDocument.getObject("Wall").Material = mat
>>> mat.ViewObject.Document.setEdit(mat.ViewObject, 0)
>>> ### End command Arch_MaterialTools
which does not include the step, where I actually select the desired material in the gui, it just creates a plain material from a template I guess. How can I fully control this step from the command line?

P.S.: Also I am not liking the idea of having to "copy" material properties from the database for each model instead of just using the database entry. It seems I have to create a new material called concrete, which copies the concrete in the database. But this is more of a feature request / my opinion than a real problem.

Thanks in advance!

-----
OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.20.27422 (Git)
Build type: Release
Branch: master
Hash: d938733eaf2c2ce7cb18d1cbb56147185c473530
Python version: 3.8.6+
Qt version: 5.15.2
Coin version: 4.0.1
OCC version: 7.5.3
Locale: German/Germany (de_DE)
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to Assign Materials in Command Line Mode

Post by onekk »

AtD wrote: Tue Jun 21, 2022 7:58 am I am developing a fully parametrized geometry creation script which shall run in the command line. I am struggling to assign materials to an Arch.Wall object. All the python-console tells me when performing this step in the UI is
Command line in what sense, Python console, script of running FC from command line?

AtD wrote: Tue Jun 21, 2022 7:58 am which does not include the step, where I actually select the desired material in the gui, it just creates a plain material from a template I guess. How can I fully control this step from the command line?
The "command echo" in Python console don't include everything you do in the GUI, as some are not commands, but maybe some GUI sequences that derive from commands that don't expose anything to the Python API.
AtD wrote: Tue Jun 21, 2022 7:58 am P.S.: Also I am not liking the idea of having to "copy" material properties from the database for each model instead of just using the database entry. It seems I have to create a new material called concrete, which copies the concrete in the database. But this is more of a feature request / my opinion than a real problem.
If "material properties" are not exposed in the "Console output" as it seems from the console output, only chances you have is that Arch API will expose some commands to assing a material via Python, so better to ask in the Arch forum for "assigning a material with scripting" or similar question.

From the little I know Arch has a decent Python API as it coded also in Python, so maybe some inspections on the source code in /Mod/Arch subdirectory could be a good place to search for.

In Linux a simple 'grep -ns "material" ./Mod/Arch/' will be sufficient, but I don't know if Windows has a similar command.

Hope it helps

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
AtD
Posts: 50
Joined: Mon Mar 18, 2019 7:14 am

Re: How to Assign Materials in Command Line Mode

Post by AtD »

Thanks for your suggestion, I will have a look.
Command line means, I need it to run in freecad's headless mode and control everything from inside the script with no additional user interaction.
AtD
Posts: 50
Joined: Mon Mar 18, 2019 7:14 am

Re: How to Assign Materials in Command Line Mode

Post by AtD »

Thanks again Carlo,
I was able to extract the needed code. Since I am running in the headless mode, I need to avoid the ViewProvider classes. However
https://github.com/FreeCAD/FreeCAD/blob ... aterial.py in function fillMaterialCombo provides the needed code to get the existing materials from the database.

This extracts the available materials

Code: Select all

def get_existing_materials():
    paths = [App.getResourceDir() + os.sep + "Mod" + os.sep + "Material" + os.sep + "StandardMaterial"]
    ap = App.ConfigGet("UserAppData") + os.sep + "Materials"
    if os.path.exists(ap):
        paths.append(ap)
    cards = {}
    for p in paths:
        for f in os.listdir(p):
            b, e = os.path.splitext(f)
            if e.upper() == ".FCMAT":
                cards[b] = p + os.sep + f
    return cards
Arch.makeMaterial creates a _ArchMaterial object. Then we can assign the read-in Material to the _ArchMaterial object with this

Code: Select all

def make_materials():
    """
        Creates material objects to assign to the used components
    """
    cards = get_existing_materials()
    import importFCMat

    # create a concrete material from the database
    concrete = Arch.makeMaterial()
    concrete.Material = importFCMat.read(cards['Concrete-Generic'])
Then the material can be used in components like wall.Material = concrete.
Hopefully this helps others too.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: How to Assign Materials in Command Line Mode

Post by onekk »

AtD wrote: Wed Jun 22, 2022 7:18 am
You are welcome.

It've simply told you were to find informations.

FC has many gems hidden inside source code, and some portions if the code is written in Python so it is always a good exercise to inspect sources when in troubles.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply