home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / share / k3d / scripts / scripted_plugins / mirror_mesh.py < prev    next >
Encoding:
Python Source  |  2008-12-29  |  5.5 KB  |  133 lines

  1. #python
  2. # k3d:plugin-class="application"
  3. # k3d:plugin-type="ActionScript"
  4. # k3d:plugin-name="Mirror and Weld"
  5. # ngui:action=""
  6.  
  7. import k3d
  8.  
  9. class MirrorMesh:
  10.     """
  11.         This class implements functions to Mirror selected Instances.
  12.         At the construction time the Mirror objects are created, there is no need to call a method later.
  13.         (actually is just like calling a function)
  14.         Usage:
  15.         MirrorMesh(document, mesh_instance, axis="x", new_node="reuse", new_mesh_instance_sufix=" plus Mirror")
  16.         There are two ways of mirroring:
  17.              -reusing the same instance (thus eliminating the option of
  18.             keep modifying the original half of object) Example:
  19.                 temp_unnecessary_object = MirrorMesh(Document, node, "reuse")
  20.             -creating parallel instance withe the name <Original mesh instance name>+<new_mesh_instance_sufix>
  21.             Example:
  22.                 MirrorMesh(Document, node, "new", "Mirror")
  23.                 #where "Mirror" is the sufix of the new created instances
  24.     """
  25.     def __init__(self, document, mesh_instance, axis="x", new_node="reuse", new_mesh_instance_sufix="Mirror and Weld"):
  26.         self.doc = document
  27.         self.mesh_instance = mesh_instance
  28.         if new_node ==  "new":
  29.             self.new_mesh_instance_sufix = new_mesh_instance_sufix
  30.             self.create_new_mesh_instance()
  31.         self.axis = axis
  32.         
  33.         self.create_nodes()
  34.         self.mesh_instance_child = self.get_child(self.mesh_instance)
  35.         self.set_dependencies()
  36.         self.set_mirror_matrix()
  37.         self.set_modifiers_selection()
  38.     def create_new_mesh_instance(self):
  39.         self.old_mesh_instance = self.mesh_instance
  40.         self.mesh_instance = self.doc.new_node("MeshInstance")
  41.         self.mesh_instance.name = self.old_mesh_instance.name +" "+ self.new_mesh_instance_sufix
  42.         self.mesh_instance.gl_painter = self.get_node("GL Default Painter")
  43.         self.mesh_instance.ri_painter = self.get_node("RenderMan Default Painter")
  44.         self.connect_nodes("mesh_instance","input_mesh","old_mesh_instance","output_mesh")
  45.     def create_nodes(self):
  46.         self.create_node("Weld", "weld", "Weld", "Weld Points Modifier")
  47.         self.create_node("MergeMesh", "merge_mesh", "MergeMesh", "MergeMesh Modifier")
  48.         self.create_merge_mesh_inputs()
  49.         self.create_node("TransformPoints", "transform_points", "Transformation Mirror", "TransformPoints Modifier")
  50.         self.create_node("FrozenTransformation", "frozen_matrix", "FrozenTransformation", "Matrix for the TransformPoints Modifier")
  51.         self.create_node("FlipOrientation", "flip_orientation", "FlipOrientation", "FlipOrientation of the TransformPoints output")
  52.     def create_node(self, type_name, property_name, label, description):
  53.         node = self.doc.new_node(type_name)
  54.         exec("self."+ property_name + " = node")
  55.     def create_merge_mesh_inputs(self):
  56.         self.merge_mesh.create_property("k3d::mesh*", "input_mesh", "Input Mesh", "")
  57.         self.merge_mesh.create_property("k3d::mesh*", "input_mesh1", "Input Mesh 1", "")
  58.     def set_dependencies(self):
  59.         self.connect_nodes("transform_points","input_matrix","frozen_matrix","output_matrix")
  60.         self.connect_nodes("transform_points","input_mesh","mesh_instance_child","output_mesh")
  61.         self.connect_nodes("flip_orientation","input_mesh","transform_points","output_mesh")
  62.         self.connect_nodes("merge_mesh","input_mesh","flip_orientation","output_mesh")
  63.         self.connect_nodes("merge_mesh","input_mesh1","mesh_instance_child","output_mesh")
  64.         self.connect_nodes("weld","input_mesh","merge_mesh","output_mesh")
  65.         self.connect_nodes("mesh_instance","input_mesh","weld","output_mesh")
  66.     def connect_nodes(self, name1, property1, name2, property2):
  67.         exec("node1 = self."+name1)
  68.         exec("node2 = self."+name2)
  69.         self.doc.set_dependency(node1.get_property(property1), node2.get_property(property2))
  70.     def set_mirror_matrix(self):
  71.         matrix = self.frozen_matrix.matrix
  72.         x = y = z = 1
  73.         if self.axis.lower() == "x":
  74.             x=-x
  75.         elif self.axis.lower() == "y":
  76.             y=-y
  77.         else:
  78.             z=-z
  79.         row = matrix[0]; row[0] = x; matrix[0] = row
  80.         row = matrix[1]; row[1] = y; matrix[1] = row
  81.         row = matrix[2]; row[2] = z; matrix[2] = row
  82.         self.frozen_matrix.matrix = matrix
  83.     def set_modifiers_selection(self):
  84.         selection = k3d.mesh_selection.select_all()
  85.         self.transform_points.mesh_selection = selection
  86.         selection = k3d.mesh_selection.select_all()
  87.         self.flip_orientation.mesh_selection = selection
  88.     def get_child(self, node):
  89.         return self.doc.get_dependency(node.get_property("input_mesh")).node()
  90.     def get_factory_id(self, name):
  91.         temp_node = self.doc.new_node(name)
  92.         id_node_factory = temp_node.factory().factory_id()
  93.         self.doc.delete_node(temp_node)
  94.         return id_node_factory
  95.     def get_node(self, name):
  96.         node = self.doc.get_node(name)
  97.         if hasattr(node, "name"):
  98.             return node
  99.         return None
  100.  
  101. def get_factory_id( name):
  102.     temp_node = Document.new_node(name)
  103.     id_node_factory = temp_node.factory().factory_id()
  104.     Document.delete_node(temp_node)
  105.     return id_node_factory
  106.  
  107. chosen_axis = k3d.ui().query_message("Choose the mirroring Axis:", ["X", "Y", "Z"])
  108. keep_original = k3d.ui().query_message("Keep the Original Instance?", ["No", "Yes"])
  109.  
  110. if chosen_axis == 1:
  111.     axis = "x"
  112. elif chosen_axis == 2:
  113.     axis = "y"
  114. else:
  115.     axis = "z"
  116.  
  117. if keep_original == 1: #dont keep
  118.     creation_mode = "reuse"
  119. else: #keep
  120.     creation_mode = "new"
  121.  
  122. Document.start_change_set()
  123. try: 
  124.     selection_node = Document.get_node_by_metadata("inode_selection", "ngui:unique_node", "node_selection")
  125.     id_msh_inst = get_factory_id("MeshInstance")
  126.     for node in Document.nodes():
  127.         if id_msh_inst == node.factory().factory_id() and selection_node.selection_weight(node):
  128.             MirrorMesh(Document, node, axis, creation_mode)
  129.     Document.finish_change_set("Mirror Selected Mesh Instances")
  130. except: #there was an error
  131.     Document.cancel_change_set()
  132.     raise
  133.