[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [3780] branches/protopipe: Modelbuilder inserts pipes now.

Aurel W aurel.w at gmail.com
Thu Sep 20 14:44:15 CEST 2012


Revision: 3780
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=3780
Author:   aurel
Date:     2012-09-20 12:44:14 +0000 (Thu, 20 Sep 2012)
Log Message:
-----------
Modelbuilder inserts pipes now.

Modified Paths:
--------------
    branches/protopipe/TODO
    branches/protopipe/modelbuilder.py
    branches/protopipe/pipeproto.py

Modified: branches/protopipe/TODO
===================================================================
--- branches/protopipe/TODO	2012-09-20 12:20:25 UTC (rev 3779)
+++ branches/protopipe/TODO	2012-09-20 12:44:14 UTC (rev 3780)
@@ -1,5 +1,6 @@
 * Make numpy optional
-* Mapping + length check
++ (DONE) Mapping
+* length check
 * Apply model transformations
 * Make deviation tollerance controlable
 * Apply mesh modifiers
@@ -18,3 +19,6 @@
     - Approximate the center of mass by assuming a set of bounding
       boxes with a mass. Iterative solution: reduce two bounding boxes
       to one.
+
+Tools:
+    - signal/slot properties in blender

Modified: branches/protopipe/modelbuilder.py
===================================================================
--- branches/protopipe/modelbuilder.py	2012-09-20 12:20:25 UTC (rev 3779)
+++ branches/protopipe/modelbuilder.py	2012-09-20 12:44:14 UTC (rev 3780)
@@ -20,37 +20,92 @@
 import bmesh
 import mathutils
 
+import math
+
 from protopipe.jointproto import JointProto
 from protopipe.jointproto import JointDescriptor
 from protopipe.jointproto import ProtoLibrary
 from protopipe.jointproto import JointRegistration
+
+from protopipe.pipeproto import PipeProto
+from protopipe.pipeproto import PipeFactory
+from protopipe.pipeproto import PipeDescriptor
+
 from protopipe import ppapp
 
+from protopipe.utils import *
 
+
 class ModelBuilder:
 
     def __init__(self, obj):
+        # the object to build 
+        self.baseObj = obj
+
         self.buildLayer = 1
-        self.obj = obj
+        self.defaultDiameter = 0.2
+
+        # generating the name for new objects
         self.nextJointName = self.nextJointNameGen().__next__
-        pass
+        self.nextPipeName = self.nextPipeNameGen().__next__
 
+
     def buildOnMesh(self):
         bm = bmesh.new()
-        bm.from_mesh(self.obj.data)
+        bm.from_mesh(self.baseObj.data)
         bm.verts.index_update()
         bm.edges.index_update()
 
-        ### for every vertice, try to add a joint ###
+        # The length of the individual pipes on an edge
+        # Substract in-length on both ends.
+        pipeLengths = {}
+
+        ### for every vertice try to add a joint ###
         for vert in bm.verts:
             desc = JointDescriptor()
             desc.buildFromVert(bm, vert.index)
             (reg, jr) = ppapp.jointFactory.getProto(desc)
+
             if reg:
-                print("inserting model for joint")
                 self.insertJoint(vert.co, jr)
+            else:
+                continue
+            
+            # substract in-lengths from edges
+            for (i, mappedI) in enumerate(jr.mapping):
+                edge = vert.link_edges[mappedI]
 
+                sub = [0,0]
+                if edge.index in pipeLengths.keys():
+                    sub = pipeLengths[edge.index]
 
+                if edge.verts[0] == vert:
+                    #FIXME check if mapping is not inverted (or bug)
+                    sub[0] += jr.jproto.jointLengthsIn[i]
+                else:
+                    sub[1] += jr.jproto.jointLengthsIn[i]
+
+                pipeLengths[edge.index] = sub
+
+        ### for every edge try to add a pipe ###
+        for edge in bm.edges:
+            # the physical length of the pipe:
+            plength = edge.calc_length() - sum(pipeLengths[edge.index])
+            #FIXME handle minimum length
+            vert = edge.verts[0]
+            vec = edge.verts[1].co - vert.co
+            #FIXME proper origin
+            vec = vec.normalized() * (pipeLengths[edge.index][0]+(plength/2.0))
+
+            # build pipe descriptor
+            desc = PipeDescriptor(plength, self.defaultDiameter)
+
+            # get a new pipe
+            (isreg, pipeReg) = ppapp.pipeFactory.getProto(desc)
+            self.insertPipe(vert.co, vec, pipeReg)
+            
+
+
     def insertJoint(self, co, jr):
         model = jr.modelInstance
         # put on a layer
@@ -66,24 +121,38 @@
         model.name = self.nextJointName()
 
 
-    def insertPipe(self, co, rot_mat, pproto):
+    def insertPipe(self, co, vec, pipeReg):
         # - Translate by the in-length or have the model created accordingly
+        model = pipeReg.modelInstance
+        model.location = co + vec
         # - Get the edge vector
         # - Specify a axis where the pipe is oriented as a Vector
+        zaxis = mathutils.Vector((0,0,1))
         # - Compute the cross product between those two vectors
+        aavec = vec.cross(zaxis)
+        aavec.normalize()
         # - Compute the angle between both vectors
-        # - Define a quaternion with axis/angle
+        #   ... and invert transformation
+        aangle = math.acos( clamp11(zaxis.dot(vec.normalized())) ) * -1.0
+        print("aangle: ", aangle)
         # - Rotate by the quaternion
+        model.rotation_mode = 'AXIS_ANGLE'
+        model.rotation_axis_angle = [aangle, aavec[0], aavec[1], aavec[2]]
         # - Bake that Cake!
-        #nobj = None
-        pass # over that joint
-        
+        model.name = self.nextPipeName()
 
 
+
     def nextJointNameGen(self):
         i = 0
         while True:
-            yield self.obj.name + "_j" + str(i)
+            yield self.baseObj.name + "_j" + str(i)
             i += 1
 
 
+    def nextPipeNameGen(self):
+        i = 0
+        while True:
+            yield self.baseObj.name + "_p" + str(i)
+
+

Modified: branches/protopipe/pipeproto.py
===================================================================
--- branches/protopipe/pipeproto.py	2012-09-20 12:20:25 UTC (rev 3779)
+++ branches/protopipe/pipeproto.py	2012-09-20 12:44:14 UTC (rev 3780)
@@ -39,7 +39,7 @@
         self.diameter = descriptor.diameter
 
         # generate the acording mesh object
-        bpy.ops.mesh.primitive_cylinder_add(radius=diameter)
+        bpy.ops.mesh.primitive_cylinder_add(radius=self.diameter)
         model = bpy.context.active_object
         model.scale[2] = (self.length/2.0)
         self.model = model
@@ -68,7 +68,7 @@
     No transformation to a mesh-edge is specified.
     """
 
-    def __init__(self, pProto, descritpor, modelInstance=None):
+    def __init__(self, pipeProto, descriptor, modelInstance=None):
         self.descriptor = descriptor
         self.pipeProto = pipeProto
         self.modeInstance = modelInstance
@@ -94,9 +94,9 @@
 
     def search(self, descriptor):
         pProto = PipeProto()
-        (isreg, pReg) = pProto.register(descriptor)
+        (isreg, pReg) = pProto.registerDescriptor(descriptor)
         # the prototype is single, so use model as instance
-        pReg.modelInstance = pReg.PipeProto.model
+        pReg.modelInstance = pReg.pipeProto.model
         if isreg:
             return (True, pReg)
 
@@ -111,7 +111,7 @@
     def getProto(self, descriptor):
         for provider in self.providers:
             (isreg, pReg) = provider.search(descriptor)
-            if reg:
+            if isreg:
                 return (True, pReg)
 
 



More information about the Bf-extensions-cvs mailing list