User Tools

Site Tools


blender

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
blender [2022/02/07 13:29] – external edit 127.0.0.1blender [2023/05/17 09:27] (current) ssm2017
Line 550: Line 550:
 </sxh> </sxh>
 the object needs to dont have any “transformation” the object needs to dont have any “transformation”
 +
 +==== inventory ====
 +<sxh python>
 +import bpy
 +import csv
 +
 +def displayLine(title, qty, price):
 +    print(title+ " : "+ str(qty)+ "("+ str(price* qty)+ ")")
 +
 +def cherche():
 +    # get selection
 +    selected_objects = bpy.context.selected_objects
 +
 +    inventory = {
 +        "eclisse-de-liaison-40-52" : {
 +            "price":4.75,
 +            "qty":0
 +        },
 +        "corniere-murale-2000mm": {
 +            "price":33.82,
 +            "qty":0
 +        },
 +        # solives
 +        "solive-omega 65-1000mm": {
 +            "price":17,
 +            "qty":0
 +        },
 +        "solive-omega 65-2000mm": {
 +            "price":34,
 +            "qty":0
 +        },
 +        "solive-omega 65-2500mm": {
 +            "price":42.44,
 +            "qty":0
 +        },
 +        "solive-omega 65-3000mm": {
 +            "price":51.03,
 +            "qty":0
 +        },
 +        # lambourdes
 +        "lambourde-omega 35-100mm": {
 +            "price":1.018,
 +            "qty":0
 +        },
 +        "lambourde-omega 35-150mm": {
 +            "price":1.52,
 +            "qty":0
 +        },
 +        "lambourde-omega 35-2000mm": {
 +            "price":20.37,
 +            "qty":0
 +        },
 +        "lambourde-omega 35-3000mm": {
 +            "price":30.52,
 +            "qty":0
 +        },
 +        "longueur-en-metres-lambourde-a-decouper": {
 +            "price":10.18,
 +            "qty":0
 +        },
 +        # profil de descente
 +        "profil-de-descente-55-20-100mm": {
 +            "price":0.57,
 +            "qty":0
 +        },
 +        "longueur-en-metres-profil-de-descente": {
 +            "price":5.78,
 +            "qty":0
 +        },
 +        # plots
 +        "plot": {
 +            "price":2.4,
 +            "qty":0
 +        },
 +        # lames (7 par m² donc a 90€ le m² ça fait 12.85 la lame)
 +        "lame-terrasse-1000mm": {
 +            "price":12.85,
 +            "qty":0
 +        },
 +        # scotch
 +        "bande-adhesive-epdm-100mm": {
 +            "price":0.12,
 +            "qty":0
 +        },
 +        "bande-adhesive-epdm-150mm": {
 +            "price":0.18,
 +            "qty":0
 +        },
 +        "bande-adhesive-epdm-1000mm": {
 +            "price":1.21,
 +            "qty":0
 +        },
 +        "bande-adhesive-epdm-2000mm": {
 +            "price":2.42,
 +            "qty":0
 +        },
 +        "longueur-en-metres-bande-adhesive": {
 +            "price": 1.21,
 +            "qty":0
 +        }
 +    }
 +
 +    # do the job
 +    for o in selected_objects:
 +        if o.type != 'EMPTY':
 +            # get the object's name
 +            object_name = o.name
 +
 +            for item, details in inventory.items():
 +                if object_name.__contains__(item):
 +                    details["qty"] += 1
 +                    # longueur bande adhesive
 +                    if object_name.__contains__("bande-adhesive-epdm-100mm"):
 +                        inventory["longueur-en-metres-bande-adhesive"]["qty"] += 0.1
 +                    if object_name.__contains__("bande-adhesive-epdm-150mm"):
 +                        inventory["longueur-en-metres-bande-adhesive"]["qty"] += 0.15
 +                    if object_name.__contains__("bande-adhesive-epdm-1000mm"):
 +                        inventory["longueur-en-metres-bande-adhesive"]["qty"] += 1
 +                    if object_name.__contains__("bande-adhesive-epdm-2000mm"):
 +                        inventory["longueur-en-metres-bande-adhesive"]["qty"] += 2
 +                    # longueur lambourde a decouper
 +                    if object_name.__contains__("lambourde-omega 35-100mm"):
 +                        inventory["longueur-en-metres-lambourde-a-decouper"]["qty"] += 0.1
 +                    if object_name.__contains__("lambourde-omega 35-150mm"):
 +                        inventory["longueur-en-metres-lambourde-a-decouper"]["qty"] += 0.15
 +                    # longueur profil de descente
 +                    if object_name.__contains__("profil-de-descente-55-20-100mm"):
 +                        inventory["longueur-en-metres-profil-de-descente"]["qty"] += 0.1
 +    return inventory
 +
 +def montre(inventory):
 +    total_price = 0
 +    for item, value in inventory.items():
 +        if not item.__contains__("bande-adhesive-epdm")\
 +        and not item.__contains__("lambourde-omega 35-100mm")\
 +        and not item.__contains__("lambourde-omega 35-150mm")\
 +        and not item.__contains__("profil-de-descente-55-20-100mm"):
 +            if value["qty"] > 0:
 +                print(item)
 +                print("\t\tqty:\t"+ str(round(value["qty"], 2)))
 +                print("\t\tp/u:\t"+ str(value["price"])+ " ht")
 +                price = value["qty"]*value["price"]
 +                total_price += price
 +                print("\ttotal price:\t"+ str(round(price, 2))+ " ht\n")
 +    print("----------------------------------------")
 +    print("total price: "+ str(round((total_price* 1.206), 2))+ " ttc")
 +
 +def writeCSV(inventory):
 +    total_price = 0
 +    rows = [['designation', 'qty', 'pu', 'total']]
 +    for item, value in inventory.items():
 +        if not item.__contains__("bande-adhesive-epdm")\
 +        and not item.__contains__("lambourde-omega 35-100mm")\
 +        and not item.__contains__("lambourde-omega 35-150mm")\
 +        and not item.__contains__("profil-de-descente-55-20-100mm"):
 +            if value["qty"] > 0:
 +                price = value["qty"]*value["price"]
 +                rows.append([item, round(value["qty"], 2), value["price"], round(price, 2)])
 +
 +    with open(bpy.path.abspath("//")+'\\prix_terrasse.csv', 'w', newline='') as file: 
 +        writer = csv.writer(file, delimiter='|')
 +        writer.writerows(rows)
 +    
 +print("========================================")
 +bpy.ops.object.select_all(action='DESELECT')
 +# get collection
 +col = bpy.data.collections["terrassteel-droit"]
 +
 +# get all children collections recursively
 +for subcol in col.children_recursive:
 +    for obj in col.all_objects:
 +        obj.select_set(True)
 +
 +
 +inventory = cherche()
 +montre(inventory)
 +writeCSV(inventory)
 +print("========================================")
 +</sxh>
 +
 +==== script as module ====
 +<sxh python>
 +import bpy
 +my_module = bpy.data.texts["Text"].as_module()
 +
 +my_module.toto()
 +</sxh>
 +
 +==== delete collection ====
 +<sxh python>
 +# source : https://blender.stackexchange.com/a/173894
 +
 +import bpy
 +#from bpy import context
 +
 +
 +name = "Collection 1"
 +remove_collection_objects = True
 +
 +#coll = context.collection # 
 +coll = bpy.data.collections.get(name)
 +
 +if coll:
 +    if remove_collection_objects:
 +        obs = [o for o in coll.objects if o.users == 1]
 +        while obs:
 +            bpy.data.objects.remove(obs.pop())
 +
 +    bpy.data.collections.remove(coll)
 +</sxh>
 +
 +==== duplicate collection linked ====
 +<sxh python>
 +import bpy
 +
 +def duplicate_collection_by_name(collection_name):
 +    original_collection = bpy.data.collections.get(collection_name)
 +    
 +    if original_collection is None:
 +        print(f"Collection '{collection_name}' does not exist.")
 +        return
 +    
 +    new_collection = original_collection.copy()
 +    new_collection.name = f"{collection_name}_duplicate"
 +    
 +    bpy.context.scene.collection.children.link(new_collection)
 +
 +</sxh>
 +
 +==== duplicate collection ====
 +<sxh python>
 +import bpy
 +
 +def duplicate_collection_by_name(collection_name):
 +    original_collection = bpy.data.collections.get(collection_name)
 +    
 +    if original_collection is None:
 +        print(f"Collection '{collection_name}' does not exist.")
 +        return
 +    
 +    new_collection = bpy.data.collections.new(f"{collection_name}_duplicate")
 +    bpy.context.scene.collection.children.link(new_collection)
 +    
 +    for obj in original_collection.objects:
 +        new_object = obj.copy()
 +        new_object.data = obj.data.copy()
 +        new_collection.objects.link(new_object)
 +
 +</sxh>
 +
 +==== delete empty collection ====
 +<sxh python>
 +import bpy
 +
 +def delete_empty_collections():
 +    collections = bpy.data.collections
 +    
 +    for collection in collections:
 +        if len(collection.objects) == 0:
 +            bpy.data.collections.remove(collection, do_unlink=True)
 +
 +</sxh>
 +
 +==== delete collection and its content ====
 +<sxh python>
 +import bpy
 +
 +def delete_collection_by_name(collection_name):
 +    collection = bpy.data.collections.get(collection_name)
 +    
 +    if collection is not None:
 +        bpy.data.collections.remove(collection, do_unlink=True)
 +    else:
 +        print(f"Collection '{collection_name}' does not exist.")
 +</sxh>
 +
 {{tag>blender}} {{tag>blender}}
blender.1644236959.txt.gz · Last modified: by 127.0.0.1