Text Size
Text Size
Exit Full Screen
Python Mini
Show
Source Code
View Source Code in Full Screen
Open in New Tab
from processing import * from math import sin, cos camx = 100.0 camy = -35.0 camz = 100.0 keys = [] state = 'report' textbox = '' textname = '' updated = '' selected = None delmar = 0 clipboard = [] boxes = [] mb = 0 def touching(x1,y1,x2,y2,sx1,sy1,sx2,sy2): if x1+sx1 >= x2 and x1 <= x2+sx2 and y1+sy1 >= y2 and y1 <= y2+sy2: return True else: return False def button(x,txt,txtn,upd,stt='textboxselpos'): global textname, state, updated, textbox fill(170) if touching(mouse.x,mouse.y,10+x,10,0,0,70,30): fill(50) if mb == 1: textname = txtn;state = stt;updated = upd;textbox = ''; rect(20+x,20,70,30) fill(0);textSize(20);translate(0,0,10);text(txt,30+x,35);translate(0,0,-10); def buttonSelector(x,txt,sell,sx=0,y=0,stt='menu'): global textname, state, updated, textbox, selected fill(170) if touching(mouse.x,mouse.y,10+x,10+y,0,0,70+sx,30): fill(50) if mb == 1: state = stt;selected = sell rect(20+x,20+y,70+sx,30) fill(0);textSize(20);translate(0,0,10);text(txt,30+x,35+y);translate(0,0,-10); def buttonState(x,txt,stt): global textname, state, updated, textbox, selected fill(170) if touching(mouse.x,mouse.y,10+x,10,0,0,70,30): fill(50) if mb == 1: state = stt; rect(20+x,20,70,30) fill(0);textSize(20);translate(0,0,10);text(txt,30+x,35);translate(0,0,-10); def bound(a,lower,upper=None): if a < lower and lower != None: return lower elif a > upper and upper != None: return upper else: return a def setup(): size(500,500,P3D) def mousePressed(): global mb mb = 1 def mouseReleased(): global mb mb = 0 def keyPressed(): global keys, textbox, state, boxes, textname if keyboard.keyCode not in keys: keys.append(keyboard.keyCode) #print keys if state[:7] == 'textbox': if keyboard.keyCode not in [16,UP,DOWN,LEFT,RIGHT,17,18,10]: textbox = textbox + keyboard.key if keyboard.keyCode == LEFT: textbox = textbox[:len(textbox)-1] elif keyboard.keyCode == 10: k = state state = state[7:] if updated == 'selname': for i in boxes: if i[6] == textbox: state = k textname = 'Name already used:' if state != k: boxes[selected][6] = textbox elif updated == 'selx': try: boxes[selected][0] = float(textbox) except: state = k elif updated == 'sely': try: boxes[selected][1] = float(textbox) except: state = k elif updated == 'selz': try: boxes[selected][2] = float(textbox) except: state = k elif updated == 'selscax': try: boxes[selected][3] = float(textbox) except: state = k elif updated == 'selscay': try: boxes[selected][4] = float(textbox) except: state = k elif updated == 'selscaz': try: boxes[selected][5] = float(textbox) except: state = k def keyReleased(): global keys if keyboard.keyCode in keys: keys.remove(keyboard.keyCode) def draw(): global camx, camy, camz, boxes, mb, selected, state, textname, textbox, delmar global updated, keys, clipboard if delmar > 0: delmar -= 1 if state[:7] != 'textbox': if 68 in keys: camx -= PI/100 if 65 in keys: camx += PI/100 camx %= TAU if 83 in keys and camy < 1000: camy += 2 if 87 in keys and camy > -1000: camy -= 2 if 69 in keys and camz < 1000: camz += 2 if 81 in keys and camz > 2: camz -= 2 background(0) lights() camera(cos(camx)*camz, camy, sin(camx)*camz, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) if state[:6] != 'report': stroke(255,0,0) line(100,0,0,0,0,0) stroke(0,255,0) line(0,100,0,0,0,0) stroke(0,0,255) pushMatrix() translate(0,0,50) box(.1,.1,100) popMatrix() stroke(255) noFill() box(10) for j in range(len(boxes)): i = boxes[j] stroke(0) fill(255) if j == selected: stroke(0,255,0) fill(200,255,200) pushMatrix() translate(i[0],i[1],i[2]) box(i[3],i[4],i[5]) popMatrix() camera() stroke(0) fill(200) rect(0,0,500,50) pushMatrix() translate(0,0,20) if state[:7] == 'textbox': fill(0) textSize(20) text(textname,20,20) text(textbox,20,40) elif state[:4] == 'menu': fill(170) if touching(mouse.x,mouse.y,10,10,0,0,100,30): fill(50) if mb == 1: boxes.insert(0,[0,0,0,10,10,10,'']) selected = 0 textname = 'Box Name:';updated = 'selname';state = 'textboxselpos';textbox = '' rect(20,20,100,30) fill(0);textSize(20);translate(0,0,10);text('New Box',30,35);translate(0,0,-10); buttonState(380,'Help','report1') fill(170) if touching(mouse.x,mouse.y,300,10,0,0,70,30): fill(50) if mb == 1: prev = [0,0,0,0,0,0] print 'pushMatrix()' print 'lights()' print 'camera('+str(cos(camx)*camz)+', '+str(camy)+', '+str(sin(camx)*camz)+', 0, 0, 0, 0, 1, 0)' for l in boxes: i = [] for j in range(6): i.append(float(l[j])) i.append(l[6]) print 'translate('+str(i[0]-prev[0])+', '+str(i[1]-prev[1])+', '+str(i[2]-prev[2])+')'; print 'box('+str(i[3])+', '+str(i[4])+', '+str(i[5])+') #' + i[6]; prev = i print 'popMatrix()' print rect(310,20,70,30) fill(0);textSize(20);translate(0,0,10);text('Export',320,35);translate(0,0,-10); if state[4:] == 'select': fill(170) if touching(mouse.x,mouse.y,130,10,0,0,150,30): fill(50) if mb == 1: state = 'menu' rect(140,20,140,30) fill(0);textSize(20);translate(0,0,10);text('Back',150,35);translate(0,0,-10); for j in range(len(boxes)): i = boxes[j] if len(boxes) > 11: k = -40*((bound(float(mouse.y)-40,0))/460)*(len(boxes)-10) else: k = 0 buttonSelector(120,i[6],j,70,40*(j+1)+k,'selpos') fill(130) rect(130,10,160,40*(len(boxes)+1)+10) else: fill(170) if touching(mouse.x,mouse.y,130,10,0,0,150,30): fill(50) if mb == 1: state = 'menuselect' rect(140,20,140,30) fill(0);textSize(20);translate(0,0,10);text('Select Existing',150,35);translate(0,0,-10); if LEFT in keys: if camx/PI < .25 or camx/PI >= 1.75: for i in boxes: i[2] += .5 elif camx/PI < .75 and camx/PI >= .25: for i in boxes: i[0] -= .5 elif camx/PI < 1.25 and camx/PI >= .75: for i in boxes: i[2] -= .5 else: for i in boxes: i[0] += .5 elif RIGHT in keys: if camx/PI < .25 or camx/PI >= 1.75: for i in boxes: i[2] -= .5 elif camx/PI < .75 and camx/PI >= .25: for i in boxes: i[0] += .5 elif camx/PI < 1.25 and camx/PI >= .75: for i in boxes: i[2] += .5 else: for i in boxes: i[0] -= .5 elif UP in keys: for i in boxes: i[1] -= .5 elif DOWN in keys: for i in boxes: i[1] += .5 if 32 in keys: if 86 in keys and clipboard != []: boxes.insert(0,[clipboard[0],clipboard[1],clipboard[2],clipboard[3],clipboard[4],clipboard[5],'']) selected = 0 textname = 'Box Name:';updated = 'selname';state = 'textboxselpos';textbox = '' elif state[:3] == 'sel': if state[3:6] == 'pos': button(0,'Edit X','New X','selx') button(80,'Edit Y','New Y','sely') button(160,'Edit Z','New Z','selz') buttonState(240,'Scale','selsca') buttonSelector(380,'Back',None) if LEFT in keys: if camx/PI < .25 or camx/PI >= 1.75: boxes[selected][2] += .5 elif camx/PI < .75 and camx/PI >= .25: boxes[selected][0] -= .5 elif camx/PI < 1.25 and camx/PI >= .75: boxes[selected][2] -= .5 else: boxes[selected][0] += .5 elif RIGHT in keys: if camx/PI < .25 or camx/PI >= 1.75: boxes[selected][2] -= .5 elif camx/PI < .75 and camx/PI >= .25: boxes[selected][0] += .5 elif camx/PI < 1.25 and camx/PI >= .75: boxes[selected][2] += .5 else: boxes[selected][0] -= .5 elif UP in keys: boxes[selected][1] -= .5 elif DOWN in keys: boxes[selected][1] += .5 else: button(0,'Length','New Length','selscax','textboxselsca') button(80,'Height','New Height','selscay','textboxselsca') button(160,'Width','New Width','selscaz','textboxselsca') buttonState(240,'Move','selpos') fill(255,200,200) if touching(mouse.x,mouse.y,390,10,0,0,70,30): fill(50) if mb == 1: if delmar == 0: delmar = 90 else: boxes.pop(selected) selected = None state = 'menu' rect(400,20,70,30) fill(0);textSize(20);translate(0,0,10); if delmar == 0: text('Delete',410,35) else: text('Sure?',410,35) translate(0,0,-10); if LEFT in keys: if (camx/PI < .25 or camx/PI >= 1.75) or (camx/PI < 1.25 and camx/PI >= .75): boxes[selected][5] -= .5 else: boxes[selected][3] -= .5 elif RIGHT in keys: if (camx/PI < .25 or camx/PI >= 1.75) or (camx/PI < 1.25 and camx/PI >= .75): boxes[selected][5] += .5 else: boxes[selected][3] += .5 elif UP in keys: boxes[selected][4] += .5 elif DOWN in keys: boxes[selected][4] -= .5 if 32 in keys: if 68 in keys: boxes.insert(0,[boxes[selected][0],boxes[selected][1],boxes[selected][2],boxes[selected][3],boxes[selected][4],boxes[selected][5],'']) selected = 0 textname = 'Box Name:';updated = 'selname';state = 'textboxselpos';textbox = '' elif 67 in keys: clipboard = [boxes[selected][0],boxes[selected][1],boxes[selected][2],boxes[selected][3],boxes[selected][4],boxes[selected][5],''] elif 86 in keys and clipboard != []: boxes.insert(0,[clipboard[0],clipboard[1],clipboard[2],clipboard[3],clipboard[4],clipboard[5],'']) selected = 0 textname = 'Box Name:';updated = 'selname';state = 'textboxselpos';textbox = '' elif 88 in keys: boxes[selected][0] *= -1 keys.remove(88) elif 89 in keys: boxes[selected][1] *= -1 keys.remove(89) elif 90 in keys: boxes[selected][2] *= -1 keys.remove(90) elif state[:6] == 'report': stroke(0) fill(170) rect(20,20,100,30) rect(140,20,140,30) rect(400,20,70,30) rect(310,20,70,30) fill(0);textSize(20);translate(0,0,10);text('New Box',30,35);text('Select Existing',150,35);text('Help',410,35);text('Export',320,35);translate(0,0,-10); fill(220) rect(100,100,300,30) fill(170) rect(100,100,300,300) fill(0) translate(0,0,10); if len(state) == 6: text('New Transmission',110,115); text('On the subject of: Modeling',110,155); text('Welcome to p-3D modeler!\nTo view controls, press Help.',110,205); text('Press export to export the\nmodel to P3D code.',110,275); fill(170) if touching(mouse.x,mouse.y,200,340,0,0,80,40): fill(50) if mb == 1: state = 'menu'; rect(210,340,70,30) fill(0);textSize(20);translate(0,0,10);text('OK',220,355);translate(0,0,-10); else: text('Help',110,115); if state[6:] == '1': text('Controls:',110,155); text('At Anytime:',110,205); text('W: move camera up.\nS: move camera down.\nD: move clockwise around origin.\nA: move cCw around origin.',110,235); elif state[6:] == '2': text('Q: move closer to the origin.\nE: Move farther from the origin.',110,205); text('Help: Open this menu.',110,285) elif state[6:] == '3': text('When nothing is selected:',110,155); text('Arrow keys: move everything',110,205); text('up, down, left, and right.',110,225); text('Select Existing: choose an',110,265) text('already existing box to select.',110,285); elif state[6:] == '4': text('New Box:',110,175) text('Asks for an identifier for\na new box and creates it.',110,205); text('Export: Gives the model and\ncamera position as P3D code.',110,285); elif state[6:] == '5': text('While in a textbox:',110,155); text('Type to enter text.\nEnter: Submit.\nLeft Arrow key: Backspace.',110,205); elif state[6:] == '6': text('While a box is selected:',110,155); text('Move/Scale: Switch between\nmove mode and scale\nmode.',110,205); elif state[6:] == '7': text('In Move mode:',110,155); text('Arrow keys: move the box\nup, down, left, and right.',110,195); text("Edit X/Y/Z: Enter a number\nfor the box's X/Y/Z.",110,265); text("Back: deselect the box.",110,325); elif state[6:] == '8': text('In Scale mode:',110,155); text('Arrow keys: Scale the box\nwide, long, and high.',110,195); text("Length/Height/Width: Enter a\nnumber for the parameter.",110,265); text("Delete: delete the box.",110,325); elif state[6:] == '9': text('Button Combos:',110,155); text('Space + D: Clone selected box.\nSpace + C: Copy selected box.\nSpace + V: Paste from clipboard.\nSpace + X: Flip X.\nSpace + Y: Flip Y.\nSpace + Z: Flip Z.',110,180); fill(170) if touching(mouse.x,mouse.y,110,340,0,0,80,40): fill(50) if mb == 1: state = 'menu'; rect(120,340,70,30) fill(0);textSize(20);translate(0,0,10);text('Back',130,355);translate(0,0,-10); if state[6:] != '9': fill(170) if touching(mouse.x,mouse.y,300,340,0,0,80,40): fill(50) if mb == 1: state = 'report' + str(int(state[6:])+1); rect(310,340,70,30) fill(0);textSize(20);translate(0,0,10);text('Next',320,355);translate(0,0,-10); translate(0,0,-10); popMatrix() if mb == 1: mb = 2 run()
modeler.py
( around 428 lines python code )
Published By:
luyfru
Published on
2023-09-21T18:56:41Z
Python Mini
- an
OYOclass
application,
own your own class today
.
Run
Result
×
Error message shows here