Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Gcode Exporter" and "Evaluate Image" #2298

Merged
merged 16 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
SvProportionalEditNode
SvRaycasterLiteNode
SvOBJInsolationNode
EvaluateImageNode

## Transforms
SvRotationNode
Expand Down Expand Up @@ -310,6 +311,7 @@
SvSetCustomMeshNormals
---
SvSpiralNode
SvExportGcodeNode

## Alpha Nodes
SvCurveViewerNode
Expand Down Expand Up @@ -340,4 +342,3 @@
SvOffsetLineNode
SvContourNode
SvPlanarEdgenetToPolygons

195 changes: 195 additions & 0 deletions nodes/analyzer/evaluate_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

import bpy
from bpy.props import IntProperty, FloatProperty, StringProperty, EnumProperty

from sverchok.node_tree import SverchCustomTreeNode
from sverchok.data_structure import updateNode, fullList
from math import floor


class EvaluateImageNode(bpy.types.Node, SverchCustomTreeNode):
''' Evaluate Image '''
bl_idname = 'EvaluateImageNode'
bl_label = 'Evaluate Image'
bl_icon = 'FILE_IMAGE'

image_name = StringProperty(name='image_name', description='image name', default='', update=updateNode)

boundary_modes = [
("CLIP", "Clip", "", 0),
("CYCLIC", "Repeat", "", 1),
("MIRROR", "Mirror", "", 2)]

shift_modes = [
("NONE", "None", "", 0),
("ALTERNATE", "Alternate", "", 1),
("CONSTANT", "Constant", "", 2)]

shift_mode_U = EnumProperty(
name="U Shift",
description="U Shift",
default="NONE", items=shift_modes,
update=updateNode)

shift_mode_V = EnumProperty(
name="V Shift",
description="V Shift",
default="NONE", items=shift_modes,
update=updateNode)

boundU = EnumProperty(
name="U Bounds",
description="U Boundaries",
default="CYCLIC", items=boundary_modes,
update=updateNode)

boundV = EnumProperty(
name="V Bounds",
description="V Boundaries",
default="CYCLIC", items=boundary_modes,
update=updateNode)

domU = FloatProperty(
name='U domain', description='U domain', default=1, min=0.00001,
options={'ANIMATABLE'}, update=updateNode)

domV = FloatProperty(
name='V domain', description='V domain', default=1, min=0.00001,
options={'ANIMATABLE'}, update=updateNode)

shiftU = FloatProperty(
name='U shift', description='U shift', default=0.5, soft_min=-1, soft_max=1,
options={'ANIMATABLE'}, update=updateNode)

shiftV = FloatProperty(
name='V shift', description='V shift', default=0, soft_min=-1, soft_max=1,
options={'ANIMATABLE'}, update=updateNode)

def sv_init(self, context):
self.inputs.new('VerticesSocket', "Verts UV")
self.inputs.new('StringsSocket', "U domain").prop_name = 'domU'
self.inputs.new('StringsSocket', "V domain").prop_name = 'domV'
#if self.shift_mode_U not 'NONE':
#self.inputs.new('StringsSocket', "U shift").prop_name = 'shiftU'
#if self.shift_mode_V not 'NONE':
#self.inputs.new('StringsSocket', "V shift").prop_name = 'shiftV'
self.outputs.new('StringsSocket', "R")
self.outputs.new('StringsSocket', "G")
self.outputs.new('StringsSocket', "B")

def draw_buttons(self, context, layout):
layout.label(text="Image:")
layout.prop_search(self, "image_name", bpy.data, 'images', text="")

row = layout.row(align=True)
col = row.column(align=True)
col.label(text="Tile U:")
col.prop(self, "boundU", text="")
col.label(text="Shift U:")
col.prop(self, "shift_mode_U", text="")
if self.shift_mode_U != 'NONE':
col.prop(self, "shiftU", text="")

col = row.column(align=True)
col.label(text="Tile V:")
col.prop(self, "boundV", text="")
col.label(text="Shift V:")
col.prop(self, "shift_mode_V", text="")
if self.shift_mode_V != 'NONE':
col.prop(self, "shiftV", text="")


def process(self):
verts = self.inputs['Verts UV'].sv_get()

inputs, outputs = self.inputs, self.outputs

# inputs
if inputs['Verts UV'].is_linked:
verts = inputs['Verts UV'].sv_get()[0]
else:
verts = [(0,0,0),(0,1,0),(1,0,0),(1,1,0)]

if inputs['U domain'].is_linked:
domU = inputs['U domain'].sv_get()[0][0]
else: domU = self.domU

if inputs['V domain'].is_linked:
domV = inputs['V domain'].sv_get()[0][0]
else: domV = self.domV

# outputs
red = [[]]
green = [[]]
blue = [[]]

if outputs['R'].is_linked or outputs['G'].is_linked or outputs['B'].is_linked:
# copy images data, pixels is created on every access with [i], extreme speedup.
# http://blender.stackexchange.com/questions/3673/why-is-accessing-image-data-so-slow
imag = bpy.data.images[self.image_name].pixels[:]
sizeU = bpy.data.images[self.image_name].size[0]
sizeV = bpy.data.images[self.image_name].size[1]
for vert in verts:
vx = vert[0]*(sizeU-1)/domU
vy = vert[1]*sizeV/domV
u = floor(vx)
v = floor(vy)
u0 = u

if self.shift_mode_U == 'ALTERNATE':
if (v//sizeV)%2: u += floor(sizeU*self.shiftU)
if self.shift_mode_U == 'CONSTANT':
u += floor(sizeU*self.shiftU*(v//sizeV))
if self.boundU == 'CLIP':
u = max(0,min(u,sizeU-1))
elif self.boundU == 'CYCLIC':
u = u%sizeU
elif self.boundU == 'MIRROR':
if (u//sizeU)%2: u = sizeU - 1 - u%(sizeU)
else: u = u%(sizeU)


if self.shift_mode_V == 'ALTERNATE':
if (u0//sizeU)%2: v += floor(sizeV*self.shiftV)
if self.shift_mode_V == 'CONSTANT':
v += floor(sizeV*self.shiftV*(u0//sizeU))
if self.boundV == 'CLIP':
v = max(0,min(v,sizeV-1))
elif self.boundV == 'CYCLIC':
v = v%sizeV
elif self.boundV == 'MIRROR':
if (v//sizeV)%2: v = sizeV - 1 - v%(sizeV)
else: v = v%(sizeV)

index = int(u*4 + v*4*sizeU)
red[0].append(imag[index])
green[0].append(imag[index+1])
blue[0].append(imag[index+2])
outputs['R'].sv_set(red)
outputs['G'].sv_set(green)
outputs['B'].sv_set(blue)


def register():
bpy.utils.register_class(EvaluateImageNode)


def unregister():
bpy.utils.unregister_class(EvaluateImageNode)
18 changes: 9 additions & 9 deletions nodes/generator/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ class ImageNode(bpy.types.Node, SverchCustomTreeNode):
options={'ANIMATABLE'}, update=updateNode)

Xvecs = IntProperty(
name='Xvecs', description='Xvecs', default=10, min=2, max=100,
name='Xvecs', description='Xvecs', default=10, min=2, soft_max=100,
options={'ANIMATABLE'}, update=updateNode)

Yvecs = IntProperty(
name='Yvecs', description='Yvecs', default=10, min=2, max=100,
name='Yvecs', description='Yvecs', default=10, min=2, soft_max=100,
options={'ANIMATABLE'}, update=updateNode)

Xstep = FloatProperty(
name='Xstep', description='Xstep', default=1.0, min=0.01, max=100,
name='Xstep', description='Xstep', default=1.0, min=0.01, soft_max=100,
options={'ANIMATABLE'}, update=updateNode)

Ystep = FloatProperty(
name='Ystep', description='Ystep', default=1.0, min=0.01, max=100,
name='Ystep', description='Ystep', default=1.0, min=0.01, soft_max=100,
options={'ANIMATABLE'}, update=updateNode)

def sv_init(self, context):
Expand All @@ -82,12 +82,12 @@ def process(self):

# inputs
if inputs['vecs X'].is_linked:
IntegerX = min(int(inputs['vecs X'].sv_get()[0][0]), 100)
IntegerX = min(int(inputs['vecs X'].sv_get()[0][0]), 1000000)
else:
IntegerX = int(self.Xvecs)

if inputs['vecs Y'].is_linked:
IntegerY = min(int(inputs['vecs Y'].sv_get()[0][0]), 100)
IntegerY = min(int(inputs['vecs Y'].sv_get()[0][0]), 1000000)
else:
IntegerY = int(self.Yvecs)

Expand All @@ -109,7 +109,7 @@ def process(self):

if outputs['edgs'].is_linked:
listEdg = []

for i in range(IntegerY):
for j in range(IntegerX-1):
listEdg.append((IntegerX*i+j, IntegerX*i+j+1))
Expand All @@ -127,7 +127,7 @@ def process(self):
listPlg.append((IntegerX*j+i, IntegerX*j+i+1, IntegerX*j+i+IntegerX+1, IntegerX*j+i+IntegerX))
plg = [list(listPlg)]
outputs['pols'].sv_set(plg)


def make_vertices(self, delitelx, delitely, stepx, stepy, image_name):
lenx = bpy.data.images[image_name].size[0]
Expand All @@ -150,7 +150,7 @@ def make_vertices(self, delitelx, delitely, stepx, stepy, image_name):
# каждый пиксель кодируется RGBA, и записан строкой, без разделения на строки и столбцы.
middle = (imag[addition]*R+imag[addition+1]*G+imag[addition+2]*B)*imag[addition+3]
vertex = [x*stepx[x], y*stepy[y], middle]
vertices.append(vertex)
vertices.append(vertex)
addition += int(xcoef*4)
return vertices

Expand Down
Loading