WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   Creating objects with VizLayers and returning creates duplicates....? (https://forum.worldviz.com/showthread.php?t=4795)

Nyhood 09-06-2013 12:46 PM

Creating objects with VizLayers and returning creates duplicates....?
 
Code:

import viz
import vizact
import vizshape
from math import sin, cos, tan, pi

viz.go()

def createCube(cubeSize=2):
        viz.startLayer(viz.QUADS)
        #front
        viz.vertex(0,0,0)
        viz.vertex(cubeSize,0,0)
        viz.vertex(cubeSize,cubeSize,0)
        viz.vertex(0,cubeSize,0)
       
        #bottom
        viz.vertexColor(viz.RED)
        viz.vertex(0,0,0)
        viz.vertex(cubeSize,0,0)
        viz.vertex(cubeSize,0,cubeSize)
        viz.vertex(0,0,cubeSize)
       
        #left
        viz.vertexColor(viz.YELLOW)
        viz.vertex(0,0,0)
        viz.vertex(0,cubeSize,0)
        viz.vertex(0,cubeSize,cubeSize)
        viz.vertex(0,0,cubeSize)
       
        #right
        viz.vertexColor(viz.BLUE)
        viz.vertex(cubeSize,0,0)
        viz.vertex(cubeSize,cubeSize,0)
        viz.vertex(cubeSize,cubeSize,cubeSize)
        viz.vertex(cubeSize,0,cubeSize)
       
        #back
        viz.vertexColor(viz.GRAY)
        viz.vertex(0,0,cubeSize)
        viz.vertex(cubeSize,0,cubeSize)
        viz.vertex(cubeSize,cubeSize,cubeSize)
        viz.vertex(0,cubeSize,cubeSize)
       
        #top
        viz.vertexColor(viz.GREEN)
        viz.vertex(0,cubeSize,0)
        viz.vertex(cubeSize,cubeSize,0)
        viz.vertex(cubeSize,cubeSize,cubeSize)
        viz.vertex(0,cubeSize,cubeSize)
        cube = viz.endLayer()

        #rotate = vizact.spin(x=0,y=45,z=0,speed=90)
        #myQuad.addAction(rotate)
        return cube

def createCylinder(radius=3,height=5,detail=10):
        angleIncrement = 360/detail;
       
        #create a group of everything
        cylinder = viz.addGroup()
       
        viz.startLayer(viz.POLYGON)
        #viz.vertex(0,0,0)
        for i in range(detail):
                x = radius * cos(((i*angleIncrement)*pi)/180)
                y = radius * sin(((i*angleIncrement)*pi)/180)
#                x = radius * cos(i*angleIncrement)
#                y = radius * sin(i*angleIncrement)
                print("x is " + str(x))
                print("y is " + str(y))
                viz.normal(x,0,y)
                viz.vertex(x,0,y)
        bottom = viz.endLayer(parent=cylinder)
       
       
#make the top
        viz.startLayer(viz.POLYGON)
        #viz.vertex(0,height,0)
        for i in range(detail):
                x = radius * cos(((i*angleIncrement)*pi)/180)
                y = radius * sin(((i*angleIncrement)*pi)/180)
                viz.vertex(x,height,y)
        top = viz.endLayer(parent=cylinder)
       
        #now make all the sides
        viz.startLayer(viz.QUAD_STRIP)
        viz.vertexColor(viz.GRAY)
        for i in range(detail):
                x = radius * cos(((i*angleIncrement)*pi)/180)
                y = radius * sin(((i*angleIncrement)*pi)/180)
                viz.vertex(x,0,y)
                viz.vertex(x,height,y)
       
        x = radius * cos(((0*angleIncrement)*pi)/180)
        y = radius * sin(((0*angleIncrement)*pi)/180)
        viz.vertex(x,0,y)
        viz.vertex(x,height,y)
        sides = viz.endLayer(parent=cylinder)

        return cylinder

box1 = createCube(9);
box1.setPosition(0,5,0)

head = createCube(2)
head.addParent(box1)
head.setEuler(180,0,0)
head.setPosition(3,8,0)

arm1 = createCylinder(1,9,20)
arm1.addParent(box1)
arm1.setPosition(-3,-2,3)
arm1.setEuler(0,0,-20)

arm2 = createCylinder(1,9,20)
arm2.addParent(box1)
arm2.setPosition(11,-2,3)
arm2.setEuler(0,0,20)

I am creating objects and assigning them to values through function return values. For some reason, everything I create is duplicated somehow and I'm at a loss. Am I already placing the object, then placing it again by assigning it to a value? New to Python and WorldViz.

farshizzo 09-11-2013 01:41 PM

Try using setParent instead of addParent. When you use addParent the object will still retain its current parents.


All times are GMT -7. The time now is 07:53 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC