2024-01-06 03:51:45 +01:00
|
|
|
"""
|
|
|
|
Basic FileHandler for Operator that imports multiple files
|
2024-01-06 06:53:42 +01:00
|
|
|
----------------------------------------------------------
|
2024-01-06 03:51:45 +01:00
|
|
|
|
|
|
|
Also operators can be invoked with multiple files from 'drag-and-drop',
|
|
|
|
but for this it is require to define the following properties:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2024-01-06 06:53:42 +01:00
|
|
|
|
2024-01-06 03:51:45 +01:00
|
|
|
directory: StringProperty(subtype='FILE_PATH')
|
|
|
|
files: CollectionProperty(type=bpy.types.OperatorFileListElement)
|
|
|
|
|
|
|
|
This ``directory`` and ``files`` properties now will be used by the
|
|
|
|
``FileHandler`` to set 'drag-and-drop' filepath data.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
import bpy
|
2024-03-29 01:22:51 +01:00
|
|
|
from bpy_extras.io_utils import ImportHelper
|
2024-01-06 03:51:45 +01:00
|
|
|
from mathutils import Vector
|
|
|
|
|
|
|
|
|
2024-03-29 01:22:51 +01:00
|
|
|
class ShaderScriptImport(bpy.types.Operator, ImportHelper):
|
2024-01-06 03:51:45 +01:00
|
|
|
"""Test importer that creates scripts nodes from .txt files"""
|
|
|
|
bl_idname = "shader.script_import"
|
|
|
|
bl_label = "Import a text file as a script node"
|
|
|
|
|
|
|
|
"""
|
|
|
|
This Operator can import multiple .txt files, we need following directory and files
|
|
|
|
properties that the file handler will use to set files path data
|
|
|
|
"""
|
2024-03-29 01:22:51 +01:00
|
|
|
directory: bpy.props.StringProperty(subtype='FILE_PATH', options={'SKIP_SAVE', 'HIDDEN'})
|
|
|
|
files: bpy.props.CollectionProperty(type=bpy.types.OperatorFileListElement, options={'SKIP_SAVE', 'HIDDEN'})
|
|
|
|
|
|
|
|
"""Allow the user to select if the node's label is set or not"""
|
|
|
|
set_label: bpy.props.BoolProperty(name="Set Label", default=False)
|
2024-01-06 03:51:45 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def poll(cls, context):
|
|
|
|
return (context.region and context.region.type == 'WINDOW'
|
|
|
|
and context.area and context.area.ui_type == 'ShaderNodeTree'
|
|
|
|
and context.object and context.object.type == 'MESH'
|
|
|
|
and context.material)
|
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
""" The directory property need to be set. """
|
|
|
|
if not self.directory:
|
|
|
|
return {'CANCELLED'}
|
|
|
|
x = 0.0
|
|
|
|
y = 0.0
|
|
|
|
for file in self.files:
|
|
|
|
"""
|
|
|
|
Calls to the operator can set unfiltered file names,
|
|
|
|
ensure the file extension is .txt
|
|
|
|
"""
|
|
|
|
if file.name.endswith(".txt"):
|
|
|
|
node_tree = context.material.node_tree
|
|
|
|
text_node = node_tree.nodes.new(type="ShaderNodeScript")
|
|
|
|
text_node.mode = 'EXTERNAL'
|
|
|
|
import os
|
|
|
|
filepath = os.path.join(self.directory, file.name)
|
|
|
|
text_node.filepath = filepath
|
|
|
|
text_node.location = Vector((x, y))
|
2024-03-29 01:22:51 +01:00
|
|
|
|
|
|
|
# Set the node's title to the file name
|
|
|
|
if self.set_label:
|
|
|
|
text_node.label = file.name
|
|
|
|
|
2024-01-06 03:51:45 +01:00
|
|
|
x += 20.0
|
|
|
|
y -= 20.0
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
"""
|
2024-03-29 01:22:51 +01:00
|
|
|
Use ImportHelper's invoke_popup() to handle the invocation so that this operator's properties
|
|
|
|
are shown in a popup. This allows the user to configure additional settings on the operator like
|
|
|
|
the `set_label` property. Consider having a draw() method on the operator in order to layout the
|
|
|
|
properties in the UI appropriately.
|
2024-01-06 03:51:45 +01:00
|
|
|
|
2024-03-29 01:22:51 +01:00
|
|
|
If filepath information is not provided the file select window will be invoked instead.
|
|
|
|
"""
|
2024-03-29 01:55:23 +01:00
|
|
|
|
2024-01-06 03:51:45 +01:00
|
|
|
def invoke(self, context, event):
|
2024-03-29 01:22:51 +01:00
|
|
|
return self.invoke_popup(context)
|
2024-01-06 03:51:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SHADER_FH_script_import(bpy.types.FileHandler):
|
|
|
|
bl_idname = "SHADER_FH_script_import"
|
|
|
|
bl_label = "File handler for shader script node import"
|
|
|
|
bl_import_operator = "shader.script_import"
|
|
|
|
bl_file_extensions = ".txt"
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def poll_drop(cls, context):
|
|
|
|
return (context.region and context.region.type == 'WINDOW'
|
|
|
|
and context.area and context.area.ui_type == 'ShaderNodeTree')
|
|
|
|
|
|
|
|
|
|
|
|
bpy.utils.register_class(ShaderScriptImport)
|
|
|
|
bpy.utils.register_class(SHADER_FH_script_import)
|