Exemple #1
0
    def execute(self):
        bm = context.bm
        state = context.getState()
        shape = state.shape
        # get rectangle origin
        origin = shape.center()
        xVec = self.xSize/2 * xAxis
        yVec = self.ySize/2 * yAxis

        if self.replace:
            context.popState()
            shape.delete()
        
        # rotation matrix
        matrix = rotation_zNormal_xHorizontal(shape.firstLoop, shape.getNormal())
        shape = createRectangle((
            bm.verts.new( (-xVec - yVec)*matrix + origin),
            bm.verts.new( ( xVec - yVec)*matrix + origin),
            bm.verts.new( ( xVec + yVec)*matrix + origin),
            bm.verts.new( (-xVec + yVec)*matrix + origin)
        ))
        if self.operator:
            context.pushState(shape=shape)
            self.operator.execute()
            context.popState()
        elif self.replace:
            context.pushState(shape=shape)
 def execute(self):
     shape = context.getState().shape
     # We now know the absolute shape size, so let's update self.parts
     # for self.symmetric = True and self.relativeCoord1 = False
     if self.symmetric and not self.relativeCoord1:
         self.updateSymmetricAbsolute(shape)
     shapesWithRule = shape.extrude2(self.parts, self)
     # apply the rule for each shape in shapesWithRule list
     for entry in shapesWithRule:
         context.pushState(shape=entry[0])
         entry[1].execute()
         context.popState()
Exemple #3
0
 def execute(self):
     shape = context.getState().shape
     # only 2D shapes can be copied at the moment!
     if not isinstance(shape, Shape2d): return
     
     duplicate = bmesh.ops.duplicate(context.bm, geom = (shape.face,))
     copiedFace = [entry for entry in duplicate["geom"] if isinstance(entry, bmesh.types.BMFace)][0]
     constructor = type(shape)
     copiedShape = constructor(copiedFace.loops[0])
     
     if self.operator:
         context.pushState(shape=copiedShape)
         self.operator.execute()
         context.popState()
def decompose_execute(shape, _parts):	
	# create a dict from the operatorDef list
	parts = {}
	for part in _parts:
		parts[part.value] = part
	
	# components is a dictionary with a comp-selector as the key and a list of 2D-shapes as the related value 
	components = shape.decompose(parts)
	if len(components)>0:
		# now apply the rule for each decomposed 2D-shape
		for selector in components:
			for _shape in components[selector]:
				context.pushState(shape=_shape)
				parts[selector].execute()
				context.popState()
Exemple #5
0
	def execute(self):
		shape = context.getState().shape
		
		parts = self.parts if self.reverse==False else reversed(self.parts)
		# Calculate cuts.
		# cuts is a list of tuples (cutShape, ruleForTheCutShape)
		cuts = shape.split(self.direction, parts)
		
		if len(cuts)==1:
			# degenerate case, i.e. no cut is needed
			cuts[0][2].execute()
		else:
			# apply the rule for each cut
			for cut in cuts:
				context.pushState(shape=cut[1])
				cut[2].execute()
				context.popState()
Exemple #6
0
    def execute(self):
        shape = context.getState().shape

        parts = self.parts if self.reverse == False else reversed(self.parts)
        # Calculate cuts.
        # cuts is a list of tuples (cutShape, ruleForTheCutShape)
        cuts = shape.split(self.direction, parts)

        if len(cuts) == 1:
            # degenerate case, i.e. no cut is needed
            cuts[0][2].execute()
        else:
            # apply the rule for each cut
            for cut in cuts:
                context.pushState(shape=cut[1])
                cut[2].execute()
                context.popState()
 def execute(self):
     shape = context.getState().shape
     face = shape.face
     self.init(len(face.verts))
     manager = Manager()
     roof = Roof(face.verts, shape.getNormal(), manager)
     # soffits
     if self.soffits:
         manager.rule = self.soffit
         roof.inset(*self.soffits, negate=True)
     # fascias
     if self.fasciaSize:
         manager.rule = self.fascia
         roof.translate(self.fasciaSize)
     # hip roof itself
     manager.rule = self.face
     roof.roof(*self.pitches)
     shape.delete()
     # finalizing: if there is a rule for the shape, execute it
     for entry in manager.shapes:
         context.pushState(shape=entry[0])
         entry[1].execute()
         context.popState()
Exemple #8
0
 def execute(self):
     shape = context.getState().shape
     face = shape.face
     self.init(len(face.verts))
     manager = Manager()
     roof = Roof(face.verts, shape.getNormal(), manager)
     # soffits
     if self.soffits:
         manager.rule = self.soffit
         roof.inset(*self.soffits, negate=True)
     # fascias
     if self.fasciaSize:
         manager.rule = self.fascia
         roof.translate(self.fasciaSize)
     # hip roof itself
     manager.rule = self.face
     roof.roof(*self.pitches)
     shape.delete()
     # finalizing: if there is a rule for the shape, execute it
     for entry in manager.shapes:
         context.pushState(shape=entry[0])
         entry[1].execute()
         context.popState()
 def execute(self):
     shape = context.getState().shape
     face = shape.face
     manager = Manager()
     polygon = Polygon(face.verts, shape.getNormal(), manager)
     manager.rule = self.side
     kwargs = {"height": self.height} if self.height else {}
     polygon.inset(*self.insets, **kwargs)
     # create a shape for the cap if necessary
     cap = self.cap
     if not isinstance(cap, Delete):
         shape = polygon.getShape(type(shape))
         if cap:
             context.pushState(shape=shape)
             self.cap.execute()
             context.popState()
     if not self.keepOriginal:
         context.facesForRemoval.append(face)
     # finalizing: if there is a rule for the shape, execute it
     for entry in manager.shapes:
         context.pushState(shape=entry[0])
         entry[1].execute()
         context.popState()
Exemple #10
0
 def execute(self):
     shape = context.getState().shape
     face = shape.face
     manager = Manager()
     polygon = Polygon(face.verts, shape.getNormal(), manager)
     
     # process each inset
     for inset in self.insets:
         if isinstance(inset, tuple):
             height = inset[1]
             if isinstance(height, Operator):
                 rule = height
                 height = height.value
             else:
                 rule = self.side
             manager.rule = rule
             if float(inset[0]): # not zero
                 kwargs = {"height": height} if height else {}
                 polygon.inset(inset[0], **kwargs)
             else:
                 polygon.translate(height)
     # create a shape for the cap if necessary
     cap = self.cap
     if not isinstance(cap, Delete):
         shape = polygon.getShape(type(shape))
         if cap:
             context.pushState(shape=shape)
             self.cap.execute()
             context.popState()
     if not self.keepOriginal:
         context.facesForRemoval.append(face)
     # finalizing: if there is a rule for the shape, execute it
     for entry in manager.shapes:
         context.pushState(shape=entry[0])
         entry[1].execute()
         context.popState()
Exemple #11
0
    def extrude(self):
        bm = context.bm

        loop = self.firstLoop
        normal = loop.face.normal
        # lower vertex (along the first loop)
        vert1 = loop.vert.co
        # neighbor of vert1 to the right
        vert = loop.link_loop_next.vert.co
        # vector from vert1 to vert
        vec1 = vert - vert1
        vec1.normalize()
        # vector along the height of the band of rectangles
        axis = loop.link_loop_prev.vert.co - vert1
        if self.closed:
            # the special case of a closed band of rectangles
            # previous loop
            _loop = loop.link_loop_prev.link_loops[0].link_loop_prev
            vec2 = vec1
            vec1 = vert1 - _loop.vert.co
            vec1.normalize()

            _vertEx1 = getInset(vert1, vec1, vec2, self.getDepth(_loop),
                                self.getDepth(loop), _loop.face.normal, axis)
            _vertEx2 = bm.verts.new(_vertEx1 + axis)
            _vertEx1 = bm.verts.new(_vertEx1)
            prevVertEx1 = _vertEx1
            prevVertEx2 = _vertEx2
            # restore vec1
            vec1 = vec2
        else:
            # extruded counterpart of vert1
            prevVertEx1 = vert1 + self.getDepth(loop) * normal
            # upper vertex (upper neighbor of vert1)
            prevVertEx2 = bm.verts.new(prevVertEx1 + axis)
            prevVertEx1 = bm.verts.new(prevVertEx1)
            # starting rectangle
            shape = createRectangle((loop.vert, prevVertEx1, prevVertEx2,
                                     loop.link_loop_prev.vert))
            if self.join.material:
                context.pushState(shape=shape)
                self.join.material.execute()
                context.popState()

        index = self.end1
        while True:
            context.facesForRemoval.append(loop.face)
            _loop = loop
            _loopNext = _loop.link_loop_next
            if index == self.end2:
                break
            # next loop
            loop = loop.link_loop_next.link_loops[0].link_loop_next
            # neighbor of vert to the right
            vert2 = loop.link_loop_next.vert.co
            # vector from vert to vert2
            vec2 = vert2 - vert
            vec2.normalize()
            vertEx1 = getInset(vert, vec1, vec2, self.getDepth(_loop),
                               self.getDepth(loop), normal, axis)
            vertEx2 = bm.verts.new(vertEx1 + axis)
            vertEx1 = bm.verts.new(vertEx1)
            shape = createRectangle(
                (prevVertEx1, vertEx1, vertEx2, prevVertEx2))
            if self.join.material:
                context.pushState(shape=shape)
                self.join.material.execute()
                context.popState()
            # lower cap
            shape = createRectangle(
                (_loop.vert, _loopNext.vert, vertEx1, prevVertEx1))
            if self.join.material:
                context.pushState(shape=shape)
                self.join.material.execute()
                context.popState()
            # upper cap
            shape = createRectangle(
                (_loopNext.link_loop_next.vert, _loop.link_loop_prev.vert,
                 prevVertEx2, vertEx2))
            if self.join.material:
                context.pushState(shape=shape)
                self.join.material.execute()
                context.popState()

            vec1 = vec2
            vert = vert2
            prevVertEx1 = vertEx1
            prevVertEx2 = vertEx2
            normal = loop.face.normal

            index = loop.face.index

        if self.closed:
            vertEx1 = _vertEx1
            vertEx2 = _vertEx2
        else:
            vertEx1 = vert + self.getDepth(loop) * normal
            vertEx2 = bm.verts.new(vertEx1 + axis)
            vertEx1 = bm.verts.new(vertEx1)
            # closing rectangle
            loop = loop.link_loop_next
            shape = createRectangle(
                (vertEx1, loop.vert, loop.link_loop_next.vert, vertEx2))
            if self.join.material:
                context.pushState(shape=shape)
                self.join.material.execute()
                context.popState()
        shape = createRectangle((prevVertEx1, vertEx1, vertEx2, prevVertEx2))
        if self.join.material:
            context.pushState(shape=shape)
            self.join.material.execute()
            context.popState()
        # lower cap
        shape = createRectangle(
            (_loop.vert, _loopNext.vert, vertEx1, prevVertEx1))
        if self.join.material:
            context.pushState(shape=shape)
            self.join.material.execute()
            context.popState()
        # upper cap
        shape = createRectangle(
            (_loopNext.link_loop_next.vert, _loop.link_loop_prev.vert,
             prevVertEx2, vertEx2))
        if self.join.material:
            context.pushState(shape=shape)
            self.join.material.execute()
            context.popState()
Exemple #12
0
def apply(ruleFile, startRule="Begin"):
    from .bl_util import create_rectangle

    blenderContext = context.blenderContext
    obj = blenderContext.object
    if obj:
        bpy.ops.object.mode_set(mode="OBJECT")

    noMeshCondition = not obj or obj.type != "MESH"
    if noMeshCondition or len(obj.data.polygons) != 1:
        if not noMeshCondition:
            # delete if it's non-flat mesh
            bpy.ops.object.delete()
        create_rectangle(blenderContext, 20, 10)
    # apply all transformations to the active Blender object
    bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
    # setting the path to the rule for context
    context.ruleFile = ruleFile if isinstance(ruleFile,
                                              str) else ruleFile.__file__
    params = None
    mesh = blenderContext.object.data
    # create a uv layer for the mesh
    # TODO: a separate pass through the rules is needed to find out how many uv layers are needed
    mesh.uv_layers.new(name=Texture.defaultLayer)
    # initialize the context
    context.init()
    # initializing bmesh instance
    bm = bmesh.new()
    bm.from_mesh(mesh)
    if hasattr(bm.faces, "ensure_lookup_table"):
        bm.faces.ensure_lookup_table()
    context.addAttribute("bm", bm)
    # list of unused faces for removal
    context.addAttribute("facesForRemoval", [])
    # set up the material registry
    context.addAttribute("materialManager", MaterialManager())
    # set up vertex registry to ensure vertex uniqueness
    context.addAttribute("vertexRegistry", VertexRegistry())
    # set a constructor for join manager, it may be replaced by actual instance of the join manager
    context.addAttribute("joinManager", JoinManager)

    # push the initial state with the initial shape to the execution stack
    context.pushState(shape=getInitialShape(bm))

    if isinstance(ruleFile, str):
        module = getModule(ruleFile)

        # prepare context internal stuff
        context.prepare()
        # params is a list of tuples: (paramName, instanceofParamClass)
        params = getParams(module)
    else:
        # ruleFile is actually a module
        module = ruleFile

    # setting the current operator to a dummy one to avoid an exception
    class dummy:
        def addChildOperator(self, o):
            pass

        def removeChildOperators(self, numParts):
            pass

    context.operator = dummy()
    # evaluate the rule set
    getattr(module, startRule)().execute()

    # remove unused faces from context.facesForRemoval
    bmesh.ops.delete(bm, geom=context.facesForRemoval, context='FACES')
    # there still may be some doubles, inspite of the use of util.VertexMaterial
    #bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.0001)

    # clean up context.facesForRemoval
    context.facesForRemoval = []
    context.executeDeferred()
    # remove unused faces from context.facesForRemoval
    bmesh.ops.delete(bm, geom=context.facesForRemoval, context='FACES')

    # write everything back to the mesh
    bm.to_mesh(mesh)
    # cleaning context from blender specific members
    context.removeAttributes()

    return (module, params)
Exemple #13
0
def apply(ruleFile, startRule="Begin"):
	from .bl_util import create_rectangle
	
	blenderContext = context.blenderContext
	obj = blenderContext.object
	if obj:
		bpy.ops.object.mode_set(mode="OBJECT")
	
	noMeshCondition = not obj or obj.type != "MESH"
	if noMeshCondition or len(obj.data.polygons) != 1:
		if not noMeshCondition:
			# delete if it's non-flat mesh
			bpy.ops.object.delete()
		create_rectangle(blenderContext, 20, 10)
	# apply all transformations to the active Blender object
	bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
	# setting the path to the rule for context
	context.ruleFile = ruleFile if isinstance(ruleFile, str) else ruleFile.__file__
	params = None
	mesh = blenderContext.object.data
	# create a uv layer for the mesh
	# TODO: a separate pass through the rules is needed to find out how many uv layers are needed 
	mesh.uv_textures.new(Texture.defaultLayer)
	# initialize the context
	context.init()
	# initializing bmesh instance
	bm = bmesh.new()
	bm.from_mesh(mesh)
	if hasattr(bm.faces, "ensure_lookup_table"):
		bm.faces.ensure_lookup_table()
	context.addAttribute("bm", bm)
	# list of unused faces for removal
	context.addAttribute("facesForRemoval", [])
	# set up the material registry
	context.addAttribute("materialManager", MaterialManager())
	# set up vertex registry to ensure vertex uniqueness
	context.addAttribute("vertexRegistry", VertexRegistry())
	# set a constructor for join manager, it may be replaced by actual instance of the join manager
	context.addAttribute("joinManager", JoinManager)
	
	# push the initial state with the initial shape to the execution stack
	context.pushState(shape=getInitialShape(bm))
	
	if isinstance(ruleFile, str):
		module = getModule(ruleFile)

		# prepare context internal stuff
		context.prepare()
		# params is a list of tuples: (paramName, instanceofParamClass)
		params = getParams(module)
	else:
		# ruleFile is actually a module
		module = ruleFile
	
	# setting the current operator to a dummy one to avoid an exception
	class dummy:
		def addChildOperator(self, o): pass
		def removeChildOperators(self, numParts): pass
	context.operator = dummy()
	# evaluate the rule set
	getattr(module, startRule)().execute()
	
	# remove unused faces from context.facesForRemoval
	bmesh.ops.delete(bm, geom=context.facesForRemoval, context=5)
	# there still may be some doubles, inspite of the use of util.VertexMaterial
	#bmesh.ops.remove_doubles(bm, verts=bm.verts, dist=0.0001)
	
	# clean up context.facesForRemoval
	context.facesForRemoval = []
	context.executeDeferred()
	# remove unused faces from context.facesForRemoval
	bmesh.ops.delete(bm, geom=context.facesForRemoval, context=5)
	
	# write everything back to the mesh
	bm.to_mesh(mesh)
	# cleaning context from blender specific members
	context.removeAttributes()
	
	return (module, params)
Exemple #14
0
    def extrude(self):
        bm = context.bm
        
        loop = self.firstLoop
        normal = loop.face.normal
        # lower vertex (along the first loop)
        vert1 = loop.vert.co
        # neighbor of vert1 to the right
        vert = loop.link_loop_next.vert.co
        # vector from vert1 to vert
        vec1 = vert - vert1
        vec1.normalize()
        # vector along the height of the band of rectangles
        axis = loop.link_loop_prev.vert.co - vert1
        if self.closed:
            # the special case of a closed band of rectangles
            # previous loop
            _loop = loop.link_loop_prev.link_loops[0].link_loop_prev
            vec2 = vec1
            vec1 = vert1 - _loop.vert.co
            vec1.normalize()
            
            _vertEx1 = getInset(vert1, vec1, vec2, self.getDepth(_loop), self.getDepth(loop), _loop.face.normal, axis)
            _vertEx2 = bm.verts.new(_vertEx1 + axis)
            _vertEx1 = bm.verts.new(_vertEx1)
            prevVertEx1 = _vertEx1
            prevVertEx2 = _vertEx2
            # restore vec1
            vec1 = vec2
        else:
            # extruded counterpart of vert1
            prevVertEx1 = vert1 + self.getDepth(loop)*normal
            # upper vertex (upper neighbor of vert1)
            prevVertEx2 = bm.verts.new(prevVertEx1 + axis)
            prevVertEx1 = bm.verts.new(prevVertEx1)
            # starting rectangle
            shape = createRectangle((loop.vert, prevVertEx1, prevVertEx2, loop.link_loop_prev.vert))
            if self.join.material:
                context.pushState(shape=shape)
                self.join.material.execute()
                context.popState()

        index = self.end1
        while True:
            context.facesForRemoval.append(loop.face)
            _loop = loop
            _loopNext = _loop.link_loop_next
            if index==self.end2:
                break
            # next loop
            loop = loop.link_loop_next.link_loops[0].link_loop_next
            # neighbor of vert to the right
            vert2 = loop.link_loop_next.vert.co
            # vector from vert to vert2
            vec2 = vert2 - vert
            vec2.normalize()
            vertEx1 = getInset(vert, vec1, vec2, self.getDepth(_loop), self.getDepth(loop), normal, axis)
            vertEx2 = bm.verts.new(vertEx1 + axis)
            vertEx1 = bm.verts.new(vertEx1)
            shape = createRectangle((prevVertEx1, vertEx1, vertEx2, prevVertEx2))
            if self.join.material:
                context.pushState(shape=shape)
                self.join.material.execute()
                context.popState()
            # lower cap
            shape = createRectangle((_loop.vert, _loopNext.vert, vertEx1, prevVertEx1))
            if self.join.material:
                context.pushState(shape=shape)
                self.join.material.execute()
                context.popState()
            # upper cap
            shape = createRectangle((_loopNext.link_loop_next.vert, _loop.link_loop_prev.vert, prevVertEx2, vertEx2))
            if self.join.material:
                context.pushState(shape=shape)
                self.join.material.execute()
                context.popState()
            
            vec1 = vec2
            vert = vert2
            prevVertEx1 = vertEx1
            prevVertEx2 = vertEx2
            normal = loop.face.normal
            
            index = loop.face.index
        
        if self.closed:
            vertEx1 = _vertEx1
            vertEx2 = _vertEx2
        else:
            vertEx1 = vert + self.getDepth(loop)*normal
            vertEx2 = bm.verts.new(vertEx1 + axis)
            vertEx1 = bm.verts.new(vertEx1)
            # closing rectangle
            loop = loop.link_loop_next
            shape = createRectangle((vertEx1, loop.vert, loop.link_loop_next.vert, vertEx2))
            if self.join.material:
                context.pushState(shape=shape)
                self.join.material.execute()
                context.popState()
        shape = createRectangle((prevVertEx1, vertEx1, vertEx2, prevVertEx2))
        if self.join.material:
            context.pushState(shape=shape)
            self.join.material.execute()
            context.popState()
        # lower cap
        shape = createRectangle((_loop.vert, _loopNext.vert, vertEx1, prevVertEx1))
        if self.join.material:
            context.pushState(shape=shape)
            self.join.material.execute()
            context.popState()
        # upper cap
        shape = createRectangle((_loopNext.link_loop_next.vert, _loop.link_loop_prev.vert, prevVertEx2, vertEx2))
        if self.join.material:
            context.pushState(shape=shape)
            self.join.material.execute()
            context.popState()