Skip to content

Instantly share code, notes, and snippets.

@zeffii
Forked from anonymous/node_Empty.py
Last active August 29, 2015 14:01
Show Gist options
  • Save zeffii/7a9bd8576373c8b122e3 to your computer and use it in GitHub Desktop.
Save zeffii/7a9bd8576373c8b122e3 to your computer and use it in GitHub Desktop.
import bpy
from node_s import *
from util import *
from bpy.props import FloatVectorProperty, StringProperty, BoolProperty
class svLinkedEmptyNode(Node, SverchCustomTreeNode):
''' Linked Empty to vector '''
bl_idname = 'svLinkedEmptyNode'
bl_label = 'Empty'
bl_icon = 'OUTLINER_OB_EMPTY'
n_id = StringProperty(default='', options={'SKIP_SAVE'})
bpy.types.Object.identifier = StringProperty(default='')
# bpy.types.Object.current_color = FloatVectorProperty(default=(.3, 0, 0), size=3)
empty_location = FloatVectorProperty(
default=(0, 0, 0), size=3)
empty_matrix = FloatVectorProperty(
default=(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1), size=16)
empty_name = StringProperty(default='')
# string prop for name
is_copy = BoolProperty(default=False)
def setup_new_empty(self):
bpy.ops.object.add(type='EMPTY', location=self.empty_location)
myob = bpy.context.active_object
self.label = myob.name
self.empty_name = myob.name
myob.identifier = str(self.n_id)
self.width = 140
self.height = 50
def add_outputs(self):
self.outputs.new('VerticesSocket', "Vector", "Vector")
self.outputs.new('MatrixSocket', "Matrix", "Matrix")
def init(self, context):
self.n_id = node_id(self)
self.setup_new_empty()
print('i am called', self.n_id, self.empty_name)
if self.is_copy:
# outputs already added
return
else:
# needs outputs
self.add_outputs()
# def draw_buttons(self, context,layout):
# ao = bpy.context.active_object
# if not ao:
# return
# if ao.identifier == str(self.n_id):
# ao.current_color = (0.7, .4, 1)
# else:
# ao.current_color = (1, 1, 1)
def find_empty(self):
for obj in bpy.data.objects:
if (obj.type == 'EMPTY') and (obj.identifier == str(self.n_id)):
return obj
def update(self):
self.n_id = node_id(self)
vector_out_socket = self.outputs.get('Matrix', None)
if vector_out_socket:
self.use_custom_color = True
Empty = self.find_empty()
if not Empty:
# print empty seems to be removed, delete node or recreate from
# .ext
self.color = (1, 0.3, 0.3)
return
self.empty_location = Empty.location
matrix = list(Empty.matrix_world) # no need to flip this time.
SvSetSocketAnyType(self, 'Vector', [Empty.location[:]])
SvSetSocketAnyType(self, 'Matrix', [matrix])
self.color = (1, 1, 1)
return
self.use_custom_color = False
def update_socket(self, context):
self.update()
def copy(self, node):
self.is_copy = True
self.n_id = ''
self.empty_location = node.empty_location
self.init(bpy.context)
def free(self):
pass
def register():
bpy.utils.register_class(svLinkedEmptyNode)
def unregister():
bpy.utils.unregister_class(svLinkedEmptyNode)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment