BlenderCN论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1930|回复: 2

修改内置的自定义节点pynode模块 适用于 所有节点树

[复制链接]
发表于 2013-6-21 12:30:45 | 显示全部楼层 |阅读模式
为什么要研究自定义节点?
先看两个链接:
http://wiki.blender.org/index.php/Dev:Ref/Release_Notes/2.67/Python_Nodes
http://wiki.blender.org/index.php/User:Phonybone/Python_Nodes

盗链两张图[点击查看大图]:




是的,pynode类似Softimage的ICE,或者能让blender变成houdini那样的全节点。



这个pynode是blender自带的范例,原文件在
X:\Program Files\Blender Foundation\blender 2.6\2.67\scripts\templates_py\custom_nodes.py

原文件在节点编辑器里增加了一个新的 自定义树,让自定义的节点只能在新的自定义节点树下工作。
这里修改的脚本,屏蔽了 新建自定义节点树,并让自定义节点能够 在 合成、贴图、材质 这三个节点树下 都能使用。

另外,系统集成的模板是不断升级的,当前修改的版本是2.67.1的,本帖子并不会随软件更新。
此修改版本,无任何实际功能,仅作为学习参考。
  1. import bpy
  2. from bpy_types import NodeTree, Node, NodeSocket

  3. # Implementation of custom nodes from Python


  4. # 不需要新的 节点树,已在 注册函数中,屏蔽此类
  5. # Derived from the NodeTree base type, similar to Menu, Operator, Panel, etc.
  6. class DemoCustomTree(NodeTree):
  7.     # Description string
  8.     '''A custom node tree type that will show up in the node editor header'''
  9.     # Optional identifier string. If not explicitly defined, the python class name is used.
  10.     bl_idname = 'DemoCustomTreeType'
  11.     # Label for nice name display
  12.     bl_label = 'Demo Demo Custom Node Tree'
  13.     # Icon identifier
  14.     # NOTE: If no icon is defined, the node tree will not show up in the editor header!
  15.     #       This can be used to make additional tree types for groups and similar nodes (see below)
  16.     #       Only one base tree class is needed in the editor for selecting the general category
  17.     bl_icon = 'NODETREE'


  18. # Custom socket type
  19. class DemoCustomSocket(NodeSocket):
  20.     # Description string
  21.     '''Custom node socket type'''
  22.     # Optional identifier string. If not explicitly defined, the python class name is used.
  23.     bl_idname = 'DemoCustomSocketType'
  24.     # Label for nice name display
  25.     bl_label = 'Demo Demo Custom Node Socket'

  26.     # Enum items list
  27.     my_items = [
  28.         ("DOWN", "Down", "Where your feet are"),
  29.         ("UP", "Up", "Where your head should be"),
  30.         ("LEFT", "Left", "Not right"),
  31.         ("RIGHT", "Right", "Not left")
  32.     ]

  33.     myEnumProperty = bpy.props.EnumProperty(name="Direction", description="Just an example", items=my_items, default='UP')

  34.     # Optional function for drawing the socket input value
  35.     def draw(self, context, layout, node, text):
  36.         if self.is_linked:
  37.             layout.label(text)
  38.         else:
  39.             layout.prop(self, "myEnumProperty", text=text)

  40.     # Socket color
  41.     def draw_color(self, context, node):
  42.         return (1.0, 0.4, 0.216, 0.5)

  43. # Mix-in class for all custom nodes in this tree type.
  44. # Defines a poll function to enable instantiation.
  45. class DemoCustomTreeNode :
  46.     """
  47.     # 注释掉这个,让节点适用于所有 节点树
  48.     # comment this for all node_tree
  49.     @classmethod
  50.     def poll(cls, ntree):
  51.         return ntree.bl_idname == 'DemoCustomTreeType'
  52.     """
  53.     pass

  54. # Derived from the Node base type.
  55. class DemoCustomNode(Node, DemoCustomTreeNode):
  56.     # === Basics ===
  57.     # Description string
  58.     '''A custom node'''
  59.     # Optional identifier string. If not explicitly defined, the python class name is used.
  60.     bl_idname = 'DemoCustomNodeType'
  61.     # Label for nice name display
  62.     bl_label = 'Demo Custom Node'
  63.     # Icon identifier
  64.     bl_icon = 'SOUND'

  65.     # === Custom Properties ===
  66.     # These work just like custom properties in ID data blocks
  67.     # Extensive information can be found under
  68.     # [url]http://wiki.blender.org/index.php/Doc:2.6/Manual/Extensions/Python/Properties[/url]
  69.     DemoStringProperty = bpy.props.StringProperty()
  70.     DemoFloatProperty = bpy.props.FloatProperty(default=3.1415926)

  71.     # === Optional Functions ===
  72.     # Initialization function, called when a new node is created.
  73.     # This is the most common place to create the sockets for a node, as shown below.
  74.     # NOTE: this is not the same as the standard __init__ function in Python, which is
  75.     #       a purely internal Python method and unknown to the node system!
  76.     def init(self, context):
  77.         self.inputs.new('DemoCustomSocketType', "Hello")
  78.         self.inputs.new('NodeSocketFloat', "World")
  79.         self.inputs.new('NodeSocketVector', "!")

  80.         self.outputs.new('NodeSocketColor', "How")
  81.         self.outputs.new('NodeSocketColor', "are")
  82.         self.outputs.new('NodeSocketFloat', "you")

  83.     # Copy function to initialize a copied node from an existing one.
  84.     def copy(self, node):
  85.         print("Copying from node ", node)

  86.     # Free function to clean up on removal.
  87.     def free(self):
  88.         print("Removing node ", self, ", Goodbye!")

  89.     # Additional buttons displayed on the node.
  90.     def draw_buttons(self, context, layout):
  91.         layout.label("Node settings")
  92.         layout.prop(self, "DemoFloatProperty")

  93.     # Detail buttons in the sidebar.
  94.     # If this function is not defined, the draw_buttons function is used instead
  95.     def draw_buttons_ext(self, context, layout):
  96.         layout.prop(self, "DemoFloatProperty")
  97.         # DemoStringProperty button will only be visible in the sidebar
  98.         layout.prop(self, "DemoStringProperty")


  99. ### Node Categories ###
  100. # Node categories are a python system for automatically
  101. # extending the Add menu, toolbar panels and search operator.
  102. # For more examples see release/scripts/startup/nodeitems_builtins.py

  103. import nodeitems_utils
  104. from nodeitems_utils import NodeCategory, NodeItem

  105. # our own base class with an appropriate poll function,
  106. # so the categories only show up in our own tree type
  107. class DemoNodeCategory(NodeCategory):
  108.     """
  109.     # comment this for all node_tree
  110.     # 注释掉这个,让节点适用于所有 节点树
  111.     @classmethod
  112.     def poll(cls, context):
  113.         return context.space_data.tree_type == 'DemoCustomTreeType'
  114.     """
  115.     pass

  116. # all categories in a list
  117. node_categories = [
  118.     # identifier, label, items list
  119.     DemoNodeCategory("SOMENODES", "Some Nodes", items=[
  120.         # our basic node
  121.         NodeItem("DemoCustomNodeType"),
  122.         ]),
  123.     DemoNodeCategory("OTHERNODES", "Other Nodes", items=[
  124.         # the node item can have additional settings,
  125.         # which are applied to new nodes
  126.         # NB: settings values are stored as string expressions,
  127.         # for this reason they should be converted to strings using repr()
  128.         NodeItem("DemoCustomNodeType", label="Node A",
  129.                 settings={
  130.                 "DemoStringProperty" : repr("Lorem ipsum dolor sit amet"),
  131.                 "DemoFloatProperty" : repr(1.0),
  132.                 }),
  133.         NodeItem("DemoCustomNodeType", label="Node B",
  134.                 settings={
  135.                 "DemoStringProperty" : repr("consectetur adipisicing elit"),
  136.                 "DemoFloatProperty" : repr(2.0),
  137.                 }),
  138.         ]),
  139.     ]


  140. def register():
  141.     #不需要新的 节点树
  142.     #bpy.utils.register_class(DemoCustomTree)
  143.     bpy.utils.register_class(DemoCustomSocket)
  144.     bpy.utils.register_class(DemoCustomNode)

  145.     nodeitems_utils.register_node_categories("CUSTOM_NODES", node_categories)


  146. def unregister():
  147.     nodeitems_utils.unregister_node_categories("CUSTOM_NODES")

  148.     #不需要新的 节点树
  149.     #bpy.utils.unregister_class(DemoCustomTree)
  150.     bpy.utils.unregister_class(DemoCustomSocket)
  151.     bpy.utils.unregister_class(DemoCustomNode)


  152. if __name__ == "__main__":
  153.     register()


复制代码
blender pynodes / custom nodes  for all node tree type / mode.



残留疑问http://www.blenderartists.org/forum/showthread.php?297657-what-s-the-quot-RNA_float_get-xxx-default_value-not-found-quot-in-pynode
发表于 2013-6-21 23:24:32 | 显示全部楼层
请教一下关于Geometry Nodes有没有好的案例可以学习。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-6-22 09:38:30 | 显示全部楼层
Geometry Nodes在上面链接里有git的下载,但是更新滞后,也还是测试中,每天版无法使用,其他版本没测试。
插件信息好像是2.66的。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

Blender最新中文教学视频|Blender头条|小黑屋|手机版|Archiver|Blender中国 ( 蜀ICP备17002929号 )360网站安全检测平台

GMT+8, 2019-11-14 05:27 , Processed in 0.016761 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表