Ordnerinhalt in template_list anzeigen

Blenders Funktionsumfang mit Python-Scripten erweitern.

Re: Ordnerinhalt in template_list anzeigen

Beitragvon Tiles » 22.10.2016, 11:25

Hui, vielen Dank. Das muss ich gleich mal noch mal studieren \o/

EDIT sagt, Prinzip erkannt, und Fehler entdeckt. Vielen Dank dafür :)

Ich habe zwischenzeitlich mal das Addon weitergebaut. Das ist nun im Tool Shelf unter Create , Minilib Panel. Und eigentlich fast fertig. Es tut was es soll. Es läuft auch brav als Script. Kurioserweise lässt es sich aber noch nicht als Addon installieren :/

Auch da Edit: da scheine ich mal wieder in eine für mich unsinnige Limitierung zu rennen dass Addons keine globalen Variablen verwenden kann. Ich frage mich jetzt noch welchen Sinn das haben soll. Denn beim Scripten kann man ja. Und stehe natürlich mal wieder vor dem Problem das jetzt fixen zu müssen. Hach ...

String Property ist nun ein Directory Loader. Die Index Variable ist komplett entsorgt, das greift den Index jetzt direkt vom Prop ab. Irgendwas hatte ich da eh verbockt. Beim Laden war die Index Variable immer wieder auf Null, obwohl sie in der def myindex ja gesetzt wurde -.- . def myindex und update habe ich auch komplett entsorgt. Und auch ansonsten habe ich noch mal kräftig hin und hergeräumt und nachkommentiert.

Achtung, das Addon appendet alles im blend file ...

So siehts im Moment aus:
Code: Alles auswählen
# The script is under Apache license

# Big thanks to the helpful souls at Blendpolis

import bpy
import os

from bpy.props import StringProperty

bl_info = {
    "name": "Minilib",
    "description": "A mini library addon",
    "author": "Reiner 'Tiles' Prokein",
    "version": (0.1),
    "blender": (2, 76, 0),
    "location": "Tool Shelf > Create > Minilib",
    "warning": "",
    "wiki_url": "none",
    "category": "Create"}

 # -----------------------------------------------------------------------------------------------------
 # the Minilib panel is in the Tool Shelf in the Create tab
 # -----------------------------------------------------------------------------------------------------
 
mylist = [] # the list to retreive the files in the target folder
my_listing = [] # the list to create the dropdown box.

bpy.types.Scene.path = bpy.props.StringProperty(name="dir_path", description="", default="", maxlen=1024, subtype='DIR_PATH') # Directory loader
bpy.context.scene.path ="C:/myassets" # the directory to check

############################# Read Asset

class VIEW3D_read_asset(bpy.types.Operator):
    """Blubb"""
    bl_idname = "view3d.read_asset"
    bl_label = "View Selected All Regions"
    bl_options = {'REGISTER', 'UNDO'}
   
    def execute(self, context):

        scn = bpy.context.scene # current scene   
        filepath = scn.path + "/" + str(mylist[int(context.scene.MyEnum)] + ".blend") # path to the blend       
        link = False # append, set to true to keep the link to the original file. There is no append item.     
       
        #append all objects from .blend file
        with bpy.data.libraries.load(filepath) as (data_from, data_to):
            data_to.objects = data_from.objects
                       
         # When you want to load just specific object types
#        obj_name = "Cube" # name of object(s) in the lib file to append or link   
#        with bpy.data.libraries.load(filepath, link=link) as (data_from, data_to):
#            data_to.objects = [name for name in data_from.objects if name.startswith(obj_name)]

        #link object to current scene
        for obj in data_to.objects:
            if obj is not None:
               scn.objects.link(obj)
               obj.select = False # We don't want to have the asset selected after loading
        return {'FINISHED'}

############################# read directory

def read_dir(path):
        mylist.clear() # clear the list. We don't want double, triple, etc. entries.
        my_listing.clear() # clear the list. We don't want double, triple, etc. entries.
        for path,dirs,files in os.walk(path):
            for filename in files:
                if filename.endswith(".blend"):
                    filename = os.path.splitext(filename)[0] # just filename, no extention name
                    mylist.append(os.path.join(filename)) # add the files to the mylist list.
       
        # fill the list my_listing
        for n, entry in enumerate(mylist):
            my_listing.append((str(n), entry, entry+".blend"))
        return my_listing

class VIEW3D_read_dir(bpy.types.Operator):
    """Blubb"""
    bl_idname = "view3d.read_dir"
    bl_label = "Read Dir"

    def execute(self, context):
        read_dir(path)
        return {'FINISHED'}
   
#################### Enumerate the dropdown list in the dropdown prop

old_path = ""
old_dir = ""

def item_cb(self, context):
    global old_path, old_dir
    # Check if path is the same
    if bpy.context.scene.path == old_path:
        return old_dir
    #Path has changed or is not set. So read the dir again.
    old_path = bpy.context.scene.path
    old_dir = read_dir(bpy.context.scene.path)
    return old_dir
   
########################### the panel #############################

class MyPanel(bpy.types.Panel):
    bl_label = "Minilib"
    bl_space_type = 'VIEW_3D'
    bl_region_type = "TOOLS"
    bl_category = "Create"

    def draw(self, context):
        layout = self.layout

        scene = context.scene
 
        layout.prop(scene, "MyEnum", text = "Item") # Dropdown box
        layout.operator("view3d.read_asset", text="append asset") # Load asset
        layout.prop(context.scene, "path", text="") # Here you can change the path.

# ------------------------------ register unregister --------------------------------------------------------

def register():   
    bpy.types.Scene.MyEnum = bpy.props.EnumProperty(items = item_cb)   
   
    bpy.utils.register_module(__name__)
   
def unregister():   
    del bpy.types.Scene.MyEnum
   
    bpy.utils.unregister_module(__name__)   
       
if __name__ == "__main__":
    register()
Dateianhänge
attributeerror.jpg
Zuletzt geändert von Tiles am 22.10.2016, 12:17, insgesamt 1-mal geändert.
Free Game Graphics, Freeware Games http://www.reinerstilesets.de
Benutzeravatar
Tiles
 
Registriert:
20.11.2007, 12:19

Re: Ordnerinhalt in template_list anzeigen

Beitragvon Tiles » 22.10.2016, 11:52

Kleiner Schritt weiter. Ich habe dem Ding jetzt einfach erst mal den default Pfad weggenommen. Das bpy.context.scene.path. Und auch die String Property nun ins Register gepackt. Ein Default Pfad wäre natürlich schon wichtig -.-

EDIT sagt, dabei liegt die Lösung doch genau vor meinen Ohren! Die String Property hat doch ein Default. Eieiei.

Script entsprechend abgeändert. Habe fertig. Läuft rund. Wenn noch jemand was findet, immer her damit. Ich wäre ja immer noch dran interessiert wie man das jetzt auch noch mit der template_list lösen könnte :)

Jedenfalls vielen Dank für die Hilfe bisher :)

Code: Alles auswählen
# The script is under Apache license

# Big thanks to the helpful souls at Blendpolis

import bpy
import os

from bpy.props import StringProperty

bl_info = {
    "name": "Minilib",
    "description": "A mini library addon",
    "author": "Reiner 'Tiles' Prokein",
    "version": (0,1,1),
    "blender": (2, 76, 0),
    "location": "Tool Shelf > Create > Minilib",
    "warning": "",
    "wiki_url": "none",
    "category": "Create"}

 # -----------------------------------------------------------------------------------------------------
 # the Minilib panel is in the Tool Shelf in the Create tab
 # -----------------------------------------------------------------------------------------------------
 
mylist = [] # the list to retreive the files in the target folder
my_listing = [] # the list to create the dropdown box.

############################# Read Asset

class VIEW3D_read_asset(bpy.types.Operator):
    """Blubb"""
    bl_idname = "view3d.read_asset"
    bl_label = "View Selected All Regions"
    bl_options = {'REGISTER', 'UNDO'}
   
    def execute(self, context):

        scn = bpy.context.scene # current scene   
        filepath = scn.path + "/" + str(mylist[int(context.scene.MyEnum)] + ".blend") # path to the blend       
        link = False # append, set to true to keep the link to the original file. There is no append item.     
       
        #append all objects from .blend file
        with bpy.data.libraries.load(filepath) as (data_from, data_to):
            data_to.objects = data_from.objects
                       
         # When you want to load just specific object types
#        obj_name = "Cube" # name of object(s) in the lib file to append or link   
#        with bpy.data.libraries.load(filepath, link=link) as (data_from, data_to):
#            data_to.objects = [name for name in data_from.objects if name.startswith(obj_name)]

        #link object to current scene
        for obj in data_to.objects:
            if obj is not None:
               scn.objects.link(obj)
               obj.select = False # We don't want to have the asset selected after loading
        return {'FINISHED'}

############################# read directory

def read_dir(path):
        mylist.clear() # clear the list. We don't want double, triple, etc. entries.
        my_listing.clear() # clear the list. We don't want double, triple, etc. entries.
        for path,dirs,files in os.walk(path):
            for filename in files:
                if filename.endswith(".blend"):
                    filename = os.path.splitext(filename)[0] # just filename, no extention name
                    mylist.append(os.path.join(filename)) # add the files to the mylist list.
       
        # fill the list my_listing
        for n, entry in enumerate(mylist):
            my_listing.append((str(n), entry, entry+".blend"))
        return my_listing

class VIEW3D_read_dir(bpy.types.Operator):
    """Blubb"""
    bl_idname = "view3d.read_dir"
    bl_label = "Read Dir"

   

    def execute(self, context):
       
        read_dir(path)
        return {'FINISHED'}
   
#################### Enumerate the dropdown list in the dropdown prop

old_path = ""
old_dir = ""

def item_cb(self, context):
    global old_path, old_dir
    # Check if path is the same
    if bpy.context.scene.path == old_path:
        return old_dir
    #Path has changed or is not set. So read the dir again.
    old_path = bpy.context.scene.path
    old_dir = read_dir(bpy.context.scene.path)
    return old_dir
   
########################### the panel #############################

class MyPanel(bpy.types.Panel):
    bl_label = "Minilib"
    bl_space_type = 'VIEW_3D'
    bl_region_type = "TOOLS"
    bl_category = "Create"

    def draw(self, context):
        layout = self.layout

        scene = context.scene

        layout.prop(scene, "MyEnum", text = "Item") # Dropdown box
        layout.operator("view3d.read_asset", text="append asset") # Load asset
        layout.prop(context.scene, "path", text="") # Here you can change the path.

# ------------------------------ register unregister --------------------------------------------------------

def register():
    bpy.types.Scene.path = bpy.props.StringProperty(name="dir_path", description="", default="C:/myassets", maxlen=1024, subtype='DIR_PATH') # Directory loader
   
    bpy.types.Scene.MyEnum = bpy.props.EnumProperty(items = item_cb)   
   
    bpy.utils.register_module(__name__)
   
def unregister():   
    del bpy.types.Scene.MyEnum
    del bpy.types.Scene.path
   
    bpy.utils.unregister_module(__name__)   
       
if __name__ == "__main__":
    register()
Free Game Graphics, Freeware Games http://www.reinerstilesets.de
Benutzeravatar
Tiles
 
Registriert:
20.11.2007, 12:19

Re: Ordnerinhalt in template_list anzeigen

Beitragvon Tiles » 23.10.2016, 08:52

poor auf Stackexchange hat noch mal ein super Beispiel für die template_list zusammengezimmert. Ich bin zwar immer noch am auseinandernehmen. Aber so langsam lichtet sich der Nebel ^^

https://gist.github.com/p2or/37ec841fa3 ... 5ebc4b1b42

Stack Exchange Beitrag: http://blender.stackexchange.com/questi ... plate-list
Free Game Graphics, Freeware Games http://www.reinerstilesets.de
Benutzeravatar
Tiles
 
Registriert:
20.11.2007, 12:19

Vorherige

Zurück zu Blender Python


Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 1 Gast